-
转载请注明出处哈:http://carlosfu.iteye.com/blog/2237511
- 一、Ehcache三个重要的类
1. CacheManager: 管理Cache
2. Cache: 管理K-V缓存对象
3. Element: K-V缓存对象
下面这张图形象表现三者的关系:
二、Ehcache常用API
1. 创建CacheManager有多种方法:
CacheManager.newInstance(Configuration configuration) – Create a new CacheManager or return the existing one named in the configuration.(非单例) CacheManager.create() – Create a new singleton CacheManager with default configuration, or return the existing singleton. This is the same as CacheManager.getInstance().(单例) CacheManager.create(Configuration configuration) – Create a singleton CacheManager with the passed-in configuration, or return the existing singleton.(单例) new CacheManager(Configuration configuration) – Create a new CacheManager, or throw an exception if the CacheManager named in the configuration already exists or if the parameter (configuration) is null.(非单例)
实际应用建议使用第三种:CacheManager.create(Configuration configuration)
CacheManager cacheManager = CacheManager.create(BaseTest.class.getClassLoader().getResourceAsStream("ehcache.xml")); //加入jmx MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer(); ManagementService.registerMBeans(cacheManager, mBeanServer, true, true, true, true); Cache cache = cacheManager.getCache("yourCacheName");
2. Cache常用API
(1) 添加和更新key-value
//add cache.put(new Element("key1", "value1")); //This updates the entry for "key1" cache.put(new Element("key1", "value2"));
(2) 获取key-value:
The following gets a Serializable value from an element with a key of key1. Cache cache = manager.getCache("sampleCache1"); Element element = cache.get("key1"); Serializable value = element.getValue(); The following gets a NonSerializable value from an element with a key of key1. Cache cache = manager.getCache("sampleCache1"); Element element = cache.get("key1"); Object value = element.getObjectValue();
(3) 删除key-value:
cache.remove("key1");
(4)获取cache大小:
The following gets the number of elements currently in the cache. int elementsInMemory = cache.getSize(); The following gets the number of elements currently in the MemoryStore. long elementsInMemory = cache.getMemoryStoreSize(); The following gets the number of elements currently in the DiskStore. long elementsOnDisk = cache.getDiskStoreSize();
(5)获取内存使用情况(生产环境不用使用,影响性能):
cache.calculateInMemorySize()
(6) 判断key是否过期:
boolean isExpired = cache.isExpired(Element element)
(7) 获取统计信息:
StatisticsGateway statisticsGateway = ehcache.getStatistics();
(8) 批量添加:
void putAll(Collection<Element> elements)
(9) 批量获取:
Map<Object,Element> getAll(Collection<?> keys)
(10) 判断key的位置:
boolean isElementInMemory(Object key) boolean isElementOnDisk(Object key)
相关推荐
赠送原API文档:ehcache-3.9.9-javadoc.jar; 赠送源代码:ehcache-3.9.9-sources.jar; 赠送Maven依赖信息文件:ehcache-3.9.9.pom; 包含翻译后的API文档:ehcache-3.9.9-javadoc-API文档-中文(简体)-英语-对照版....
Ehcache 2.10.8 .tar.gz(bigmemory-max-4.3.8.4.2.tar.gz)
Ehcache 2.10.8(bigmemory-max-4.3.8.4.2.tar.gz)
hibernate-ehcache-4.1.12.Final.jar hibernate-entitymanager-4.1.12.Final.jar hibernate-jpa-2.0-api-1.0.1.Final.jar hibernate-search-4.2.0.Final.jar hibernate-search-analyzers-4.2.0.Final.jar hibernate-...
该包是hibernate中所用的一个包,专门用来去处理特定的问题,它是和ehcache一起用的一个包
Mybatis-ehcache 1.2.1源码(ehcache-cache-mybatis-ehcache-1.2.1.tar.gz)
ehcache缓存jar(ehcache-core-2.4.6.jar+ehcache-web-2.0.4.jar)
赠送原API文档:ehcache-3.3.1-javadoc.jar; 赠送源代码:ehcache-3.3.1-sources.jar; 赠送Maven依赖信息文件:ehcache-3.3.1.pom; 包含翻译后的API文档:ehcache-3.3.1-javadoc-API文档-中文(简体)-英语-对照版....
ehcache-1.6.2-distribution.tar.gz ehcache-2.7.0-distribution.tar.gz 官网下载,放在此处作甲板
ehcache-2.8.0-distribution.tar.gz
mybatis-ehcache-1.0.2.jar META-INF/LICENSE META-INF/MANIFEST.MF META-INF/NOTICE META-INF/maven/org.mybatis.caches/mybatis-ehcache/pom.properties META-INF/maven/org.mybatis.caches/mybatis-ehcache/pom....
ehcache-2.7.3-distribution.tar.gz EHCache技术文档详解.doc
Mybatis-ehcache 1.2.1源码(ehcache-cache-mybatis-ehcache-1.2.1.zip)
赠送原API文档:ehcache-2.10.0-javadoc.jar 赠送源代码:ehcache-2.10.0-sources.jar 包含翻译后的API文档:ehcache-2.10.0-javadoc-API文档-中文(简体)-英语-对照版.zip 对应Maven信息:groupId:net.sf.ehcache...
Ehcache是一种广泛使用的Java分布式缓存器,具有快速、精干等特点,是Hibernate中默认CacheProvider。它提供内存存储和磁盘存储两种方案,因此无需担心容量问题。Ehcache可以单独使用,一般在第三方库中被用到的比较...
jar包,官方版本,自测可用
赠送原API文档:ehcache-core-2.6.11-javadoc.jar; 赠送源代码:ehcache-core-2.6.11-sources.jar; 赠送Maven依赖信息文件:ehcache-core-2.6.11.pom; 包含翻译后的API文档:ehcache-core-2.6.11-javadoc-API文档...
hibernate-ehcache-4.1.0.Final.jar 是hibernate4.1使用缓存的jar包
ehcache-core-2.5.2.jar ehcache-spring-annotations-1.2.0.jar guava-13.0.1.jar ehcache-terracotta-2.5.2.jar slf4j-api-1.6.1.jar slf4j-log4j12-1.6.1.jar terracotta-toolkit-1.5-runtime-4.2.0.jar
ehcache-core-2.6.10.jar专用包,欢迎下载 ehcache-core-2.6.10.jar ehcache-core-2.6.10.jar ehcache-core-2.6.10.jarehcache-core-2.6.10.jarehcache-core-2.6.10.jar