lymerin commented on code in PR #7745:
URL: https://github.com/apache/shenyu/pull/7745#discussion_r4303526446
##########
shenyu-plugin/shenyu-plugin-cache/shenyu-plugin-cache-memory/src/main/java/org/apache/shenyu/plugin/cache/memory/MemoryCache.java:
##########
@@ -17,26 +17,43 @@
package org.apache.shenyu.plugin.cache.memory;
-import com.google.common.cache.Cache;
-import com.google.common.cache.CacheBuilder;
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import com.github.benmanes.caffeine.cache.Expiry;
+import com.github.benmanes.caffeine.cache.Scheduler;
import org.apache.shenyu.plugin.cache.ICache;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;
-import java.util.Map;
import java.util.Objects;
-import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
/**
* MemoryCache.
*/
public final class MemoryCache implements ICache {
- private final Map<String, Cache<String, byte[]>> mainCache;
+ private final Cache<String, byte[]> mainCache;
public MemoryCache() {
- this.mainCache = new ConcurrentHashMap<>();
+ this.mainCache = Caffeine.newBuilder()
+ .scheduler(Scheduler.systemScheduler())
+ .expireAfter(new Expiry<String, byte[]>() {
+ @Override
+ public long expireAfterCreate(final String key, final
byte[] value, final long currentTime) {
+ return 0;
Review Comment:
Thanks for the review.
I made two small follow-ups:
- replaced `expireVariably().get()` with `orElseThrow(...)` so a missing
variable-expiration policy fails with a clearer error;
- added comments documenting that entries must be written through
`VarExpiration.put(...)` to receive their per-entry TTL.
I left the `estimatedSize()` assertion unchanged intentionally. The purpose
of that regression test is to verify that expired entries are cleaned up by the
scheduler without requiring another lookup of the expired keys. Using
`getIfPresent`, `isExist`, or explicit `cleanUp()` in the assertion path would
itself access the cache or trigger maintenance and weaken what the test is
trying to prove.
`estimatedSize()` is approximate, but combined with Awaitility and the
10-second observation window, it provides a non-intrusive way to observe the
scheduled cleanup.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]