Skip to content

Commit 7b63e26

Browse files
authored
Add GetAllKeysByPrefix to InMemory and Redis (#422)
* - add getall (inMemory - redis) - add getall-keys (inMemory - resis) * refactor get-all and get-all-keys with optional prefix * add GetAllKeysByPrefix for InMemoryCache and Redis * handle base KeyPrefix from config in result GetAllKeysByPrefix
1 parent be302f9 commit 7b63e26

24 files changed

+312
-29
lines changed
Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace EasyCaching.Demo.ConsoleApp
1+
using System.Threading.Tasks;
2+
3+
namespace EasyCaching.Demo.ConsoleApp
24
{
35
using EasyCaching.Core;
46
using EasyCaching.SQLite;
@@ -16,13 +18,13 @@ static void Main(string[] args)
1618
{
1719
option.UseInMemory("m1");
1820

19-
//option.UseRedis(config =>
20-
//{
21-
// config.DBConfig = new Redis.RedisDBOptions { Configuration = "localhost" };
22-
// config.SerializerName = "json";
23-
//}, "r1");
24-
25-
21+
// option.UseRedis(config =>
22+
// {
23+
// config.DBConfig = new Redis.RedisDBOptions { Configuration = "localhost" };
24+
// config.SerializerName = "json";
25+
// }, "r1");
26+
//
27+
2628
option.UseSQLite(c =>
2729
{
2830
c.DBConfig = new SQLiteDBOptions
@@ -33,43 +35,51 @@ static void Main(string[] args)
3335
};
3436
}, "s1");
3537

36-
//option.WithJson(jsonSerializerSettingsConfigure: x =>
37-
//{
38-
// x.TypeNameHandling = Newtonsoft.Json.TypeNameHandling.None;
39-
//}, "json");
38+
// option.WithJson(jsonSerializerSettingsConfigure: x =>
39+
// {
40+
// x.TypeNameHandling = Newtonsoft.Json.TypeNameHandling.None;
41+
// }, "json");
4042
});
4143

4244
IServiceProvider serviceProvider = services.BuildServiceProvider();
4345
var factory = serviceProvider.GetService<IEasyCachingProviderFactory>();
44-
45-
//var redisCache = factory.GetCachingProvider("r1");
46-
47-
//redisCache.Set<Product>("rkey", new Product() { Name = "test" }, TimeSpan.FromSeconds(20));
48-
49-
//var redisVal = redisCache.Get<Product>("rkey");
50-
51-
//Console.WriteLine($"redis cache get value, {redisVal.HasValue} {redisVal.IsNull} {redisVal.Value} ");
52-
53-
46+
47+
// var redisCache = factory.GetCachingProvider("r1");
48+
//
49+
// redisCache.Set<Product>("rkey", new Product() { Name = "test" }, TimeSpan.FromSeconds(20));
50+
//
51+
// var redisAllKey = redisCache.GetAllKeysByPrefix("rkey");
52+
//
53+
// var redisVal = redisCache.Get<Product>("rkey");
54+
//
55+
// Console.WriteLine($"redis cache get value, {redisVal.HasValue} {redisVal.IsNull} {redisVal.Value}");
56+
57+
58+
5459
var mCache = factory.GetCachingProvider("m1");
55-
60+
5661
mCache.Set<Product>("mkey1", new Product() { Name = "test" }, TimeSpan.FromSeconds(20));
5762

5863
var mVal1 = mCache.Get<Product>("mkey1");
5964

60-
6165
mCache.Set<string>("mkey", "mvalue", TimeSpan.FromSeconds(20));
62-
66+
6367
var mVal = mCache.Get<string>("mkey");
64-
68+
69+
var mAllKey = mCache.GetAllKeysByPrefix("mk");
70+
6571
Console.WriteLine($"in-memory cache get value, {mVal.HasValue} {mVal.IsNull} {mVal.Value} ");
6672

73+
6774
var sCache = factory.GetCachingProvider("s1");
6875

76+
6977
sCache.Set<string>("skey", "svalue", TimeSpan.FromSeconds(20));
7078

79+
7180
var sVal = sCache.Get<string>("skey");
7281

82+
7383
Console.WriteLine($"sqlite cache get value, {sVal.HasValue} {sVal.IsNull} {sVal.Value} ");
7484

7585
Console.ReadKey();
@@ -82,3 +92,4 @@ public class Product
8292
public string Name { get; set; }
8393
}
8494
}
95+

