Skip to content

Commit

Permalink
Develop (#45)
Browse files Browse the repository at this point in the history
* Efcache 1.3.2 upgrade with netstandard2.1 (#44)

* Added netstandard2.0 target framework.

* Upgrade to EFCache v1.3.2, because of broken binary formatter serialization on System.RuntimeType (see moozzyk/EFCache#51) since dotnet core 2.

Added target framework netstandard2.1.

Co-authored-by: Van Meerbeeck, Serge <[email protected]>

* Prepare PR for release

Co-authored-by: Ian Robertson <[email protected]>
Co-authored-by: sergevm <[email protected]>
Co-authored-by: Van Meerbeeck, Serge <[email protected]>
Co-authored-by: Ian Robertson <[email protected]>
  • Loading branch information
5 people authored Nov 9, 2020
1 parent efae42e commit d23e2d6
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 68 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*.user
*.sln.docstates
.vs
.idea

# Build results

Expand Down
6 changes: 3 additions & 3 deletions EFCache.Redis.Tests/EFCache.Redis.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net48</TargetFramework>
<TargetFrameworks>net48;netcoreapp3.0</TargetFrameworks>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>

</PropertyGroup>

<ItemGroup>
<PackageReference Include="Moq" Version="4.14.5" />
<PackageReference Include="Moq" Version="4.14.7" />
<PackageReference Include="MSTest.TestAdapter" Version="2.1.2" />
<PackageReference Include="MSTest.TestFramework" Version="2.1.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.0" />
</ItemGroup>

<ItemGroup>
Expand Down
54 changes: 21 additions & 33 deletions EFCache.Redis.Tests/RedisCacheTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public RedisCacheTests()
try
{
// See if we have a running copy of redis in a K8s Cluster
// helm install --name redis-dev --set password=secretpassword --set master.disableCommands= stable/redis
// helm upgrade --install redis-dev --set password=secretpassword --set master.disableCommands= bitnami/redis

// kubectl get secret --namespace default redis-dev -o jsonpath="{.data.redis-password}" | base64 --decode
// kubectl port-forward --namespace default svc/redis-dev-master 6379:6379
var connString = "localhost:6379,password=secretpassword";
Expand All @@ -38,12 +39,12 @@ public RedisCacheTests()
AdminConnectionString = string.Join(",", connString, "allowAdmin=true");

}
catch (Exception)
catch(Exception)
{
// Could not connect to redis above, so start a local copy
RedisStorageEmulatorManager.Instance.StartProcess(false);
}

}

[TestMethod]
Expand Down Expand Up @@ -98,7 +99,7 @@ public void Item_still_returned_after_sliding_expiration_period()

object fromCache = null;
// In a loop of 20 seconds retrieve the item every 5 second seconds.
for (var i = 0; i < 4; i++)
for(var i = 0; i < 4; i++)
{
Thread.Sleep(5000); // Wait 5 seconds
// Retrieve item again. This should update LastAccess and as such keep the item 'alive'
Expand Down Expand Up @@ -155,7 +156,7 @@ public void Count_returns_numers_of_cached_entries()
Assert.AreEqual(0, cache.Count);
}


[TestMethod]
public async Task ThreadingBlockTest()
{
Expand All @@ -167,8 +168,10 @@ public async Task ThreadingBlockTest()

cache.CachingFailed += (sender, e) =>
{
if (e?.InnerException is LockTimeoutException)
if(e?.InnerException is LockTimeoutException)
{
exception = e.InnerException;
}
};
cache.Purge();

Expand All @@ -185,20 +188,21 @@ public async Task ThreadingBlockTest()

var tasks = new Task[10];

for (var i = 0; i < 10; i++)
for(var i = 0; i < 10; i++)
{
var icopy = i;
tasks[i] = Task.Run(() =>
{
var watch = new Stopwatch();
watch.Start();
Debug.WriteLine($"Invalidate {icopy} start");
if (i == 9)
if(i == 9)
{
cache.InvalidateItem("1");
}
else
{
object val;
cache.GetItem("1", out val);
cache.GetItem("1", out var val);
}
watch.Stop();
Debug.WriteLine($"Invalidate {icopy} complete after {watch.ElapsedMilliseconds}");
Expand All @@ -211,8 +215,7 @@ public async Task ThreadingBlockTest()
Debug.WriteLine($"Get start");
var watch = new Stopwatch();
watch.Start();
object value;
cache.GetItem("1", out value);
cache.GetItem("1", out var value);
watch.Stop();
Debug.WriteLine($"Get complete after {watch.ElapsedMilliseconds}");
});
Expand All @@ -227,10 +230,7 @@ public async Task ThreadingBlockTest()

}

private void Cache_CachingFailed(object sender, RedisCacheException e)
{
throw new NotImplementedException();
}
private void Cache_CachingFailed(object sender, RedisCacheException e) => throw new NotImplementedException();


[TestMethod]
Expand All @@ -244,7 +244,7 @@ public void Count_does_not_return_expired_entries()

cache.PutItem("1", new object(), new string[0], TimeSpan.MaxValue, DateTimeOffset.Now.AddSeconds(1));

Assert.AreEqual(1, cache.Count);
Assert.AreEqual(1, cache.Count);

Thread.Sleep(1000);

Expand Down Expand Up @@ -282,31 +282,19 @@ public void GetItem_validates_parameters()

[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void PutItem_validates_key_parameter()
{
new RedisCache(RegularConnectionString).PutItem(null, 42, new string[0], TimeSpan.Zero, DateTimeOffset.Now);
}
public void PutItem_validates_key_parameter() => new RedisCache(RegularConnectionString).PutItem(null, 42, new string[0], TimeSpan.Zero, DateTimeOffset.Now);

[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void PutItem_validates_dependentEntitySets_parameter()
{
new RedisCache(RegularConnectionString).PutItem("1", 42, null, TimeSpan.Zero, DateTimeOffset.Now);
}
public void PutItem_validates_dependentEntitySets_parameter() => new RedisCache(RegularConnectionString).PutItem("1", 42, null, TimeSpan.Zero, DateTimeOffset.Now);

[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void InvalidateSets_validates_parameters()
{
new RedisCache(RegularConnectionString).InvalidateSets(null);
}
public void InvalidateSets_validates_parameters() => new RedisCache(RegularConnectionString).InvalidateSets(null);

[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void InvalidateItem_validates_parameters()
{
new RedisCache(RegularConnectionString).InvalidateItem(null);
}
public void InvalidateItem_validates_parameters() => new RedisCache(RegularConnectionString).InvalidateItem(null);

[TestMethod]
public void GetItem_does_not_crash_if_cache_is_unavailable()
Expand Down
12 changes: 9 additions & 3 deletions EFCache.Redis.Tests/StorageEmulatorManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,17 @@ protected StorageEmulatorManager(string processName, ProcessStartInfo processSta

public void StartProcess(bool waitForExit)
{
if (IsProcessStarted()) return;
if(IsProcessStarted())
{
return;
}

using (var process = Process.Start(_processStartInfo))
using(var process = Process.Start(_processStartInfo))
{
if (process != null && waitForExit) process.WaitForExit();
if(process != null && waitForExit)
{
process.WaitForExit();
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions EFCache.Redis/EFCache.Redis.csproj
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net48</TargetFramework>
<TargetFrameworks>net461;net472;net48;netstandard2.1</TargetFrameworks>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="EntityFramework.Cache" Version="1.3.1" />
<PackageReference Include="EntityFramework.Cache" Version="1.3.2" />
<PackageReference Include="StackExchange.Redis" Version="2.1.58" />
</ItemGroup>

Expand Down
6 changes: 4 additions & 2 deletions EFCache.Redis/EFCache.Redis.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
<metadata>
<id>EFCache.Redis</id>
<version>2020.8.21.1</version>
<version>2020.8.21.2</version>
<authors>Ian Robertson</authors>
<projectUrl>https://github.com/silentbobbert/EFCache.Redis</projectUrl>
<iconUrl>https://avatars0.githubusercontent.com/u/3257988</iconUrl>
Expand All @@ -12,7 +12,7 @@
<tags>EF6 EF6.3 Redis Caching L2</tags>
<dependencies>
<dependency id="EntityFramework" version="6.3.0" />
<dependency id="EntityFramework.Cache" version="1.3.1" />
<dependency id="EntityFramework.Cache" version="1.3.2" />
<dependency id="StackExchange.Redis" version="2.1.58" />
<dependency id="Pipelines.Sockets.Unofficial" version="2.1.16" />
<dependency id="System.Buffers" version="4.5.1" />
Expand All @@ -33,5 +33,7 @@
<file src="bin\Release\net472\EFCache.Redis.pdb" target="lib\net472\EFCache.Redis.pdb" />
<file src="bin\Release\net48\EFCache.Redis.dll" target="lib\net48\EFCache.Redis.dll" />
<file src="bin\Release\net48\EFCache.Redis.pdb" target="lib\net48\EFCache.Redis.pdb" />
<file src="bin\Release\netstandard2.1\EFCache.Redis.dll" target="lib\netstandard2.1\EFCache.Redis.dll" />
<file src="bin\Release\netstandard2.1\EFCache.Redis.pdb" target="lib\netstandard2.1\EFCache.Redis.pdb" />
</files>
</package>
3 changes: 1 addition & 2 deletions EFCache.Redis/IRedisCache.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using EFCache;
using System;
using System;

namespace EFCache.Redis
{
Expand Down
4 changes: 2 additions & 2 deletions EFCache.Redis/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@
// Build Number
// Revision
//
[assembly: AssemblyVersion("2020.8.21.1")]
[assembly: AssemblyFileVersion("2020.8.21.1")]
[assembly: AssemblyVersion("2020.11.9.1")]
[assembly: AssemblyFileVersion("2020.11.9.1")]

16 changes: 13 additions & 3 deletions EFCache.Redis/RedisCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ public bool GetItem(string key, out object value)
OnCachingFailed(e);
}

if (value == null) return false;
if (value == null)
{
return false;
}

var entry = (CacheEntry)value;

Expand Down Expand Up @@ -187,7 +190,11 @@ private TimeSpan GetTimeSpanExpiration(DateTimeOffset expiration)
private static string HashKey(string key)
{
//Looking up large Keys in Redis can be expensive (comparing Large Strings), so if keys are large, hash them, otherwise if keys are short just use as-is
if (key.Length <= 128) return key;
if (key.Length <= 128)
{
return key;
}

using (var sha = new SHA1CryptoServiceProvider())
{
key = Convert.ToBase64String(sha.ComputeHash(Encoding.UTF8.GetBytes(key)));
Expand Down Expand Up @@ -244,7 +251,10 @@ public void InvalidateItem(string key)
{
var entry = _database.Get<CacheEntry>(key);

if (entry == null) return;
if (entry == null)
{
return;
}

_database.KeyDelete(key, CommandFlags.FireAndForget);

Expand Down
12 changes: 10 additions & 2 deletions EFCache.Redis/StackExchangeRedisExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ public static void Set<T>(this IDatabase cache, string key, T value, TimeSpan ex

static byte[] Serialize<T>(T o) where T : class
{
if (o == null) return null;
if (o == null)
{
return null;
}

var binaryFormatter = new BinaryFormatter();

using (var memoryStream = new MemoryStream())
Expand All @@ -34,7 +38,11 @@ static byte[] Serialize<T>(T o) where T : class

static T Deserialize<T>(byte[] stream)
{
if (stream == null || !stream.Any()) return default(T);
if (stream == null || !stream.Any())
{
return default(T);
}

var binaryFormatter = new BinaryFormatter();
using (var memoryStream = new MemoryStream(stream))
{
Expand Down
16 changes: 0 additions & 16 deletions EFCache.Redis/packages.config

This file was deleted.

0 comments on commit d23e2d6

Please sign in to comment.