-
Notifications
You must be signed in to change notification settings - Fork 525
Expand file tree
/
Copy pathRedisMessageCacheManager.cs
More file actions
63 lines (52 loc) · 2.08 KB
/
RedisMessageCacheManager.cs
File metadata and controls
63 lines (52 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using Grand.Infrastructure.Caching.Message;
using Grand.Infrastructure.Configuration;
using MediatR;
using Microsoft.Extensions.Caching.Memory;
namespace Grand.Infrastructure.Caching.Redis;
public class RedisMessageCacheManager : MemoryCacheBase, ICacheBase
{
private readonly IMemoryCache _cache;
private readonly IMessageBus _messageBus;
public RedisMessageCacheManager(IMemoryCache cache, IMediator mediator, IMessageBus messageBus, CacheConfig config)
: base(cache, mediator, config)
{
_cache = cache;
_messageBus = messageBus;
}
/// <summary>
/// Removes the value with the specified key from the cache
/// </summary>
/// <param name="key">Key of cached item</param>
/// <param name="publisher">Publisher</param>
public override Task RemoveAsync(string key, bool publisher = true)
{
_cache.Remove(key);
if (publisher)
_messageBus.PublishAsync(new MessageEvent { Key = key, MessageType = (int)MessageEventType.RemoveKey });
return Task.CompletedTask;
}
/// <summary>
/// Removes items by key prefix
/// </summary>
/// <param name="prefix">String prefix</param>
/// <param name="publisher">publisher</param>
public override Task RemoveByPrefix(string prefix, bool publisher = true)
{
var entriesToRemove = CacheEntries.Where(x => x.Key.StartsWith(prefix, StringComparison.OrdinalIgnoreCase));
foreach (var cacheEntries in entriesToRemove) _cache.Remove(cacheEntries.Key);
if (publisher)
_messageBus.PublishAsync(new MessageEvent
{ Key = prefix, MessageType = (int)MessageEventType.RemoveByPrefix });
return Task.CompletedTask;
}
/// <summary>
/// Clear cache
/// </summary>
/// <param name="publisher">publisher</param>
public override async Task Clear(bool publisher = true)
{
await base.Clear(publisher);
if (publisher)
await _messageBus.PublishAsync(new MessageEvent { Key = "", MessageType = (int)MessageEventType.ClearCache });
}
}