Java Cache ehcache-Core classes and methods introduction

Source: Internet
Author: User

Core classes and methods in Ehcache

EhCache There is a CacheManager type, which is responsible for managing Cache . the element objectis stored in the Cache and theelement must be key-value that 's right. the Cache is actually physically implemented in memory or on disk. The logical representation of these components is the class that will be discussed below. Their approach provides a programmatic way to access it.

CacheManager

responsible for Cache to create, access, and remove.

CacheManager Create

The CacheManager supports two modes of creation: single ( Singleton Mode ) and instances ( Instancemode ).

in the 2.5 In the previous version, the same JVM any number of the same names is allowed in the CacheManager. Each call to new CacheManager (...) Once, a new CacheManager instance is generated, regardless of how many already exist. Call Cachemanager.create (...) to return the singleton CacheManager that already exists for that configuration, or create one if it does not exist.


2.5 after the version, not allowed in the same JVM There are multiple CacheManager with the same name in it. Creating a CacheManager () constructor that is not a singleton instance may break this rule, but will throw a NPE exception. If your code is to Create multiple instances of the same name in the same JVM, use the static method Cachemanager.create () and always return the CacheManager of the corresponding name (if it already exists), or create one.

In fact, we can directly use Spring Ehcachemanagerfactorybean in the [spring2.5.4] to help us complete the creation of CacheManager, see how it is created:

if (this.shared) {//shared CacheManager Singleton at the VM level.if (this.configlocation! = null) {This.cachemanager = C Achemanager.create (This.configLocation.getInputStream ());} else {This.cachemanager = Cachemanager.create ();}} else {//Independent CacheManager instance (the default). if (this.configlocation! = null) {This.cachemanager = new Cachema Nager (This.configLocation.getInputStream ());} else {This.cachemanager = new CacheManager ();}} if (this.cachemanagername! = null) {this.cacheManager.setName (this.cachemanagername);}



EhCache2.5.2 and the creation of the above versions are summarized as follows:

    • Cachemanager.newinstance (Configuration): Creates a new CacheManager object or returns the CacheManager of the name in the corresponding configuration that already exists
    • Cachemanager.create (): Creates a new default configuration of singleton CacheManager, or returns a singleton that already exists.
    • Cachemanager.create (configuration), create a singleton cachemanager that corresponds to the name in the incoming configuration file, or return a singleton cachemanager that already exists.
    • Create a new CacheManager (configuration), CacheManager, or throw an exception if the corresponding configuration CacheManager already exists or the configuration parameter is empty.

in Instance Mode cache Span lang= "en-US" >memorystore diskstore CacheManager must have a different diskstore path. When a new cachemanager is created, you need to check for no other CacheManager Use the same diskstore path. If there is, an exception is thrown cacheexception cachemanager

Singletonmode and the Instance Mode It can be mixed, without conflict.

Ehcache

all the Cache interfaces are implemented. Ehcache . Each cache has a name and attributes, and contains Element.

Ehcache in the Cache equivalent to a piece of cache area in another cache system.

Element

Element is stored in Cache the atomic units in the. It has a key, a value , and a record of access. Element is either placed in the cache or removed from the cache . They may also be removed due to expiration, which relies on configuration.

References:

Source Document

Java Cache ehcache-Core classes and methods introduction

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.