2-level cache in java
How can I organize a 2 level cache in Java?
- 1st level - memory
- 2nd level - filesystem
Where I can find information about 2 level cache implementation and cache strategies configuration or can you show me any methods that can solve this problem.
I suggest using imcache and load necessary items from disk using cache loader as follows,
Cache<String,Object> yourCache = CacheBuilder.
concurrentHeapCache().cacheLoader(new CacheLoader<String, Object>() {
public Object load(String key) {
//code to load item from file.
}
}).build();
It looks like using option 2 with overwrite to disk option will work for OP. These 2 urls can help: http://memcached.org/ and http://ehcache.org/
Not clear what your question is, if you are talking about hibernate 2nd level caching, see here http://www.javalobby.org/java/forums/t48846.html
If you just want to know how to cache in java, then you have a few options 1.) Store the data in a HashMap 2.) Use Memcached 3.) Store it in a file 4.) Store it in a database
Each have their trade-offs, we would need to know more about your problem to advise.
I have used OS Cache - http://www.opensymphony.com/oscache/wiki/Feature%20List.html And found it one of the best ways to implement 2 level caching.
It works like a hashmap with key value pairs.
It transparently stores the most used data in memory and then swaps the rest to the file system(you can change the algorithm)
It scales up really well. Even for a large number of objects it does not show any performance degradation
It supports clustering as well
It is highly configurable
It is FREE and Open Source!
I've build something that can be useful for your needs.
https://github.com/enryold/onion-cache
精彩评论