`
carlosfu
  • 浏览: 576838 次
  • 性别: Icon_minigender_1
  • 来自: 北京
博客专栏
Ba8b5055-9c58-3ab0-8a1c-e710f0495d2c
BigMemory实战与理...
浏览量:30533
53b2087e-c637-34d2-b61d-257846f73ade
RedisCluster开...
浏览量:150214
C9f66038-7478-3388-8086-d20c1f535495
缓存的使用与设计
浏览量:123525
社区版块
存档分类
最新评论

BigMemory系列文章--4.Ehcache重要类和常用API

阅读更多

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)

 

  • 大小: 82 KB
分享到:
评论

相关推荐

    ehcache-3.9.9-API文档-中英对照版.zip

    赠送原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(bigmemory-max-4.3.8.4.2.tar.gz)

    Ehcache 2.10.8是该产品的特定版本,它包含了针对缓存管理和优化的一系列特性和改进。在bigmemory-max-4.3.8.4.2.tar.gz这个压缩包中,我们可以找到与Ehcache 2.10.8相关的所有组件和配置文件。 1. Ehcache的核心...

    Ehcache 2.10.8 .tar.gz(bigmemory-max-4.3.8.4.2.tar.gz)

    Ehcache 2.x系列是其历史上的一个稳定版本,支持多种缓存策略,包括LRU(Least Recently Used)和LFU(Least Frequently Used)等,用于自动清理不常访问的数据。此外,Ehcache支持分布式缓存,可以在多台服务器之间...

    hibernate jar包:hibernate-commons-annotations-4.0.1.Final.jar等

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

    ehcache-core-2.6.11-API文档-中英对照版.zip

    赠送原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文档...

    cache-api-1.0.0.jar

    “cache-api-1.0.0.jar”作为实现JSR107规范的库,提供了对缓存操作的一系列接口和类。这些接口和类允许开发者方便地在应用中集成缓存功能,如存储和检索数据、设置过期策略、处理并发访问等。通过使用这个API,...

    hibernate-ehcache-4.3.9.Final.jar

    该包是hibernate中所用的一个包,专门用来去处理特定的问题,它是和ehcache一起用的一个包

    slf4j-api-1.7.36.zip

    Ehcache是一种广泛使用的Java分布式缓存器,具有快速、精干等特点,是Hibernate中默认CacheProvider。它提供内存存储和磁盘存储两种方案,因此无需担心容量问题。Ehcache可以单独使用,一般在第三方库中被用到的比较...

    ehcache缓存jar(ehcache-core-2.4.6.jar+ehcache-web-2.0.4.jar)

    ehcache缓存jar(ehcache-core-2.4.6.jar+ehcache-web-2.0.4.jar)

    ehcache-3.3.1-API文档-中英对照版.zip

    赠送原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.rar ehcache-core-2.6.10.jar依赖包

    ehcache-core-2.6.10.jar依赖包 MyBatiesEhCache二级缓存 Ehcache是一种广泛使用的开源Java分布式缓存。主要面向通用缓存,Java EE和轻量级容器。它具有内存和磁盘存储,缓存加载器,缓存扩展,缓存异常处理程序,一个...

    ehcache-core-2.4.5.jar

    "ehcache-core-2.4.5.jar"是Ehcache的核心库,该版本为2.4.5,包含Ehcache的基本功能和API。在使用这个版本的Ehcache时,需要额外引入两个SLF4J(Simple Logging Facade for Java)的依赖,分别是"slf4j-api-1.6.1....

    mybatis-ehcache-1.0.2.jar

    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.10.0-API文档-中英对照版.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-2.7.3-distribution.tar.gz

    标题"ehcache-2.7.3-distribution.tar.gz"表明这是一个包含EHCache 2.7.3版本的发行版压缩包,格式为tar.gz,这是一种常见的Linux/Unix系统中用于打包和压缩文件的格式。这个版本的EHCache是Java缓存系统的一个版本...

    ehcache-core-2.5.0.jar

    jar包,官方版本,自测可用

    hibernate-ehcache-4.1.0.Final.jar

    hibernate-ehcache-4.1.0.Final.jar 是hibernate4.1使用缓存的jar包

    ehcache-1.6.2-distribution.tar.gz

    在这些分布包中,你可以找到相关的配置示例和API文档,帮助开发者理解如何进行集成和配置。 总的来说,Ehcache作为一款高效的缓存解决方案,通过提供内存管理和磁盘存储,减少了对数据库的访问,提升了系统性能。...

    ehcache所需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.jar ehcache-core-2.6.10.jarehcache-core-2.6.10.jarehcache-core-2.6.10.jar

Global site tag (gtag.js) - Google Analytics