|
1 | 1 | using Microsoft.AspNetCore.Http; |
2 | | -using Microsoft.Extensions.Caching.Memory; |
| 2 | +using Microsoft.Extensions.Caching.Distributed; |
3 | 3 | using System.Security.Claims; |
4 | 4 |
|
5 | 5 | namespace SharedLibrary.Cache |
6 | 6 | { |
7 | 7 | public sealed class CacheAccessProvider : ICacheAccessProvider |
8 | 8 | { |
9 | | - private readonly IMemoryCache _cache; |
| 9 | + private readonly IDistributedCache _cache; |
10 | 10 | private readonly IHttpContextAccessor _http; |
11 | 11 |
|
12 | | - public CacheAccessProvider(IMemoryCache cache, IHttpContextAccessor http) |
| 12 | + public CacheAccessProvider(IDistributedCache cache, IHttpContextAccessor http) |
13 | 13 | { |
14 | 14 | _cache = cache; |
15 | 15 | _http = http; |
16 | 16 | } |
17 | 17 |
|
18 | | - public Task<string?> GetAccessTokenAsync(CancellationToken ct = default) |
| 18 | + public async Task<string?> GetAccessTokenAsync(CancellationToken ct = default) |
19 | 19 | { |
20 | 20 | var user = _http.HttpContext?.User; |
21 | 21 | var uid = |
22 | 22 | user?.FindFirst("sub")?.Value |
23 | 23 | ?? user?.FindFirst(ClaimTypes.NameIdentifier)?.Value; |
24 | 24 |
|
25 | | - if (string.IsNullOrEmpty(uid)) return Task.FromResult<string?>(null); |
| 25 | + if (string.IsNullOrEmpty(uid)) return null; |
26 | 26 |
|
27 | | - var key = $"token:{uid}"; |
28 | | - _cache.TryGetValue(key, out string? token); |
29 | | - return Task.FromResult(token); |
| 27 | + var key = $"auth:token:{uid}"; |
| 28 | + var token = await _cache.GetStringAsync(key, ct); |
| 29 | + return token; |
| 30 | + |
30 | 31 | } |
31 | 32 |
|
32 | | - public Task<string?> GetUserPermissionsAsync(CancellationToken ct = default) |
| 33 | + public async Task<string?> GetUserPermissionsAsync(CancellationToken ct = default) |
33 | 34 | { |
34 | 35 | var user = _http.HttpContext?.User; |
35 | 36 | var uid = |
36 | 37 | user?.FindFirst("sub")?.Value |
37 | 38 | ?? user?.FindFirst(ClaimTypes.NameIdentifier)?.Value; |
38 | 39 |
|
39 | | - if (string.IsNullOrEmpty(uid)) return Task.FromResult<string?>(null); |
| 40 | + if (string.IsNullOrEmpty(uid)) return null; |
40 | 41 |
|
41 | | - var key = $"perm:{uid}"; |
42 | | - _cache.TryGetValue(key, out string? permissions); |
43 | | - return Task.FromResult(permissions); |
| 42 | + var key = $"auth:permissions:{uid}"; |
| 43 | + var permissions = await _cache.GetStringAsync(key, ct); |
| 44 | + return permissions; |
44 | 45 | } |
45 | 46 |
|
46 | 47 | // Optional helper method to set the token into cache |
47 | | - public void SetAccessToken(string token, int userId, DateTime expiresAtUtc) |
48 | | - { |
49 | | - var ttl = ToTtl(expiresAtUtc); |
50 | | - _cache.Set($"token:{userId}", token, ttl); |
| 48 | + public async Task SetAccessToken(string token, int userId, DateTime expiresAtUtc, CancellationToken ct = default) |
| 49 | + { |
| 50 | + var ttl = ToTtl(expiresAtUtc); |
| 51 | + |
| 52 | + var options = new DistributedCacheEntryOptions |
| 53 | + { |
| 54 | + AbsoluteExpirationRelativeToNow = ttl |
| 55 | + }; |
| 56 | + |
| 57 | + // IMPORTANT: Do not manually prefix here if using InstanceName in startup. |
| 58 | + var key = $"auth:token:{userId}"; |
| 59 | + |
| 60 | + await _cache.SetStringAsync(key, token, options, ct); |
51 | 61 | } |
52 | 62 |
|
53 | | - public void SetUserPermissions(string permissions, int userId, DateTime expiresAtUtc) |
| 63 | + public async Task SetUserPermissions(string permissions, int userId, DateTime expiresAtUtc, CancellationToken ct = default) |
54 | 64 | { |
55 | 65 | var ttl = ToTtl(expiresAtUtc); |
56 | | - _cache.Set($"perm:{userId}", permissions, ttl); |
| 66 | + |
| 67 | + var options = new DistributedCacheEntryOptions |
| 68 | + { |
| 69 | + AbsoluteExpirationRelativeToNow = ttl |
| 70 | + }; |
| 71 | + |
| 72 | + // IMPORTANT: Do not manually prefix here if using InstanceName in startup. |
| 73 | + var key = $"auth:permissions:{userId}"; |
| 74 | + |
| 75 | + await _cache.SetStringAsync(key, permissions, options, ct); |
57 | 76 | } |
58 | 77 |
|
59 | 78 | public Task RemoveAsync(string userId, CancellationToken ct = default) |
60 | 79 | { |
61 | | - _cache.Remove($"token:{userId}"); |
| 80 | + _cache.Remove($"auth:token:{userId}"); |
| 81 | + _cache.Remove($"auth:permissions:{userId}"); |
62 | 82 | return Task.CompletedTask; |
63 | 83 | } |
64 | 84 |
|
|
0 commit comments