Skip to content

Commit b3f4348

Browse files
claudiamurialdoclaudiamurialdo
authored andcommitted
Add local memory cache to Redis implementation to reduce roundtrips (#1214)
* Add local memory cache to Redis implementation to reduce roundtrips * It seems better to keep GxRedis.cs unified for both .NET Framework and .NET Core. * Applied Fazzato’s review suggestions * Fix build error. * Read ENABLE_MEMORY_CACHE from provider settings and apply it only in the default constructor used by CacheAPI. * Refactor local cache settings in Redis implementation to support configurable maximum TTL and adjust local cache factor to 20%. --------- Co-authored-by: claudiamurialdo <[email protected]> (cherry picked from commit 6126f20) # Conflicts: # dotnet/src/dotnetframework/Providers/Cache/GxRedis/GxRedis.cs
1 parent dc7ef5e commit b3f4348

File tree

1 file changed

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

1 file changed

+28
-5
lines changed

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

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77
using GxClasses.Helpers;
88
using Microsoft.Extensions.Caching.Memory;
99
#endif
10+
using GeneXus.Encryption;
1011
using GeneXus.Services;
1112
using GeneXus.Utils;
1213
using StackExchange.Redis;
14+
using StackExchange.Redis.KeyspaceIsolation;
15+
using GeneXus.Configuration;
1316

1417
namespace GeneXus.Cache
1518
{
@@ -21,9 +24,11 @@ public sealed class Redis : ICacheService2
2124
IDatabase _redisDatabase;
2225
#if NETCORE
2326
MemoryCache _localCache;
24-
private const double DEFAULT_LOCAL_CACHE_FACTOR = 0.8;
25-
private static readonly TimeSpan LOCAL_CACHE_PERSISTENT_KEY_TTL = TimeSpan.FromMinutes(5);
26-
27+
private const double DEFAULT_LOCAL_CACHE_FACTOR = 0.2;
28+
private TimeSpan MAX_LOCAL_CACHE_TTL;
29+
private long MAX_LOCAL_CACHE_TTL_TICKS;
30+
private const int MAX_LOCAL_CACHE_TTL_DEFAULT_MIMUTES = 5;
31+
2732
#endif
2833
ConfigurationOptions _redisConnectionOptions;
2934
private const int REDIS_DEFAULT_PORT = 6379;
@@ -76,6 +81,18 @@ private void InitLocalCache(GXService providerService)
7681
{
7782
GXLogging.Debug(log, "Using Redis Hybrid mode with local memory cache.");
7883
_localCache = new MemoryCache(new MemoryCacheOptions());
84+
if (Config.GetValueOrEnvironmentVarOf("MAX_LOCAL_CACHE_TTL", out string maxCacheTtlMinutesStr) && long.TryParse(maxCacheTtlMinutesStr, out long maxCacheTtlMinutes))
85+
{
86+
MAX_LOCAL_CACHE_TTL = TimeSpan.FromMinutes(maxCacheTtlMinutes);
87+
GXLogging.Debug(log, $"MAX_LOCAL_CACHE_TTL read from config: {MAX_LOCAL_CACHE_TTL}");
88+
}
89+
else
90+
{
91+
MAX_LOCAL_CACHE_TTL = TimeSpan.FromMinutes(MAX_LOCAL_CACHE_TTL_DEFAULT_MIMUTES);
92+
GXLogging.Debug(log, $"MAX_LOCAL_CACHE_TTL using default value: {MAX_LOCAL_CACHE_TTL}");
93+
}
94+
95+
MAX_LOCAL_CACHE_TTL_TICKS = MAX_LOCAL_CACHE_TTL.Ticks;
7996
}
8097
else
8198
{
@@ -377,7 +394,13 @@ private TimeSpan LocalCacheTTL(int durationMinutes)
377394
}
378395
private TimeSpan LocalCacheTTL(TimeSpan? ttl)
379396
{
380-
return ttl.HasValue ? TimeSpan.FromTicks((long)(ttl.Value.Ticks * DEFAULT_LOCAL_CACHE_FACTOR)) : LOCAL_CACHE_PERSISTENT_KEY_TTL;
397+
if (ttl.HasValue)
398+
{
399+
double ttlTicks = ttl.Value.Ticks * DEFAULT_LOCAL_CACHE_FACTOR;
400+
if (ttlTicks < MAX_LOCAL_CACHE_TTL_TICKS)
401+
return ttl.Value;
402+
}
403+
return MAX_LOCAL_CACHE_TTL;
381404
}
382405
#endif
383406
private void ClearKeyLocal(string key)
@@ -421,7 +444,7 @@ private void SetLocal<T>(string key, T value)
421444
private void SetPersistentLocal(string cacheid, long? prefix)
422445
{
423446
#if NETCORE
424-
_localCache?.Set(cacheid, prefix, LocalCacheTTL(LOCAL_CACHE_PERSISTENT_KEY_TTL));
447+
_localCache?.Set(cacheid, prefix, LocalCacheTTL(MAX_LOCAL_CACHE_TTL));
425448
#endif
426449
}
427450
private void SetLocal<T>(string key, T value, int duration)

0 commit comments

Comments
 (0)