In-process cache framework for Java: EhCache

Source: Internet
Author: User
Tags delete cache

features of the Ehcache cache:1. Fast. 2. Simple. 3. Multiple caching Strategies 4. There are two levels of cache data: memory and disk, so there is no need to worry about capacity issues 5. The cached data is written to disk 6 during the virtual machine restart. Distributed caching can be done through RMI, pluggable APIs, and so on 7. Listening interface with cache and cache manager 8. Supports multi-cache manager instances, and multiple cache zones for one instance 9. Provides a cache implementation of Hibernate Ehcache Cache Usage (1) – Install EhcacheEhcache features, is a pure Java, the process (can also be understood as a plug-in) cache implementation, the installation of Ehcache, the need to Ehcache-x.x.jar and related class library to classpath. If Hibernate is already installed on the project, you do not need to do anything, you can use Ehcache directly. Ehcache Cache Usage (2)-Generate CacheManagerUse CacheManager to create and manage Cache1. There are 4 ways to create CacheManager: A: Create Java code with the default profile 1.CacheManager Manager = Cachemanager.create ();  B: Create Java code with the specified configuration file 1.CacheManager Manager = cachemanager.create ("Src/config/ehcache.xml"); C: Find the configuration file from Classpath and create Java code 1.URL URL = GetClass (). GetResource ("/anothername.xml");  2.CacheManager manager = cachemanager.create (URL); D: Create Java code from the input stream 1.InputStream FIS = new FileInputStream (New File ("Src/config/ehcache.xml"). GetAbsolutePath ()); 2.try {3.manager = Cachemanager.create (FIS); 4.} finally {5.fis.close (); 4.? Ehcache Cache Usage (3) – Interpreting Ehcache configuration files Ehcache.xmlImportant Parameters <diskstore path= "D:/work2/renhewww/cache"/><cache name= "SampleCache1" maxelementsinmemory= "1"            Maxelementsondisk= "10000" eternal= "false" overflowtodisk= "true" diskspoolbuffersizemb= "20" Diskpersistent= "true" timetoidleseconds= "43200" timetoliveseconds= "86400" Memorys toreevictionpolicy= "LFU"/>

Attribute Explanation:

Must attribute:

Name: Set the cache names for the flag cache, the only

Maxelementsinmemory: The maximum number of objects in memory

Maxelementsondisk: Maximum number of objects in Diskstore, if 0, no limit

Eternal: Sets whether the element is permanent, and if it is permanent, timeout ignores

Overflowtodisk: If the number in memory reaches the limit, save to disk

Optional properties:

Timetoidleseconds: Set idle time before an element expires

Timetoliveseconds: Set the active time before the element expires

Diskpersistent: Whether disk store persists on virtual machine startup. Default is False

Diskexpirythreadintervalseconds: Time to run disk end thread, default is 120 seconds

Memorystoreevictionpolicy: Strategy About Eviction

Cache child elements:

Cacheeventlistenerfactory: Registers the appropriate cache listener class for handling cache events, such as put,remove,update, and expire

Bootstrapcacheloaderfactory: Specifies the appropriate bootstrapcacheloader for initializing the cache, as well as for automatic provisioning.

Ehcache Caching Usage (4) – Create CacheCreate Cachecache cache = Manager.getcache ("SampleCache1") via CacheManager; Ehcache Cache Usage (5) – Access data with cacheStorage data element element = new Element ("Key1", "value1"); Cache.put (element); Gets the data element element = Cache.get ("Key1");

cache creation, in an automated manner

CacheManager Singletonmanager = Cachemanager.create ();

Singletonmanager.addcache ("Testcache");

Cache test = Singletonmanager.getcache ("Testcache");

Or create a cache directly

CacheManager Singletonmanager = Cachemanager.create ();

Cache Memoryonlycache = new cache ("Testcache", 5, False, False, 2);

Manager.addcache (Memoryonlycache);

Cache test = Singletonmanager.getcache ("Testcache");

Delete Cache

CacheManager Singletonmanager = Cachemanager.create ();

Singletonmanager.removecache ("SampleCache1");

After you use Ehcache, you need to close

Cachemanager.getinstance (). Shutdown ()

Use of Caches

Cache cache = Manager.getcache ("SampleCache1");

Perform CRUD operations

Cache cache = Manager.getcache ("SampleCache1");

element element = new Element ("Key1", "value1");

Cache.put (Element);

//update

Cache cache = Manager.getcache ("SampleCache1");

Cache.put (New Element ("Key1", "value1");

This updates the entry for "Key1"

Cache.put (New Element ("Key1", "value2");

//get Serializable

Cache cache = Manager.getcache ("SampleCache1");

Element element = Cache.get ("Key1");

Serializable value = Element.getvalue ();

Get non Serializable

Cache cache = Manager.getcache ("SampleCache1");

Element element = Cache.get ("Key1");

Object value = Element.getobjectvalue ();

//remove

Cache cache = Manager.getcache ("SampleCache1");

element element = new Element ("Key1", "Value1" Cache.remove ("Key1");

In-process cache framework for Java: EhCache

Related Article

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.