一. 创建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服务器上使用分布式缓存...
#### 十二、代码示例(Code Samples) 提供了丰富的代码示例,覆盖了从基本用法到高级功能的各种场景,有助于开发者快速上手并深入理解 Ehcache 的工作原理。 #### 十三、类加载与类加载器(Classloading and ...
在“ehcache-test-samples”项目中,我们可以找到一系列测试和示例,帮助开发者深入理解EhCache的工作原理以及如何在实际项目中有效利用它。 1. **EhCache的基本概念**: - **缓存**:缓存是一种存储临时数据的...
- `samples`目录:提供了一些示例应用,展示了如何在实际项目中使用Ehcache,这些例子可以帮助新手快速上手。 8. **库文件**: - `lib`目录:包含Ehcache运行所需的库文件,这些jar文件是Ehcache和其他依赖库的二...
Spring Boot 2.0 Samples中的缓存示例可能包括了如何使用Spring Cache抽象或者集成Redis、EhCache这样的缓存系统。 通过对这些文件的分析,我们可以看出Spring Boot 2.0 Samples资源包是对Spring Boot 2.0框架进行...
#### 十二、代码示例(Code Samples) 为了帮助开发者更好地理解和使用Ehcache,文档提供了大量的代码示例。这些示例涵盖了Ehcache的各种功能和使用场景,从简单的缓存配置到复杂的缓存管理都有涉及。 #### 十三、...
17. **Ehcache3**:Ehcache 3是流行的Java缓存库,Spring Boot starter-ehcache3提供了对Ehcache 3的集成。 通过这些样例,开发者可以深入理解Spring Boot Starter如何工作,以及如何结合各种技术构建现代的微服务...
在这些压缩包文件中,"samples-dist[1][1].part1.rar"、"samples-dist[1][1].part2.rar"和"samples-dist[1][1].part3.rar"可能包含的源代码涵盖了多个章节的示例,涉及以下关键知识点: 1. **配置文件**:Hibernate...
5. **缓存机制**:展示如何配置和使用Hibernate的缓存策略,如一级缓存和二级缓存,以及第三方缓存解决方案如 EhCache 和 Infinispan。 `samples.zip`中的实战案例可能涵盖: 1. **项目集成**:如何在实际项目中...
3. **Caching**:缓存管理示例,展示了如何配置Shiro使用缓存来提高性能,比如使用EhCache或Redis存储会话和权限信息。 4. **Remember Me**:记住我功能示例,让用户在关闭浏览器后仍然保持登录状态。 5. **...