Skip to content

Commit 57858ab

Browse files
claudiamurialdoBeta Bot
authored andcommitted
Cherry pick branch 'genexuslabs:feat/hybrid-redis-cache' into beta
1 parent 95c3ff7 commit 57858ab

File tree

1 file changed

+26
-5
lines changed
  • dotnet/src/dotnetframework/Providers/Cache/GxRedis

1 file changed

+26
-5
lines changed

dotnet/src/dotnetframework/Providers/Cache/GxRedis/GxRedis.cs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using GeneXus.Utils;
1313
using StackExchange.Redis;
1414
using StackExchange.Redis.KeyspaceIsolation;
15+
using GeneXus.Configuration;
1516

1617
namespace GeneXus.Cache
1718
{
@@ -24,9 +25,11 @@ public sealed class Redis : ICacheService2
2425
#if NETCORE
2526
bool _multitenant;
2627
MemoryCache _localCache;
27-
private const double DEFAULT_LOCAL_CACHE_FACTOR = 0.8;
28-
private static readonly TimeSpan LOCAL_CACHE_PERSISTENT_KEY_TTL = TimeSpan.FromMinutes(5);
29-
28+
private const double DEFAULT_LOCAL_CACHE_FACTOR = 0.2;
29+
private TimeSpan MAX_LOCAL_CACHE_TTL;
30+
private long MAX_LOCAL_CACHE_TTL_TICKS;
31+
private const int MAX_LOCAL_CACHE_TTL_DEFAULT_MIMUTES = 5;
32+
3033
#endif
3134
ConfigurationOptions _redisConnectionOptions;
3235
private const int REDIS_DEFAULT_PORT = 6379;
@@ -90,6 +93,18 @@ private void InitLocalCache(GXService providerService)
9093
{
9194
GXLogging.Debug(log, "Using Redis Hybrid mode with local memory cache.");
9295
_localCache = new MemoryCache(new MemoryCacheOptions());
96+
if (Config.GetValueOrEnvironmentVarOf("MAX_LOCAL_CACHE_TTL", out string maxCacheTtlMinutesStr) && long.TryParse(maxCacheTtlMinutesStr, out long maxCacheTtlMinutes))
97+
{
98+
MAX_LOCAL_CACHE_TTL = TimeSpan.FromMinutes(maxCacheTtlMinutes);
99+
GXLogging.Debug(log, $"MAX_LOCAL_CACHE_TTL read from config: {MAX_LOCAL_CACHE_TTL}");
100+
}
101+
else
102+
{
103+
MAX_LOCAL_CACHE_TTL = TimeSpan.FromMinutes(MAX_LOCAL_CACHE_TTL_DEFAULT_MIMUTES);
104+
GXLogging.Debug(log, $"MAX_LOCAL_CACHE_TTL using default value: {MAX_LOCAL_CACHE_TTL}");
105+
}
106+
107+
MAX_LOCAL_CACHE_TTL_TICKS = MAX_LOCAL_CACHE_TTL.Ticks;
93108
}
94109
else
95110
{
@@ -419,7 +434,13 @@ private TimeSpan LocalCacheTTL(int durationMinutes)
419434
}
420435
private TimeSpan LocalCacheTTL(TimeSpan? ttl)
421436
{
422-
return ttl.HasValue ? TimeSpan.FromTicks((long)(ttl.Value.Ticks * DEFAULT_LOCAL_CACHE_FACTOR)) : LOCAL_CACHE_PERSISTENT_KEY_TTL;
437+
if (ttl.HasValue)
438+
{
439+
double ttlTicks = ttl.Value.Ticks * DEFAULT_LOCAL_CACHE_FACTOR;
440+
if (ttlTicks < MAX_LOCAL_CACHE_TTL_TICKS)
441+
return ttl.Value;
442+
}
443+
return MAX_LOCAL_CACHE_TTL;
423444
}
424445
#endif
425446
private void ClearKeyLocal(string key)
@@ -463,7 +484,7 @@ private void SetLocal<T>(string key, T value)
463484
private void SetPersistentLocal(string cacheid, long? prefix)
464485
{
465486
#if NETCORE
466-
_localCache?.Set(cacheid, prefix, LocalCacheTTL(LOCAL_CACHE_PERSISTENT_KEY_TTL));
487+
_localCache?.Set(cacheid, prefix, LocalCacheTTL(MAX_LOCAL_CACHE_TTL));
467488
#endif
468489
}
469490
private void SetLocal<T>(string key, T value, int duration)

0 commit comments

Comments
 (0)