Skip to content

Commit ad92c43

Browse files
committed
Enhance caching and argument validation
This commit significantly improves the `DistributedCachingServices` class by introducing robust argument validation using the `ArgumentValidator` library and expanding caching capabilities to support both memory and Redis-based strategies. Key changes include the addition of `Microsoft.Extensions.Caching.Memory` and `Microsoft.Extensions.Caching.StackExchangeRedis` packages for enhanced caching flexibility, namespace and dependency adjustments to accommodate new features, and method enhancements for better nullability handling and argument validation. Notably, a new `GetAllKeys` method has been added to fetch all cache keys, with specific implementations for Redis and in-memory caching. Project file updates reflect the addition of necessary package references.
1 parent 9636928 commit ad92c43

File tree

2 files changed

+55
-8
lines changed

2 files changed

+55
-8
lines changed
Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
using Microsoft.Extensions.Caching.Distributed;
1+
using ArgumentValidator;
2+
using Microsoft.Extensions.Caching.Distributed;
3+
using Microsoft.Extensions.Caching.Memory;
4+
using Microsoft.Extensions.Caching.StackExchangeRedis;
25
using Promact.Core.Caching;
6+
using StackExchange.Redis;
7+
using System.Reflection;
38
using System.Text.Json;
49

510
namespace Promact.Caching
@@ -9,27 +14,66 @@ public class DistributedCachingServices : ICachingService
914
private readonly IDistributedCache _distributedCache;
1015
public DistributedCachingServices(IDistributedCache distributedCache)
1116
{
12-
_distributedCache = distributedCache;
17+
_distributedCache = distributedCache;
1318
}
14-
public T? Get<T>(string key)
19+
public T Get<T>(string key)
1520
{
1621
if (key == null) throw new ArgumentNullException(nameof(key));
1722
var data = _distributedCache.GetString(key);
18-
return data == null ? default : JsonSerializer.Deserialize<T>(data);
23+
#pragma warning disable CS8603 // Possible null reference return.
24+
return string.IsNullOrEmpty(data) ? default(T) : JsonSerializer.Deserialize<T>(data);
25+
#pragma warning restore CS8603 // Possible null reference return.
1926
}
2027

2128
public void Remove(string key)
2229
{
2330
if (key == null) throw new ArgumentNullException(nameof(key));
24-
_distributedCache.Remove(key);
31+
_distributedCache.Remove(key);
2532
}
2633

2734
public void Set<T>(string key, T value)
2835
{
29-
if(key == null) throw new ArgumentNullException(nameof(key));
30-
if(value == null) throw new ArgumentNullException(nameof(value));
36+
Throw.IfNull(key, nameof(key));
37+
Throw.IfNull(value, nameof(value));
3138
var data = JsonSerializer.Serialize(value);
3239
_distributedCache.SetString(key, data);
3340
}
41+
42+
#pragma warning disable CS8602 // Dereference of a possibly null reference.
43+
#pragma warning disable CS8600 // Converting null literal or possible null value to non-nullable type.
44+
#pragma warning disable CS8604 // Possible null reference argument.
45+
public HashSet<string> GetAllKeys()
46+
{
47+
//Check if the Caching provider is Redis then cast it to RedisCache and get the keys
48+
if (_distributedCache is RedisCache)
49+
{
50+
var redisCache = _distributedCache as RedisCache;
51+
52+
IDatabase db = redisCache.GetType().GetField("_cache", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(redisCache) as IDatabase;
53+
var keysResult = ((RedisValue[])db.Execute("KEYS", "*"));
54+
return new HashSet<string>(keysResult.Select(k => k.ToString()));
55+
56+
}
57+
//Check if the Caching provider is InMemory then cast it and return all the keys
58+
else if (_distributedCache is MemoryDistributedCache)
59+
{
60+
var memoryCache = _distributedCache as MemoryDistributedCache;
61+
var cache = memoryCache.GetType().GetField("_memCache", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(memoryCache) as MemoryCache;
62+
dynamic _coherentState = cache.GetType().GetField("_coherentState", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(cache);
63+
var keys = new HashSet<string>();
64+
foreach (KeyValuePair<object, dynamic> pairs in _coherentState._entries)
65+
{
66+
keys.Add(Convert.ToString(pairs.Key));
67+
}
68+
return keys;
69+
}
70+
else
71+
{
72+
throw new NotSupportedException("This method is not supported for the current caching provider");
73+
}
74+
}
75+
#pragma warning restore CS8600 // Converting null literal or possible null value to non-nullable type.
76+
#pragma warning restore CS8602 // Dereference of a possibly null reference.
77+
#pragma warning restore CS8604 // Possible null reference argument.
3478
}
35-
}
79+
}

Promact.Caching/Promact.Caching/Promact.Caching.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10+
<PackageReference Include="Argument.Validator" Version="2.0.0" />
1011
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="8.0.0" />
12+
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="8.0.0" />
13+
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="8.0.6" />
1114
<PackageReference Include="Promact.Core" Version="1.0.0.2" />
1215
<PackageReference Include="System.IO.Hashing" Version="8.0.0" />
1316
<PackageReference Include="xxHash.Core" Version="2.0.1" />

0 commit comments

Comments
 (0)