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
8、Code 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();
}