Ehcache Introduction
Ehcache is a very good, lightweight, local caching scheme that can solve the fast saving and accessing of static resources in large concurrency situations.
Introducing the Ehcache jar package
The latest version of the Ehcache jar package is introduced here:
<dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> <version>2.10.0</version> <type>pom</type></dependency>
Basic concepts
1. CacheManager
CacheManager is a cache manager, you must create a CacheManager using Ehcache, which is created in two ways:
A. Singleton mode:
privatestaticvolatile CacheManager cacheManager = CacheManager.create()
B. Ehcache 2.x allows multiple CacheManager to be present, the new CacheManager can be configured through. xml, as follows Demo-ehcache.xml
<ehcache name="DemoCacheManager"></ehcache>
Initialize non-singleton mode CacheManager with an. xml file, as follows:
privatestaticvolatile CacheManager cacheManager = CacheManager.newInstance(this.class.getClassLoader().getResourceAsStream("demo-ehcache.xml"));
2. Cache (Cached)
A CacheManager can manage multiple caches, each of which has its own properties in memory, such as the maximum number of cache elements (element), the longest surviving time in memory, the cache culling algorithm, and so on. A cache can store multiple cached elements (element). The cache is registered before use, and there are two ways to register it:
1. Register with the XML configuration file. The configuration file can be configured with the name of the cache, the maximum number of slow existing heap, the cache elimination algorithm and so on, the parameters are very many, the following is a simple example:
<ehcache ; <cache name = "demo" ma Xentrieslocalheap = "10000" eternal = "false" timetoidleseconds =" " timetoliveseconds =" + " memorystoreevictionpolicy = "LFU" ; </cache ; </ehcache ;
A cache named demo is configured above, it has a maximum number of 10,000 in memory, the longest surviving time is timetoliveseconds to 10 minutes (600s), and the longest reachable time is timetoidleseconds 5 minutes (300s). When the number exceeds the maximum number limit, the LFU algorithm is used for elimination. Eternal Indicates whether the cache is permanently non-invalidated, and when this value is true, the maximum surviving time and the longest accessible time of the above are invalid.
2. Dynamic registration via Code
The following is an example of registering an effect with an XML file:
CacheNewmemcache= New Cache("Demo", +,///maximum number of elements in memoryMemorystoreevictionpolicy.LFU,//Elimination Strategy false,//Do you want to save to disk "/data/appdatas",//Where disk is presentFale,//Whether permanently valid -,//Timetoliveseconds -,//Timetoidleseconds false,//Whether to save to disk when the JVM restarts - * -,//How long to run a disk time-out thread (this parameter is useless) NULL //Register event); CacheManager.Addcache (Newmemcache);
3. Element cache elements
A cache element is the smallest unit of data stored, consisting of its elementkey and objects, defined as follows:
newobject);
Note: Object here can be any object!!
Invoke description
1. Add Operation
add element to the cache as follows:
Cachecache= cacheManager.getCache("demo"); if (cache!=null) { =new Element("demoElementKey""demo object"); cache.put(element); }
2. Get Operations
Fetching elements from the cache
Cachecache= cacheManager.getCache("demo"); if (null!=cache) { =cache.get("demoElementKey"); }
Specific examples
For specific examples, please refer to the ehcahce-client I wrote.
Ehcache Invocation Description