Ehcache学习文档三——Key Classes and Methods(中英文对照beta1.0)

Ehcache学习文档3——Key Classes and Methods(中英文对照beta1.0)

 

Key Classes and Methods 关键类和方法

  • Introduction
    • CacheManager
    • Ehcache
    • Element

Introduction 简介

Ehcache consists of a CacheManager, which manages caches. Caches contain Elements, which are essentially name value pairs. Caches are physically implemented, either in-memory or on disk. The logical representations of these components are actualized mostly through the classes discussed below. The methods provided by these classes are largely responsible for providing programmatic access to working with Ehcache.

Ehcache的组成包括CacheManager、 Cach、Element,CacheManager管理Cach,Cach里包括Element,Element本质上是键值对。缓存内容存放在内存或磁盘上。下面要讨论的类将会展现这些组件的逻辑形式。这些类的方法将会提供通过编码来使用Ehcache的手段。

CacheManager

Creation of, access to, and removal of caches is controlled by the CacheManager.

CachManager用于创建、访问、移除缓存。

CacheManager Creation Modes 

CacheManager 的创建模式

CacheManager supports two creation modes: singleton and instance.

CacheManager 支持两种创建模式:singleton 和instance

Versions of Ehcache before version 2.5 allowed any number of CacheManagers with the same name (same configuration resource) to exist in a JVM. Therefore, each time new CacheManager(...) was called, a new CacheManager was created without regard to existing CacheManagers. Calling CacheManager.create(...) returned the existing singleton CacheManager with the configured name (if it existed) or created the singleton based on the passed-in configuration.

版本号在2.5之前的Ehcache允许在一个虚拟机里创建多个同名(同一个配置资源)CacheManager。因此每次调用new CacheManager(...)都会创建一个新的CacheManager。调用CacheManager.create(...)将会返回已有的具有该名称的单例CacheManager ,如果没有才会基于传入的配置创建。

Ehcache 2.5 and higher does not allow multiple CacheManagers with the same name to exist in the same JVM.CacheManager() constructors creating non-Singleton CacheManagers can violate this rule, causing a NullPointerException. If your code may create multiple CacheManagers of the same name in the same JVM, avoid this error by using the static CacheManager.create() methods, which always return the named (or default unnamed) CacheManager if it already exists in that JVM. If the named (or default unnamed) CacheManager does not exist, theCacheManager.create() methods create it.

Ehcache 2.5之后的版本不允许在一个JVM中存在多个同名的CacheManager。通过CacheManager()来构造非单例的CachManager将会违背这个规则,导致抛出NullPointException。如果你的代码可能在一个JVM中创建多个同名CacheManager,那么请通过使用 static CacheManager.create()方法来避免这种错误,它将返回JVM中已有的同名(默认情况下未命名)CacheManager。如果对应名称(或未命名)的CacheManager不存在,那么CacheManager.create()将会创建它。

NOTE: In Ehcache 2.5.0/2.5.1 Cachemanager.create(...) gets or creates the CacheManager regardless of whether it is a singleton or not. In Ehcache 2.5.2, calling CacheManager.create(...) returns the existing singleton CacheManager with the configured name (if it exists) or creates the singleton based on the passed-in configuration.

注意:在Ehcache 2.5.0/2.5.1Cachemanager.create(...)将会获取或创建对应的CacheManager而不管它是否是单例。在 Ehcache 2.5.2中调用 CacheManager.create(...)将会返回已有的同名单例CacheManager(如果已经存在的话),否则根据传入的配置创建。

Ehcache 2.5.2 introduced the CacheManager.newInstance(...) method, which parses the passed-in configuration to either get the existing named CacheManager or create that CacheManager if it doesn't exist.

Ehcache 2.5.2 引入了CacheManager.newInstance(...) 方法,它会解析传入的配置来获取已有的同名CacheManager 或在没有的情况下创建CacheManager 。

With Ehcache 2.5.2 and higher, the behavior of the CacheManager creation methods is as follows:

