-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRedisAdminCache.cs
More file actions
58 lines (53 loc) · 2.32 KB
/
Copy pathRedisAdminCache.cs
File metadata and controls
58 lines (53 loc) · 2.32 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
using Microsoft.Extensions.Logging;
using StackExchange.Redis;
namespace Sagara.Core.Caching;
/// <summary>
/// <para>!!! IMPORTANT !!! This class is only intended to be used as a singleton because we construct the
/// multiplexer in our constructor, and constructing multiplexers is expensive.</para>
/// <para>We have to use an instance created via DI instead of a static class because we need to be able
/// inject Configuration to have access to the connection string.</para>
/// </summary>
/// <remarks>
/// NOTE: Be sure to set abortConnect=false in the connection string so that we gracefully handle connection failures.
/// </remarks>
public class RedisAdminCache : RedisCache
{
private readonly ILogger<RedisAdminCache> _logger;
/// <summary>
/// <para>!!! IMPORTANT !!! This class is only intended to be used as a singleton because we construct the
/// multiplexer in our constructor, and constructing multiplexers is expensive.</para>
/// <para>We have to use an instance created via DI instead of a static class because we need to be able
/// inject Configuration to have access to the connection string.</para>
/// </summary>
/// <remarks>
/// <para>Same as <see cref="RedisCache"/>, but it supports protected operations, such as FLUSH.</para>
/// <para>NOTE: Be sure to set abortConnect=false in the connection string so that we gracefully handle connection failures.</para>
/// </remarks>
public RedisAdminCache(ILogger<RedisAdminCache> logger, string connectionString, RedisProtocol redisProtocol)
: base(logger, connectionString, redisProtocol, allowAdmin: true)
{
_logger = logger;
}
/// <summary>
/// Delete all the keys of all databases on the server.
/// </summary>
/// <returns></returns>
public async Task FlushAllAsync()
{
try
{
foreach (var endPoint in Multiplexer.GetEndPoints())
{
var server = Multiplexer.GetServer(endPoint);
await server
.FlushAllDatabasesAsync()
.ConfigureAwait(false);
}
}
catch (Exception ex)
{
// Don't let the cache server bring down the application.
_logger.Error_UnhandledException(ex, command: "FLUSHALL", key: "(all keys)");
}
}
}