raphw / guava-cache-overflow-extension

A Guava cache extension that allows caches to overflow to disk.
55 stars 18 forks source link

Cache expiration doesn't work. #1

Open SnoopInf opened 8 years ago

SnoopInf commented 8 years ago

Neither expireAfterWrite, nor expireAfterAccess work when using this extension. What really happens is that expired entry is going through removal process in Guava cache, but instead of being deleted it gets persisted on disk and loaded to cache like brand new item right there.

It's actually happening because of these lines:

protected boolean isPersistenceRelevant(RemovalCause removalCause) { return removalCause != RemovalCause.EXPLICIT && removalCause != RemovalCause.REPLACED; }

So RemovalCause.EXPIRED is treated as relevant cause for persistence.

To reproduce the issue:

  1. Build a cache with code like this:

Cache<String, String> c = FileSystemCacheBuilder.newBuilder() .maximumSize(100000) .expireAfterWrite(10000, TimeUnit.MILLISECONDS) .persistenceDirectory(new File(persistenceDirectory)) .recordStats() .build();

  1. Fill cache with some values. cache.put("apple", "fresh");
  2. wait 10+ sec.
  3. Get the entry you expect to expire. String res = cache.get("apple");

ExpRes - res is null as well as entry for apple is expired. AchRes - res is "fresh", because it was not removed from cache but stored on disk and then loaded back.

raphw commented 8 years ago

There is a good chance for this, I have not looked at the code base for a long time. If you create a PR, I am happy to merge it.