-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathazure-redis.cs
More file actions
executable file
·41 lines (34 loc) · 1.29 KB
/
azure-redis.cs
File metadata and controls
executable file
·41 lines (34 loc) · 1.29 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
// Program.cs
builder.Services.AddSingleton<AzureConnectors>();
// AzureConnectors.cs
public class AzureConnectors
{
private readonly Lazy<IConnectionMultiplexer> _connectionMultiplexer;
public IConnectionMultiplexer ConnectionMultiplexer => _connectionMultiplexer.Value;
public IDatabase GetRedisDatabase(int id = -1) => ConnectionMultiplexer.GetDatabase(id);
public AzureConnectors(IConfiguration configuration, ILogger<AzureConnectors> logger)
{
_connectionMultiplexer = new(() =>
{
logger.LogInformation("Initialized ConnectionMultiplexer");
return StackExchange.Redis.ConnectionMultiplexer.Connect(configuration.GetConnectionString("Redis"));
}, LazyThreadSafetyMode.ExecutionAndPublication);
}
}
// RedisCache.cs
public class RedisCache
{
private readonly AzureConnectors _azureConnectors;
public RedisCache(AzureConnectors azureConnectors)
{
_azureConnectors = azureConnectors;
}
public void Remove(string key)
{
_azureConnectors.GetRedisDatabase().KeyDelete(key, CommandFlags.FireAndForget);
}
}
// appsettings.json
"ConnectionStrings": {
"Redis" : "webinario-prod-redis.redis.cache.windows.net:6380,password=EAyTGRpw5syogbTUWtjWA72Mz66do1aMoAzCaBVPaJw=,ssl=True,abortConnect=False"
}