From a07f42bc287c76663cd666dfea3ce6bb9337d3a3 Mon Sep 17 00:00:00 2001 From: "OpenGenesis (MPL 2.0)" <153564363+opengenesislink@users.noreply.github.com> Date: Fri, 16 Oct 2026 16:55:48 +0200 Subject: [PATCH] feat(assetservice): opt-in Redis revision invalidation with cache bypass safety gate --- addons/OGLAssetService/OGLAssetConnector.cs | 29 +++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/addons/OGLAssetService/OGLAssetConnector.cs b/addons/OGLAssetService/OGLAssetConnector.cs index 6ea57378..d430f2a4 100644 --- a/addons/OGLAssetService/OGLAssetConnector.cs +++ b/addons/OGLAssetService/OGLAssetConnector.cs @@ -16,6 +16,10 @@ namespace OpenGenesisLINK.Addons.AssetService private readonly OGLObjectCache cache; private readonly OGLDiskCache diskCache; private readonly object cacheSync = new object(); + private readonly OGLRedisAssetCoordinator redis; + private readonly System.Collections.Generic.Dictionary cachedRevisions = + new System.Collections.Generic.Dictionary(StringComparer.Ordinal); + private readonly bool redisEnabled; public long MemoryCacheHits => cache.Hits; public long MemoryCacheMisses => cache.Misses; public long MemoryCacheUsedBytes => cache.UsedBytes; @@ -44,6 +48,15 @@ namespace OpenGenesisLINK.Addons.AssetService (long)diskMiB * 1024 * 1024); } + redisEnabled = settings.GetBoolean("RedisEnabled", false); + if (redisEnabled) + { + string redisConnection = Environment.GetEnvironmentVariable("OGL_ASSET_REDIS_CONNECTION"); + if (String.IsNullOrWhiteSpace(redisConnection)) + throw new InvalidOperationException("OGL_ASSET_REDIS_CONNECTION must be set when RedisEnabled=true."); + redis = new OGLRedisAssetCoordinator(redisConnection, settings.GetInt("RedisDatabase", 2)); + } + string mode = settings.GetString("Mode", "compatibility"); if (!String.Equals(mode, "compatibility", StringComparison.OrdinalIgnoreCase)) throw new NotSupportedException( @@ -55,6 +68,10 @@ namespace OpenGenesisLINK.Addons.AssetService if (String.IsNullOrWhiteSpace(id)) return base.GetData(id); lock (cacheSync) { + // Redis connectivity or revision failures must never return stale cached data. + // In this experimental mode all reads bypass local caches until a + // distributed writer protocol has been verified end-to-end. + if (redisEnabled) return base.GetData(id); if (cache.TryGet(id, out byte[] result)) return result; if (diskCache != null) { @@ -89,7 +106,10 @@ namespace OpenGenesisLINK.Addons.AssetService try { diskCache?.Invalidate(id); } catch (System.IO.IOException) { } catch (UnauthorizedAccessException) { } - return base.UpdateContent(id, data); + bool updated = base.UpdateContent(id, data); + if (updated && redisEnabled && !String.IsNullOrWhiteSpace(id)) + redis.InvalidateAsync(id).GetAwaiter().GetResult(); + return updated; } } @@ -101,7 +121,10 @@ namespace OpenGenesisLINK.Addons.AssetService try { diskCache?.Invalidate(id); } catch (System.IO.IOException) { } catch (UnauthorizedAccessException) { } - return base.Delete(id); + bool deleted = base.Delete(id); + if (deleted && redisEnabled && !String.IsNullOrWhiteSpace(id)) + redis.InvalidateAsync(id).GetAwaiter().GetResult(); + return deleted; } } @@ -117,6 +140,8 @@ namespace OpenGenesisLINK.Addons.AssetService catch (System.IO.IOException) { } catch (UnauthorizedAccessException) { } } + if (redisEnabled && !String.IsNullOrWhiteSpace(id)) + redis.InvalidateAsync(id).GetAwaiter().GetResult(); return id; } }