src/EasyCaching.CSRedis/DefaultCSRedisCachingProvider.Async.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ public override async Task<IDictionary<string, CacheValue<T>>> BaseGetAllAsync<T
6464
return result;
6565
}
6666

67+
public override Task<IEnumerable<string>> BaseGetAllKeysByPrefixAsync(string prefix, CancellationToken cancellationToken = default)
68+
{
69+
throw new NotSupportedException();
70+
}
71+
6772
/// <summary>
6873
/// Gets the async.
6974
/// </summary>

src/EasyCaching.CSRedis/DefaultCSRedisCachingProvider.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,11 @@ public override IDictionary<string, CacheValue<T>> BaseGetAll<T>(IEnumerable<str
252252
return result;
253253
}
254254

255+
public override IEnumerable<string> BaseGetAllKeysByPrefix(string prefix)
256+
{
257+
throw new NotSupportedException();
258+
}
259+
255260
/// <summary>
256261
/// Handles the prefix of CacheKey.
257262
/// </summary>

src/EasyCaching.Core/EasyCachingAbstractProvider.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ protected EasyCachingAbstractProvider(IDistributedLockFactory lockFactory, BaseP
4949
public abstract Task BaseFlushAsync(CancellationToken cancellationToken = default);
5050
public abstract CacheValue<T> BaseGet<T>(string cacheKey, Func<T> dataRetriever, TimeSpan expiration);
5151
public abstract CacheValue<T> BaseGet<T>(string cacheKey);
52+
public abstract IEnumerable<string> BaseGetAllKeysByPrefix(string prefix);
53+
public abstract Task<IEnumerable<string>> BaseGetAllKeysByPrefixAsync(string prefix, CancellationToken cancellationToken = default);
5254
public abstract IDictionary<string, CacheValue<T>> BaseGetAll<T>(IEnumerable<string> cacheKeys);
5355
public abstract Task<IDictionary<string, CacheValue<T>>> BaseGetAllAsync<T>(IEnumerable<string> cacheKeys, CancellationToken cancellationToken = default);
5456
public abstract Task<CacheValue<T>> BaseGetAsync<T>(string cacheKey, Func<Task<T>> dataRetriever, TimeSpan expiration, CancellationToken cancellationToken = default);
@@ -256,6 +258,58 @@ public CacheValue<T> Get<T>(string cacheKey)
256258
}
257259
}
258260
}
261+
262+
public IEnumerable<string> GetAllKeysByPrefix(string prefix)
263+
{
264+
var operationId = s_diagnosticListener.WriteGetCacheBefore(new BeforeGetRequestEventData(CachingProviderType.ToString(), Name, nameof(GetAllKeysByPrefix), new[] { prefix }));
265+
Exception e = null;
266+
try
267+
{
268+
return BaseGetAllKeysByPrefix(prefix);
269+
}
270+
catch (Exception ex)
271+
{
272+
e = ex;
273+
throw;
274+
}
275+
finally
276+
{
277+
if (e != null)
278+
{
279+
s_diagnosticListener.WriteGetCacheError(operationId, e);
280+
}
281+
else
282+
{
283+
s_diagnosticListener.WriteGetCacheAfter(operationId);
284+
}
285+
}
286+
}
287+
288+
public async Task<IEnumerable<string>> GetAllKeysByPrefixAsync(string prefix, CancellationToken cancellationToken = default)
289+
{
290+
var operationId = s_diagnosticListener.WriteGetCacheBefore(new BeforeGetRequestEventData(CachingProviderType.ToString(), Name, nameof(GetAllKeysByPrefixAsync), new[] { prefix }));
291+
Exception e = null;
292+
try
293+
{
294+
return await BaseGetAllKeysByPrefixAsync(prefix);
295+
}
296+
catch (Exception ex)
297+
{
298+
e = ex;
299+
throw;
300+
}
301+
finally
302+
{
303+
if (e != null)
304+
{
305+
s_diagnosticListener.WriteGetCacheError(operationId, e);
306+
}
307+
else
308+
{
309+
s_diagnosticListener.WriteGetCacheAfter(operationId);
310+
}
311+
}
312+
}
259313

