From 410fad97804c9f94f1f8e16ab38be190ad3ee999 Mon Sep 17 00:00:00 2001 From: David Rogers Date: Sun, 9 Apr 2023 07:29:02 -0500 Subject: [PATCH] copy NearCacheEntry values as-if they had been materialized from cache With goal being not sharing live object refs to the underlying "to-be-cached" objects across NearCacheEntry objects fixes test introduced in prior commit resolves #13 --- .../io/cache/redis/NearCacheEntry.java | 19 +++++++++++++++++++ .../extension/io/cache/redis/RedisCache.java | 9 +++++++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/source/java/src/lucee/extension/io/cache/redis/NearCacheEntry.java b/source/java/src/lucee/extension/io/cache/redis/NearCacheEntry.java index c1066c0..40e5009 100644 --- a/source/java/src/lucee/extension/io/cache/redis/NearCacheEntry.java +++ b/source/java/src/lucee/extension/io/cache/redis/NearCacheEntry.java @@ -25,6 +25,25 @@ public NearCacheEntry(byte[] key, Object val, int exp, long count) { this.count = count; } + private NearCacheEntry(byte[] key, Object val, int exp, long count, byte[] serialized) { + this.key = key; + this.val = val; + this.exp = exp; + this.created = System.currentTimeMillis(); + this.count = count; + this.serialized = serialized; + } + + /** + * copy this object, also copying the underlying object as-if it had been materialized from cache (in particular, the underlying object + * is copied such that is no longer a reference to the original underlying object). Note that the serialized byte[] is shared across instances of + * copied NearCacheEntries. + */ + public NearCacheEntry copy(ClassLoader cl) throws IOException { + byte[] bytes = serialized(); + return new NearCacheEntry(key, Coder.evaluate(cl, bytes), exp, count, bytes); + } + @Override public Date created() { return new Date(created); diff --git a/source/java/src/lucee/extension/io/cache/redis/RedisCache.java b/source/java/src/lucee/extension/io/cache/redis/RedisCache.java index 7dcb7f2..2e6b5b9 100644 --- a/source/java/src/lucee/extension/io/cache/redis/RedisCache.java +++ b/source/java/src/lucee/extension/io/cache/redis/RedisCache.java @@ -251,7 +251,7 @@ public CacheEntry getCacheEntry(String skey) throws IOException { if (async) { NearCacheEntry val = storage.get(bkey); if (val != null) { - return val; + return val.copy(cl); } storage.doJoin(cnt, true); } @@ -345,7 +345,12 @@ public CacheEntry getCacheEntry(String skey, CacheEntry defaultValue) { if (async) { NearCacheEntry val = storage.get(bkey); if (val != null) { - return val; + try { + return val.copy(cl); + } + catch (IOException e) { + return defaultValue; + } } storage.doJoin(cnt, true); }