6

Assume I want to cache the results of expensive method calls. These cache entries should have a different expiry duration (aka TTL). Is this possible with JCache if the entries are put into the same Cache instance?

I am aware that I can assign a different ExpiryPolicy to each cache. However I wonder if it's possible to assign a expiry duration individually to a CacheEntry.

Does JCache support this? If so I'd appreciate a link to a code example for this. The scope of this question is purely JCache without any implementation specific features.

Stefan Armbruster
  • 39,465
  • 6
  • 87
  • 97

1 Answers1

5

Variable expiry per entry is not supported in JCache.

Background: It was initially planned that the methods for the ExpiryPolicy have the entry as parameter. During the finalization of the standard this was removed, since there were concerns about additional overhead especially for distributed caches, which might need to transfer the entry data over the network before the expiry policy can be called.

cruftex
  • 5,545
  • 2
  • 20
  • 36
  • What if one creates a CacheEntryExpiredListener instance that would put back any "too early" removed entry based on custom logic ? – Stephan Nov 18 '19 at 14:05
  • @Stephan: A synchronous listener would deadlock or throw an exception, since synchronous means that the entry is blocked for other operations. An asynchronous listener could work, but the entry will disappear shortly and appear again. You also need to take care of races, since if the entry is updated by other means, you need to make sure that you are not overwriting a newer value. Second, the variable expiry would be always multiples of the configures fixed values. – cruftex Nov 20 '19 at 16:32