Ehcache 1.5.0 User Guide - Code Samples 代码实例2
Ehcache 1.5.0 用户指南)
E_mail:jianglike18@163.con
Blog: http://blog.youkuaiyun.com/jianglike18
qq:29396597
8.1.3 Adding and Removing Caches Programmatically(编程的添加和移除缓存)
You are not just stuck with the caches that were placed in the configuration. You can create and remove them programmatically.
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.
(你不要被束缚了认为缓存只能在配置文件设置。你还可以编程的增加或移除。
使用默认的配置添加一个缓存,然后使用它。下面的例子创建了名为testCache的缓存,该缓存使用配置文件的默认缓存设置。)
CacheManager singletonManager = CacheManager.create();
singletonManager.addCache("testCache");
Cache test = singletonManager.getCache("testCache");
Create a Cache and add it to the CacheManager, then use it. Note that Caches are not usable until they have been added to a CacheManager.
(创建一个缓存并添加至CacheManager对象,然后使用它。注意缓存添加至CacheManager对象后才可以使用。)
CacheManager singletonManager = CacheManager.create();
Cache memoryOnlyCache = new Cache("testCache", 5000, false, false, 5, 2);
manager.addCache(memoryOnlyCache);
Cache test = singletonManager.getCache("testCache");
See Cache#Cache(...) for the full parameters for a new Cache:
Remove cache called sampleCache1(移除缓存sampleCache1)
CacheManager singletonManager = CacheManager.create();
singletonManager.removeCache("sampleCache1");
8.1.4 Shutdown the CacheManager(关闭缓存)
Ehcache should be shutdown after use. It does have a shutdown hook, but it is best practice to shut it down in your code.
(Ehcache 使用后应该关闭。它有一个关闭的钩子,但是最好的办法是在你的代码进行关闭。)
Shutdown the singleton CacheManager
(关闭单实例的CacheManager对象。)
CacheManager.getInstance().shutdown();
Shutdown a CacheManager instance, assuming you have a reference to the CacheManager called manager
(关闭一个CacheManager实例,假定你有一个CacheManager对象的应用manager)
manager.shutdown();
See the CacheManagerTest for more examples.
(查看CacheManagerTest获取更多的例子。)