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

resolves #13
  • Loading branch information
davidAtInleague committed Apr 9, 2023
1 parent 56f7f98 commit 2a94799
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
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 @@ -334,7 +334,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 2a94799

Please sign in to comment.