Skip to content

Commit 992b84d

Browse files
author
claudiamurialdo
committed
Use the CustomRedisSessionStore.
1 parent a1dba76 commit 992b84d

File tree

2 files changed

+87
-6
lines changed

2 files changed

+87
-6
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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+
}

dotnet/src/dotnetcore/GxNetCoreStartup/Startup.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -312,12 +312,9 @@ private void ConfigureSessionService(IServiceCollection services, ISessionServic
312312
{
313313
if (sessionService is GxRedisSession)
314314
{
315-
services.AddStackExchangeRedisCache(options =>
316-
{
317-
GXLogging.Info(log, $"Using Redis for Distributed session, ConnectionString:{sessionService.ConnectionString}, InstanceName: {sessionService.InstanceName}");
318-
options.Configuration = sessionService.ConnectionString;
319-
options.InstanceName = sessionService.InstanceName;
320-
});
315+
GXLogging.Info(log, $"Using Redis for Distributed session, ConnectionString:{sessionService.ConnectionString}, InstanceName: {sessionService.InstanceName}");
316+
services.AddSingleton<IDistributedCache>(sp =>
317+
new CustomRedisSessionStore(sessionService.ConnectionString, TimeSpan.FromMinutes(Preferences.SessionTimeout), sessionService.InstanceName));
321318
services.AddDataProtection().PersistKeysToStackExchangeRedis(ConnectionMultiplexer.Connect(sessionService.ConnectionString), DATA_PROTECTION_KEYS).SetApplicationName(sessionService.InstanceName);
322319
}
323320
else if (sessionService is GxDatabaseSession)

0 commit comments

Comments
 (0)