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

 

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

Ehcache 1.5.0 用户指南)

E_mail:jianglike18@163.con

Blog: http://blog.csdn.net/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获取更多的例子。)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值