tags:

views:

2161

answers:

5

I'm looking for a simple Java in-memory cache that has good concurrency (so LinkedHashMap isn't good enough), and which can be serialized to disk periodically.

One feature I need, but which has proved hard to find, is a way to "peek" at an object. By this I mean retrieve an object from the cache without causing the cache to hold on to the object any longer than it otherwise would have.

Update: An additional requirement I neglected to mention is that I need to be able to modify the cached objects (they contain float arrays) in-place.

Can anyone provide any recommendations?

+1  A: 

How about this: http://jakarta.apache.org/jcs/

TofuBeer
+9  A: 

Ehcache is a pretty good solution for this and has a way to peek (getQuiet() is the method) such that it doesn't update the idle timestamp. Internally, Ehcache is implemented with a set of maps, kind of like ConcurrentHashMap, so it has similar kinds of concurrency benefits.

Alex Miller
Thanks, an additional question: If I retrieve an object from EHcache (say, an array), and modify it - will the object be updated in the cache? ie. is EHCache maintaining references to the objects?
sanity
I believe so but to do this safely you must appropriately lock the object, of course.
Alex Miller
A: 

Try Ehcache? It allows you to plug in your own caching expiry algorithms so you could control your peek functionality.

You can serialize to disk, database, across a cluster etc...

Stephen
+2  A: 

If you're needing something simple, would this fit the bill?

Map<K, V> myCache = Collections.synchronizedMap(new WeakHashMap<K, V>());

It wont save to disk, but you said you wanted simple...

Links:

Evan
A: 

You need NCache. It is a feature-rich distributed caching solution that gives you scalable performance even at peak load times.

It lets you expire the item using Absolute or Sliding expiration. Moreover you can use read-through, write-through caching, database sync and many more useful features.

james