-
Notifications
You must be signed in to change notification settings - Fork 7
/
RedisTokenCacheService.cs
104 lines (89 loc) · 3.54 KB
/
RedisTokenCacheService.cs
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using System;
using System.Threading.Tasks;
using AspNetCore.Authentication.ApiToken.Abstractions;
using MessagePack;
using MessagePack.Resolvers;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using StackExchange.Redis;
namespace AspNetCore.Authentication.ApiToken.Redis
{
public class RedisTokenCacheService : IApiTokenCacheService, IAsyncDisposable
{
private readonly ILogger<RedisTokenCacheService> _logger;
private readonly RedisTokenCacheOptions _options;
private IDatabase _cache;
private ConnectionMultiplexer _connection;
private readonly string _tokenCacheKeyPrefix;
public RedisTokenCacheService(IOptions<RedisTokenCacheOptions> options, ILogger<RedisTokenCacheService> logger)
{
_logger = logger;
_options = options.Value;
_tokenCacheKeyPrefix = _options.CachePrefix + ":{0}:token:{1}";
}
public async Task InitializeAsync()
{
_connection = await ConnectionMultiplexer.ConnectAsync(_options.ConnectionString);
_cache = _connection.GetDatabase();
_logger.LogInformation("Redis token cache service init successful.");
}
private string GetKey(string token, string scheme)
{
return string.Format(_tokenCacheKeyPrefix, scheme, token);
}
public async Task<TokenModelCache> GetAsync(string token, string scheme)
{
var key = GetKey(token, scheme);
var cacheData = await _cache.StringGetAsync(key);
if (!cacheData.HasValue)
{
return default;
}
return MessagePackSerializer.Deserialize<TokenModelCache>(cacheData, ContractlessStandardResolver.Options);
}
public async Task SetAsync(TokenModel token)
{
var key = GetKey(token.Scheme, token.Value);
await _cache.StringSetAsync(key, Serialize(new TokenModelCache() { Token = token }), token.LifeTime);
}
public async Task SetNullAsync(string invalidToken, string scheme)
{
var key = GetKey(scheme, invalidToken);
if (_options.InvalidTokenNullCacheTimeSpan != null)
{
var ttl = _options.InvalidTokenNullCacheTimeSpan.Value;
await _cache.StringSetAsync(key, Serialize(new TokenModelCache()), ttl);
}
}
public async Task RemoveAsync(string token, string scheme)
{
var key = GetKey(scheme, token);
await _cache.KeyDeleteAsync(key);
}
public async Task<bool> LockTakeAsync(string key, string value, TimeSpan timeOut)
{
var lockKey = string.Format(_tokenCacheKeyPrefix, key);
return await _cache.LockTakeAsync(lockKey, value, timeOut);
}
public async Task LockReleaseAsync(string key, string value)
{
var lockKey = string.Format(_tokenCacheKeyPrefix, key);
var result = await _cache.LockReleaseAsync(lockKey, value);
if (!result)
{
_logger.LogError($"Lock release failed, Key: {lockKey}, Value: {value}");
}
}
private static byte[] Serialize(TokenModelCache data)
{
return MessagePackSerializer.Serialize(data, ContractlessStandardResolver.Options);
}
public async ValueTask DisposeAsync()
{
if (_connection != null)
{
await _connection.CloseAsync();
}
}
}
}