您的位置:首页 > 产品设计 > UI/UE

Ehcache 1.5.0 User Guide - Code Samples 代码实例 3

2009-07-13 14:49 399 查看

Ehcache 1.5.0 User Guide - Code Samples 代码实例 3

(Ehcache 1.5.0 用户指南)

E_mail:jianglike18@163.con

Blog: http://blog.csdn.net/jianglike18

qq:29396597

8.2 Using Caches

All of these examples refer to manager, which is a reference to a CacheManager, which has a cache in it called sampleCache1.
(所有的这些例子都涉及到一个CacheManager对象引用的管理器,它当中有一个名为sampleCache1的缓存)

8.2.1 Obtaining a reference to a Cache(获得缓存的引用)

Obtain a Cache called "sampleCache1", which has been preconfigured in the configuration file
(获得名为"sampleCache1"缓存,该缓存在配置文件中预设置了。)
Cache cache = manager.getCache("sampleCache1");

8.2.2 Performing CRUD operations (优化增删查改操作)

Put an element into a cache
(将一个元素添加至缓存。)
Cache cache = manager.getCache("sampleCache1");
Element element = new Element("key1", "value1");
cache.put(element);

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.put()方法,ehcache知道这是一个以存在的元素,并认为该put操作是一个更新操作因此通知缓存监听。)

Cache cache = manager.getCache("sampleCache1");
cache.put(new Element("key1", "value1"));
//This updates the entry for "key1"
cache.put(new Element("key1", "value2"));

Get a Serializable value from an element in a cache with a key of "key1".
(使用关键字“key1”从缓存的元素中获取序列化值。)
Cache cache = manager.getCache("sampleCache1");
Element element = cache.get("key1");
Serializable value = element.getValue();

Get a NonSerializable value from an element in a cache with a key of "key1".
(使用关键字“key1”从缓存的元素中获取非序列化值。)
Cache cache = manager.getCache("sampleCache1");
Element element = cache.get("key1");
Object value = element.getObjectValue();

Remove an element from a cache with a key of "key1".
(根据关键字“key1”移除缓存的中的元素。)

Cache cache = manager.getCache("sampleCache1");
cache.remove("key1");

8.2.3 Disk Persistence on demand (按需持久化)

sampleCache1 has a persistent diskStore. We wish to ensure that the data and index are written immediately.
(sampleCache1有一个持久化磁盘存储路径。我们希望数据和索引立即被写入)

Cache cache = manager.getCache("sampleCache1");
cache.flush();

8.2.4 Obtaining Cache Sizes (获取缓存的大小)

Get the number of elements currently in the Cache.
(获取缓存中当前元素的数量。)

Cache cache = manager.getCache("sampleCache1");
int elementsInMemory = cache.getSize();

Get the number of elements currently in the MemoryStore.
(获取当前内存中缓存元素的数量。)

Cache cache = manager.getCache("sampleCache1");
long elementsInMemory = cache.getMemoryStoreSize();

Get the number of elements currently in the DiskStore.
(获取磁盘中缓存元素的数量。)

Cache cache = manager.getCache("sampleCache1");
long elementsInMemory = cache.getDiskStoreSize();

8.2.5 Obtaining Statistics of Cache Hits and Misses (获取缓存中命中和非命中的统计)

These methods are useful for tuning cache configurations.
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();

Get the number of times requested items were found in the MemoryStore of the cache.
(获取被请求数据项在缓存的内存中被查询的次数。)

Cache cache = manager.getCache("sampleCache1");
int hits = cache.getMemoryStoreHitCount();

Get the number of times requested items were found in the DiskStore of the cache.
(获取被请求数据项在缓存的磁盘中被查询的次数。)

Cache cache = manager.getCache("sampleCache1");
int hits = cache.getDiskStoreCount();

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();

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();

These are just the most commonly used methods. See CacheTest for more examples. See Cache for the full API.
(这些仅仅只是最常用的方法。查看CacheTest获取更多的例子。查看Cache类获取完整的API。)

8.3 Creating a new cache from defaults(使用默认配置创建一个新缓存)

A new cache with a given name can be created from defaults very simply:
(一个给出名字新的缓存使用默认配置很容易创建:)
manager.addCache("cache name");

8.4 Creating a new cache with custom parameters(使用特定参数创建一个新缓存)

The configuration for a Cache can be specified programmatically in the Cache constructor:
(缓存的配置可以在Cache的构造函数中以编程的方式进行设置。)
public Cache(
String name,
int maxElementsInMemory,
MemoryStoreEvictionPolicy memoryStoreEvictionPolicy,
boolean overflowToDisk,
boolean eternal,
long timeToLiveSeconds,
long timeToIdleSeconds,
boolean diskPersistent,
long diskExpiryThreadIntervalSeconds) {
...
}
Here is an example which creates a cache called test.
(这是一个创建test缓存的例子。)
//Create a CacheManager using defaults
(创建一个默认的CacheManager。)
CacheManager manager = CacheManager.create();

//Create a Cache specifying its configuration.
(创建一个设置的配置缓存。)
Cache testCache = new Cache("test", maxElements,
MemoryStoreEvictionPolicy.LFU, true, false, 60, 30, false, 0);
manager.addCache(cache);

Once the cache is created, add it to the list of caches managed by the CacheManager:
(一旦缓存被创建,添加到被CacheManager对象管理的缓存列表。)
manager.addCache(testCache);
The cache is not usable until it has been added.
(缓存添加到CacheManager对象后才能使用。)

8.5 Registering CacheStatistics in an MbeanServer(在MbeanServer中注册CacheStatistics对象)

This example shows how to register CacheStatistics in the JDK1.5 platform MBeanServer, which works with the JConsole management agent.
(这个例子说明怎样在JDK1.5平台的MBeanServer注册CacheStatistics对象,MbeanServer与Jconsole管理代理工作。)
CacheManager manager = new CacheManager();
MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
ManagementService.registerMBeans(manager, mBeanServer, false, false, false, true);

8.6 Browse the JUnit Tests(浏览Junit测试用例)

Ehcache comes with a comprehensive JUnit test suite, which not only tests the code, but shows you how to use ehcache.
A link to browsable unit test source code for the major ehcache classes is given per section.
The unit tests are also in the src.zip in the ehcache tarball.
(Ehcache和其广泛的Junit测试套件一起提供,Junit测试套件不仅是测试代码,更是说明了如何使用ehcache。
一个连接到主要的ehcache类单元测试的源代码链接在每一个部分都给出了。
每一个单元测试也在ehcache压缩包的src.zip文件。

JCache Examples
See the JSR107 Chapter.
(Jcache的例子
查看JSR107章节。)
Cache Server Examples
See the Cache Server Chapter.
(缓存服务的例子
查看缓存服务章节。)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: