开发者

atomic writes to ehcache

Context

I am storing a java.util.List inside ehcache.

Key(String) --> List<UserDetail>

The ordered List contains a Top 10 ranking of my most active users.

Problem

Concurrent 3rd party clients might be requesting for this list. I have a requirement to be as current as possible with regards to the ranking. Thus if the ranking is changed due the activities of users, the ordered List in the cache must not be left stale for very long. Once I've recalculated a new List, I want to replace the one in cache immediately.

Consider a busy scenario whereby multiple concurrent clients are requesting for the ranking; how can I replace the cache item in an fashion such that:开发者_StackOverflow Clients can continue to pull a possibly stale snapshot. They should never get a null value.

There will only be 1 server thread that writes to the cache.


I don't see what the problem is. Once you've replaced a cache item, clients will pull that new cache item. Up until that point they will pull the old cache item.

There should never be a time when they return a null cache item, unless you actually remove the item from the cache and then replace it.

If EHCache worked like that I would consider it pretty fundamentally broken, given that it's meant to be thread-safe!


You can simply store the new list in the cache. The next call to get will return it.

All you must make sure is that no one edits the list that is returned from the cache. For example in the server thread, you must copy the list:

List workingCopy = new ArrayList ((List)cache.get(key));
... modify list ...
cache.put (key, workingCopy);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