views:

136

answers:

2

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: 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.

A: 

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!

Phill Sacre
You're right the javadoc for net.sf.ehcache.Cache says it is thread safe. Unfortunately I am removing the cache item before calling a put. I should just only call Cache.put(..).
Jacques René Mesrine
+1  A: 

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);
Aaron Digulla