Ehcache 特点
1. 快速.
2. 简单.
3. 多种缓存策略
4. 缓存数据有两级:内存和磁盘,因此无需担心容量问题
5. 缓存数据会在虚拟机重启的过程中写入磁盘
6. 可以通过RMI、可插入API等方式进行分布式缓存
7. 具有缓存和缓存管理器的侦听接口
8. 支持多缓存管理器实例,以及一个实例的多个缓存区域
9. 提供Hibernate的缓存实现
10. 等等
import net.sf.ehcache.Cache; import net.sf.ehcache.CacheManager; import net.sf.ehcache.Element; /** * ehcache的使用 * * @author Andy * */ public class EhcacheDemo { private final static String CACHED_NAME = "cached_name"; private static CacheManager cacheManager; private static Cache cache; static { cacheManager = CacheManager.create(); cache = new Cache(CACHED_NAME, 10000, false, false, 0, 0); cacheManager.addCache(cache); } // add public void addCached(String key, String value) { Element element = new Element(key, value); cache.put(element); } // remove public void removeCached(String key) { cache.remove(key); } // get public String getCached(String key) { Element element = cache.get(key); return (String) element.getObjectValue(); } // contant public synchronized boolean isKeyInCache(String key) { return cache.isKeyInCache(key); } // clear public void clear() { cache.removeAll(); } public static void main(String[] args) { EhcacheDemo demo = new EhcacheDemo(); System.out.println(demo.isKeyInCache("abc")); demo.addCached("abc", "123"); System.out.println(demo.isKeyInCache("abc")); System.out.println(demo.getCached("abc")); } }