How to use item versioning in an EHCache instance?

I cache objects that are sent to my component asynchronously. In other words, the order in which these objects arrive is unpredictable. To avoid any problems, I included the version attribute in my objects (basically this is a timestamp). The idea is that any object that comes with a version that is older than the one that has already been cached can be discarded.

The Element class of the EHCache class (which wraps objects in EHCache) seems to make this easier: in addition to the key and value, the constructor can take a (long) version. I cannot do this work as I expected it to work. The following code snippet demonstrates my problem (using EHCache 2.1.1):

public static void main(String[] args) { final CacheManager manager = CacheManager.create(); final Cache testCache = new Cache(new CacheConfiguration("test", 40)); manager.addCache(testCache); final String key = "key"; final Element elNew = new Element(key, "NEW", 2L); testCache.put(elNew); final Element elOld = new Element(key, "OLD", 1L); testCache.put(elOld); System.out.println("Cache content:"); for (Object k : testCache.getKeys()) { System.out.println(testCache.get(k)); } } 

I expect the code above will cause the cached value to be "NEW", instead printed "OLD". If you play around a bit with the order in which the elements are inserted, you will find that the last one that was inserted is the one that remains in the cache. Versions seem to be ignored.

Am I using the version control function correctly, or maybe not designed for this purpose? Can anyone recommend alternatives?

+4
source share
1 answer

EhCache, apparently, ignores the value of the version field - its value is user-defined. Therefore, EhCache overwrites your version of 2L with version 1L , not knowing what version numbers mean.

See 1) http://jira.terracotta.org/jira/browse/EHC-765

it was decided that providing an internal version control scheme caused unnecessary overhead for all users. Instead, we now leave the version value intact, so that it is completely under the control of the user.

And 2) http://jira.terracotta.org/jira/browse/EHC-666

[...] I would prefer the solution proposed by Marek that we provide the user with full control over the version attribute and does not mutate it at all internally. This prevents any performance impact for most users, and allows the user to use it as they see fit. [...]

By agreement with Greg via email, I fixed it as my last comment.


I believe that using the version field can lead to race conditions, as a result of which one thread overwrites the updated version of the cache element with some older version. Therefore, in my application, I have a counter that keeps track of the latest version of the database, and when I load the cached value with the version field different from the value of the latest version of the database, I know that the cached value may be outdated and ignore it.

+2
source

Source: https://habr.com/ru/post/1314313/


All Articles