一. 创建CacheManager
所有ehcache的使用, 都是从 CacheManager. 开始的,有多种方法创建CacheManager实例:
1:Create a singleton CacheManager using defaults, then list caches:
String[] cacheNames = CacheManager.create().getCacheNames();
或者
String[] cacheNames = CacheManager.getInstance().getCacheNames();
查看源代码:
public static CacheManager getInstance() throws CacheException {
return CacheManager.create();
}
2:Create a CacheManager instance using defaults, then list caches.
CacheManager manager = new CacheManager();
String[] cacheNames = manager.getCacheNames();
3:Create two CacheManagers, each with a different configuration, and list the caches in each.
CacheManager manager1 = new CacheManager("src/config/ehcache1.xml");
CacheManager manager2 = new CacheManager("src/config/ehcache2.xml");
String[] cacheNamesForManager1 = manager1.getCacheNames();
String[] cacheNamesForManager2 = manager2.getCacheNames();
二:Ways of loading Cache Configuration
1:Create a CacheManager using defaults. Ehcache will look for ehcache.xml in the classpath.
CacheManager manager = new CacheManager();
2:Create a CacheManager specifying the path of a configuration file.
CacheManager manager = new CacheManager("src/config/ehcache.xml");
3:Create a CacheManager from a configuration resource in the classpath.
URL url = getClass().getResource("/anotherconfigurationname.xml");
CacheManager manager = new CacheManager(url);
4:Create a CacheManager from a configuration in an InputStream.
InputStream fis = new FileInputStream(new File("src/config/ehcache.xml").getAbsolutePath());
try {
CacheManager manager = new CacheManager(fis);
} finally {
fis.close();
}
三. Adding and Removing Caches Programmatically
手动创建一个cache, 而不是通过配置文件
1:Add a cache using defaults, then use it. The following example creates a cache called testCache, which will be configured using defaultCache from the configuration.
CacheManager singletonManager = CacheManager.create();
singletonManager.addCache("testCache");
Cache test = singletonManager.getCache("testCache");
2:Create a Cache and add it to the CacheManager, then use it.
CacheManager singletonManager = CacheManager.create();
Cache memoryOnlyCache = new Cache("testCache", 5000, false, false, 5, 2);
manager.addCache(memoryOnlyCache);
Cache test = singletonManager.getCache("testCache");
3:Remove cache called sampleCache1
CacheManager singletonManager = CacheManager.create();
singletonManager.removeCache("sampleCache1");
四. Shutdown the CacheManager
1:Shutdown the singleton CacheManager
CacheManager.getInstance().shutdown();
2:Shutdown a CacheManager instance, assuming you have a reference to the CacheManager called manager
manager.shutdown();
五. Using Caches
1:Obtaining a reference to a Cache
Obtain a Cache called "sampleCache1", which has been preconfigured in the configuration file
Cache cache = manager.getCache("sampleCache1");
2:Performing CRUD operations
Put an element into a cache
Cache cache = manager.getCache("sampleCache1");
Element element = new Element("key1", "value1");
cache.put(element);
3:Update an element in a cache. Even though cache.put() is used, Ehcache knows there is an existing element, and considers the put an update for the purpose of notifying cache listeners.
Cache cache = manager.getCache("sampleCache1");
cache.put(new Element("key1", "value1"));
//This updates the entry for "key1"
cache.put(new Element("key1", "value2"));
4:Get a Serializable value from an element in a cache with a key of "key1".
Cache cache = manager.getCache("sampleCache1");
Element element = cache.get("key1");
Serializable value = element.getValue();
5:Get a NonSerializable value from an element in a cache with a key of "key1".
Cache cache = manager.getCache("sampleCache1");
Element element = cache.get("key1");
Object value = element.getObjectValue();
6:Remove an element from a cache with a key of "key1".
Cache cache = manager.getCache("sampleCache1");
cache.remove("key1");
六. Disk Persistence on demand
1:sampleCache1 has a persistent diskStore. We wish to ensure that the data and index are written immediately.
Cache cache = manager.getCache("sampleCache1");
cache.flush();
七. Obtaining Cache Sizes
1:Get the number of elements currently in the Cache.
Cache cache = manager.getCache("sampleCache1");
int elementsInMemory = cache.getSize();
2:Get the number of elements currently in the MemoryStore.
Cache cache = manager.getCache("sampleCache1");
long elementsInMemory = cache.getMemoryStoreSize();
3:Get the number of elements currently in the DiskStore.
Cache cache = manager.getCache("sampleCache1");
long elementsInMemory = cache.getDiskStoreSize();
八. Obtaining Statistics of Cache Hits and Misses
1:Get the number of times requested items were found in the cache. i.e. cache hits
Cache cache = manager.getCache("sampleCache1");
int hits = cache.getHitCount();
2:Get the number of times requested items were found in the MemoryStore of the cache.
Cache cache = manager.getCache("sampleCache1");
int hits = cache.getMemoryStoreHitCount();
3:Get the number of times requested items were found in the DiskStore of the cache.
Cache cache = manager.getCache("sampleCache1");
int hits = cache.getDiskStoreCount();
4:Get the number of times requested items were not found in the cache. i.e. cache misses.
Cache cache = manager.getCache("sampleCache1");
int hits = cache.getMissCountNotFound();
5:Get the number of times requested items were not found in the cache due to expiry of the elements.
Cache cache = manager.getCache("sampleCache1");
int hits = cache.getMissCountExpired();
分享到:
相关推荐
ehcache3-samples, 关于使用 Ehcache 3,一些示例/教程 Ehcache示例这里知识库包含有关 Ehcache 3用法的示例和教程。示例'basic'演示 Ehcache 3的基本配置和用法'集群'- 演示如何在Terracotta服务器上使用分布式缓存...
1.解压缩到目录下,复制ehcache-monitor-kit-1.0.0\lib\ehcache-probe-1.0.0.jar包到application的web-inf/lib目录下 2.将以下配置copy的ehcache.xml文件的ehcache标签中,注:上述链接中说的配置少写了个probe包名...
【标题解析】:“ehcache.xsd_ehcache.xml代码提示.rar”这个标题表明这是一个与Ehcache缓存系统相关的资源包,主要目的是为Ehcache的配置文件ehcache.xml提供代码提示功能。Ehcache是一个广泛使用的开源Java缓存...
Ehcache是一个开源的、高性能的缓存解决方案,广泛应用于Java应用程序中,以提高数据访问的速度和效率。本文将深入探讨Ehcache的简单监控,帮助开发者更好地理解其工作原理和性能状态。 首先,了解Ehcache的核心...
赠送jar包:ehcache-3.3.1.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-...
Ehcache是一个广泛使用的开源Java缓存库,它为应用程序提供了高效的内存管理和数据缓存功能。Ehcache的核心目标是提高应用性能,通过将频繁访问的数据存储在内存中,减少对数据库的依赖,从而降低系统负载。这次我们...
**Ehcache 知识详解** Ehcache 是一个开源的、高性能的缓存解决方案,广泛应用于Java应用程序中,尤其在提升系统性能和减少数据库负载方面表现突出。它支持内存和磁盘存储,并且可以与Java持久层框架如Hibernate、...
Ehcache是一个高性能的、基于Java的进程内缓存解决方案,它被广泛应用于各种Java应用程序,包括Java EE和轻量级容器。Ehcache的主要优势在于它的快速响应、易用性和丰富的缓存策略。它提供了两种级别的缓存存储:...
【标题】:“Hibernate + Ehcache 整合使用详解” 【描述】:“Hibernate 是一款流行的 Java 持久层框架,而 Ehcache 是一个高效的分布式内存缓存系统。将两者结合,能够极大地提升应用的性能,减少数据库的负载,...
本篇文章将详细探讨MyBatis与Ehcache的集成以及`ehcache.xsd`和`ehcache.xml`这两个配置文件在其中的作用。 首先,Ehcache是一个开源的、高性能的Java缓存库,它能够极大地减少对数据库的访问,提高应用程序的响应...
赠送jar包:ehcache-3.9.9.jar; 赠送原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-...
Ehcache是一个开源的Java缓存库,广泛用于提高应用程序的性能和响应速度,通过存储经常访问的数据在内存中,避免了频繁的数据库查询。它最初由Tomi Triebel开发,现在是Terracotta公司的产品。在版本2.6.5中,...
**Ehcache** 是一个广泛使用的Java缓存库,它为应用程序提供了本地内存缓存功能,以提高数据访问速度和减少数据库负载。Ehcache在处理高并发和大数据量的场景下表现出色,尤其适用于那些频繁读取但不经常修改的数据...
EHcache是一款广泛使用的开源Java分布式缓存系统,主要设计用于提高应用程序的性能和可伸缩性。在Java应用程序中,特别是那些基于Hibernate或MyBatis的持久层框架中,EHcache作为二级缓存工具,能够显著提升数据访问...
Ehcache是一款高性能、易用且广泛应用于Java环境中的分布式缓存系统,它极大地提高了应用程序的性能和响应速度。在Spring框架中集成Ehcache,能够实现数据的快速访问,减轻数据库的压力,优化整体系统架构。本文将...
Ehcache 3 是一个广泛使用的开源Java缓存解决方案,特别是在需要高性能、低延迟的数据存储和检索场景下。Ehcache 3 提供了丰富的功能,包括本地内存缓存、磁盘持久化、多线程支持以及在分布式环境中实现集群共享缓存...
ehcache是一种广泛使用的Java缓存框架,用于提高应用程序性能,特别是在数据访问操作中。通过将数据存储在内存中,ehcache能够显著减少数据库查询次数,从而加快应用响应速度。本文将深入探讨ehcache.xml配置文件中...
**Ehcache简介** Ehcache是一款开源的Java缓存框架,它被广泛应用于提高应用程序性能,通过存储数据副本以减少对数据库的访问。Ehcache最初由Tomi Tirro设计,现在是Terracotta公司的产品,是Java世界中常用的二级...
ehcache 的 xsd 文件
EhCache是一款高效、易用且功能强大的Java缓存库,它被广泛应用于各种Java应用程序中,以提高数据访问速度并降低数据库负载。EhCache的核心特性包括内存和磁盘存储、缓存分区、过期策略以及缓存同步等。在Java开发中...