260314
public IDictionary<string, CacheValue<T>> GetAll<T>(IEnumerable<string> cacheKeys)
261315
{

src/EasyCaching.Core/IEasyCachingProvider.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,23 @@ public interface IEasyCachingProvider : IEasyCachingProviderBase
2121
/// </summary>
2222
/// <value><c>true</c> if is use lock; otherwise, <c>false</c>.</value>
2323
bool UseLock { get; }
24+
25+
/// <summary>
26+
/// Gets all keys by prefix.
27+
/// </summary>
28+
/// <param name="prefix">Prefix.</param>
29+
/// <returns>The all keys by prefix.</returns>
30+
IEnumerable<string> GetAllKeysByPrefix(string prefix);
31+
32+
/// <summary>
33+
/// Gets all keys by prefix async.
34+
/// </summary>
35+
/// <param name="prefix">Prefix.</param>
36+
/// <param name="cancellationToken">CancellationToken</param>
37+
/// <returns>The all keys by prefix async.</returns>
38+
Task<IEnumerable<string>> GetAllKeysByPrefixAsync(string prefix, CancellationToken cancellationToken = default);
2439

25-
/// <summary>
40+
/// <summary>
2641
/// Gets all.
2742
/// </summary>
2843
/// <returns>The all.</returns>

src/EasyCaching.Disk/DefaultDiskCachingProvider.Async.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ public override async Task<IDictionary<string, CacheValue<T>>> BaseGetAllAsync<T
8585
return dict;
8686
}
8787

88+
89+
public override Task<IEnumerable<string>> BaseGetAllKeysByPrefixAsync(string prefix, CancellationToken cancellationToken = default)
90+
{
91+
throw new NotSupportedException();
92+
}
93+
8894
public override async Task<CacheValue<T>> BaseGetAsync<T>(string cacheKey, Func<Task<T>> dataRetriever, TimeSpan expiration, CancellationToken cancellationToken = default)
8995
{
9096
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));

src/EasyCaching.Disk/DefaultDiskCachingProvider.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,11 @@ public override IDictionary<string, CacheValue<T>> BaseGetAll<T>(IEnumerable<str
336336
return dict;
337337
}
338338

339+
public override IEnumerable<string> BaseGetAllKeysByPrefix(string prefix)
340+
{
341+
throw new NotSupportedException();
342+
}
343+
339344
public override IDictionary<string, CacheValue<T>> BaseGetByPrefix<T>(string prefix)
340345
{
341346
ArgumentCheck.NotNullOrWhiteSpace(prefix, nameof(prefix));

src/EasyCaching.FasterKv/DefaultFasterKvCachingProvider.Async.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ public override async Task BaseFlushAsync(CancellationToken cancellationToken =
2929
}
3030
}
3131

32+
public override Task<IEnumerable<string>> BaseGetAllKeysByPrefixAsync(string prefix, CancellationToken cancellationToken = default)
33+
{
34+
throw new NotSupportedException();
35+
}
3236

3337
public override async Task<IDictionary<string, CacheValue<T>>> BaseGetAllAsync<T>(IEnumerable<string> cacheKeys,
3438
CancellationToken cancellationToken = default)

src/EasyCaching.FasterKv/DefaultFasterKvCachingProvider.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,11 @@ public override CacheValue<T> BaseGet<T>(string cacheKey)
161161
using var session = GetSession();
162162
return BaseGetInternal<T>(cacheKey, session);
163163
}
164+
165+
public override IEnumerable<string> BaseGetAllKeysByPrefix(string prefix)
166+
{
167+
throw new NotSupportedException();
168+
}
164169

165170
public override IDictionary<string, CacheValue<T>> BaseGetAll<T>(IEnumerable<string> cacheKeys)
166171
{

src/EasyCaching.InMemory/DefaultInMemoryCachingProvider.Async.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,21 @@ public override async Task<IDictionary<string, CacheValue<T>>> BaseGetAllAsync<T
275275

276276
return await Task.FromResult(_cache.GetAll<T>(cacheKeys));
277277
}
278+
279+
280+
/// <summary>
281+
/// Get all cacheKey by prefix async.
282+
/// </summary>
283+
/// <param name="prefix">Cache keys.</param>
284+
/// <param name="cancellationToken">Cache keys.</param>
285+
/// <returns>Get all cacheKey by prefix async.</returns>
286+
public override Task<IEnumerable<string>> BaseGetAllKeysByPrefixAsync(string prefix, CancellationToken cancellationToken = default)
287+
{
288+
if (_options.EnableLogging)
289+
_logger?.LogInformation("GetAllKeysAsync");
290+
291+
return Task.FromResult(_cache.GetAllKeys(prefix));
292+
}
278293

279294
/// <summary>
280295
/// Gets the by prefix async.

0 commit comments

Comments
 (0)