Ehcache 1.5.0 User Guide - Code Samples 代码实例 (1)

 

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

Ehcache 1.5.0 用户指南)

E_mail:jianglike18@163.con

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

qq:29396597

8Code Samples(代码实例)

 

This page shows some of the more common code samples to get you started. Code samples for each feature are in the relevant chapters.

(这一章展示了一个比较常用的代码开始使用。每一个特性的代码例子都在相关的章节中。)

8.1 Using the CacheManager(使用CacheManager对象)

All usages of ehcache start with the creation of a CacheManager.

ehcache使用都开始于CacheManager的创建。)

8.1.1 Singleton versus Instance (单例对比实例)

As of ehcache-1.2, ehcache CacheManagers can be created as either singletons (use the create factory method) or instances (use new).

ehcache-1.2版本以前,CacheManager对象可以使用单例模式(使用create工厂方法)或实例(使用new)。)

Create a singleton CacheManager using defaults, then list caches.

(使用默认配置创建一个单例的CacheManager对象,然后例举出缓存。)

CacheManager.create();

String[] cacheNames = CacheManager.getInstance().getCacheNames();

 

Create a CacheManager instance using defaults, then list caches.

(使用默认配置配置创建CacheManager实例,然后例举出缓存。)

 

 CacheManager manager = new CacheManager();

 String[] cacheNames = manager.getCacheNames();

 

Create two CacheManagers, each with a different configuration, and list the caches in each.

(创建两个CacheManager对象,每一个对象还有不同配置,然后例举出每一个对象的缓存。)

 

 CacheManager manager1 = new CacheManager("src/config/ehcache1.xml");

 CacheManager manager2 = new CacheManager("src/config/ehcache2.xml");

 String[] cacheNamesForManager1 = manager1.getCacheNames();

 String[] cacheNamesForManager2 = manager2.getCacheNames();

8.1.2 Ways of loading Cache Configuration(加载缓存配置的各种方式)

When the CacheManager is created it creates caches found in the configuration.

Create a CacheManager using defaults. Ehcache will look for ehcache.xml in the classpath.

(当CacheManager对象被创建时创建在配置文件中发现缓存。)

 

CacheManager manager = new CacheManager();

 

Create a CacheManager specifying the path of a configuration file.

(通过设置文件的路径创建一个CacheManager对象。)

CacheManager manager = new CacheManager("src/config/ehcache.xml");

Create a CacheManager from a configuration resource in the classpath.

(以类路径的方式指定资源创建一个CacheManager对象。)

 

URL url = getClass().getResource("/anotherconfigurationname.xml");

CacheManager manager = new CacheManager(url);

 

Create a CacheManager from a configuration in an InputStream.

(已输入流的形式指定资源创建CacheManager对象)

InputStream fis = new FileInputStream(new File("src/config/ehcache.xml").getAbsolutePath());

try {

    CacheManager manager = new CacheManager(fis);

} finally {

    fis.close();

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值