|
| 1 | +using System; |
| 2 | +using System.Threading; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using Microsoft.Extensions.Caching.Distributed; |
| 5 | +using StackExchange.Redis; |
| 6 | + |
| 7 | +namespace GeneXus.Application |
| 8 | +{ |
| 9 | + public class CustomRedisSessionStore : IDistributedCache |
| 10 | + { |
| 11 | + private readonly IDatabase _db; |
| 12 | + private readonly TimeSpan _idleTimeout; |
| 13 | + private readonly TimeSpan _refreshThreshold; |
| 14 | + private readonly string _instanceName; |
| 15 | + |
| 16 | + public CustomRedisSessionStore(string connectionString, TimeSpan idleTimeout, string instanceName) |
| 17 | + { |
| 18 | + var mux = ConnectionMultiplexer.Connect(connectionString); |
| 19 | + _db = mux.GetDatabase(); |
| 20 | + _idleTimeout = idleTimeout; |
| 21 | + _refreshThreshold = TimeSpan.FromTicks((long)(idleTimeout.Ticks * 0.2)); |
| 22 | + _instanceName = instanceName ?? string.Empty; |
| 23 | + } |
| 24 | + |
| 25 | + private string FormatKey(string key) => string.IsNullOrEmpty(_instanceName) ? key : $"{_instanceName}:{key}"; |
| 26 | + |
| 27 | + public byte[] Get(string key) => _db.StringGet(FormatKey(key)); |
| 28 | + |
| 29 | + public async Task<byte[]> GetAsync(string key, CancellationToken token = default) |
| 30 | + { |
| 31 | + string redisKey = FormatKey(key); |
| 32 | + var value = await _db.StringGetAsync(redisKey); |
| 33 | + |
| 34 | + await RefreshKeyIfNeededAsync(redisKey); |
| 35 | + |
| 36 | + return value; |
| 37 | + } |
| 38 | + |
| 39 | + public void Refresh(string key) |
| 40 | + { |
| 41 | + string redisKey = FormatKey(key); |
| 42 | + |
| 43 | + var ttl = _db.KeyTimeToLive(redisKey); |
| 44 | + |
| 45 | + if (ShouldRefreshKey(ttl)) |
| 46 | + { |
| 47 | + _db.KeyExpire(redisKey, _idleTimeout); |
| 48 | + } |
| 49 | + } |
| 50 | + private bool ShouldRefreshKey(TimeSpan? ttl) |
| 51 | + { |
| 52 | + return ttl.HasValue && ttl.Value < _refreshThreshold; |
| 53 | + } |
| 54 | + public async Task RefreshAsync(string key, CancellationToken token = default) |
| 55 | + { |
| 56 | + string redisKey = FormatKey(key); |
| 57 | + await RefreshKeyIfNeededAsync(redisKey); |
| 58 | + } |
| 59 | + private async Task RefreshKeyIfNeededAsync(string redisKey) |
| 60 | + { |
| 61 | + var ttl = await _db.KeyTimeToLiveAsync(redisKey); |
| 62 | + |
| 63 | + if (ShouldRefreshKey(ttl)) |
| 64 | + { |
| 65 | + _ = _db.KeyExpireAsync(redisKey, _idleTimeout); |
| 66 | + } |
| 67 | + } |
| 68 | + public void Remove(string key) => _db.KeyDelete(FormatKey(key)); |
| 69 | + |
| 70 | + public Task RemoveAsync(string key, CancellationToken token = default) |
| 71 | + => _db.KeyDeleteAsync(FormatKey(key)); |
| 72 | + |
| 73 | + public void Set(string key, byte[] value, DistributedCacheEntryOptions options) |
| 74 | + { |
| 75 | + _db.StringSet(FormatKey(key), value, _idleTimeout); |
| 76 | + } |
| 77 | + |
| 78 | + public Task SetAsync(string key, byte[] value, DistributedCacheEntryOptions options, CancellationToken token = default) |
| 79 | + { |
| 80 | + return _db.StringSetAsync(FormatKey(key), value, _idleTimeout); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | +} |
0 commit comments