在Ehcache 2.5.2和更高版本中,CacheManager 的创建方法如下:

  • CacheManager.newInstance(Configuration configuration) – Create a new CacheManager or return the existing one named in the configuration.创建一个 CacheManager 或者返回一个已有的同名CacheManager 
  • CacheManager.create() – Create a new singleton CacheManager with default configuration, or return the existing singleton. This is the same as CacheManager.getInstance().根据默认配置创建一个新的单例,或者返回已有的CacheManager 
  • CacheManager.create(Configuration configuration) – Create a singleton CacheManager with the passed-in configuration, or return the existing singleton .创建一个 CacheManager 或者返回一个已有的同名CacheManager 
  • new CacheManager(Configuration configuration) – Create a new CacheManager, or throw an exception if the CacheManager named in the configuration already exists. .创建一个新的 CacheManager ,或者如果存在同名CacheManager则抛出异常

See the Ehcache API documentation for more information on these methods, including options for passing in configuration. For examples, see Code Samples.

参考 Ehcache API documentation来获取这些方法详情。样例参考Code Samples。

Singleton Mode

Ehcache-1.1 supported only one CacheManager instance which was a singleton. CacheManager can still be used in this way using the static factory methods.

Instance Mode

From ehcache-1.2, CacheManager has constructors which mirror the various static create methods. This enables multiple CacheManagers to be created and used concurrently. Each CacheManager requires its own configuration.

If the Caches under management use only the MemoryStore, there are no special considerations. If Caches use the DiskStore, the diskStore path specified in each CacheManager configuration should be unique. When a new CacheManager is created, a check is made that there are no other CacheManagers using the same diskStore path. If there are, a CacheException is thrown. If a CacheManager is part of a cluster, there will also be listener ports which must be unique.

Mixed Singleton and Instance Mode

If an application creates instances of CacheManager using a constructor, and also calls a static create method, there will exist a singleton instance of CacheManager which will be returned each time the create method is called together with any other instances created via constructor. The two types will coexist peacefully.

Ehcache

All caches implement the Ehcache interface. A cache has a name and attributes. Each cache contains Elements.

A Cache in Ehcache is analogous to a cache region in other caching systems.

Cache elements are stored in the MemoryStore. Optionally they also overflow to a DiskStore.

所有的 cache 都实现了Ehcache 接口。缓存有一个name和一些 attribute。每个cache都包含了多个 Element。

Ehcache中的Cache类似于其他缓存系统中的缓存区域概念。

被缓存的Element被存在MemoryStore中,也可以配置为在内存满后转存到DiskStore。

Element

An element is an atomic entry in a cache. It has a key, a value and a record of accesses. Elements are put into and removed from caches. They can also expire and be removed by the Cache, depending on the Cache settings.

Element是cache中的原子条目。它具有一个key,一个value和一个访问记录。Element可以被放入缓存,也可以从缓存中移除。也可以由Cache根据配置项来设置过期和移除。

As of ehcache-1.2 there is an API for Objects in addition to the one for Serializable. Non-serializable Objects can use all parts of Ehcache except for DiskStore and replication. If an attempt is made to persist or replicate them they are discarded without error and with a DEBUG level log message.

从ehcache-1.2开始提供了序列化支持。没有实现序列化接口的对象可以使用Ehcache 中的各种功能除了不能DiskStore 和复制。如果尝试对它们进行序列化或复制并不会造成错误,但是会忽略对应的操作并给出一个DEBUG等级的日记信息。

The APIs are identical except for the return methods from Element. Two new methods on Element: getObjectValue and getKeyValue are the only API differences between the Serializable and Object APIs. This makes it very easy to start with caching Objects and then change your Objects to Seralizable to participate in the extra features when needed. Also a large number of Java classes are simply not Serializable.

除了从Element类返回的方法之外,所有的API都是相同的。Element有两个新方法:getObjectValue和getKeyValue是Serializable和Object API仅有的两个不同的方法 。缓存对象然后根据需要序列化这些对象是很容易的。

 

p style=