Skip to content

Commit

Permalink
copy NearCacheEntry values as-if they had been materialized from cache
Browse files Browse the repository at this point in the history
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 lucee#13
  • Loading branch information
davidAtInleague committed Nov 30, 2023
1 parent 0f7d5c5 commit 410fad9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
19 changes: 19 additions & 0 deletions source/java/src/lucee/extension/io/cache/redis/NearCacheEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 410fad9

Please sign in to comment.