Ehcache use case

Source: Internet
Author: User

Singleton Creation Method

Java code
  1. // After ehcache1.2, you can use Singleton (factory creation method) to create a singleton cachemanager instance.
  2. Cachemanager. Create ();
  3. String [] cachenames = cachemanager. getinstance (). getcachenames ();
  4. // Use the default configuration to create cachemanager
  5. Cachemanager manager = new cachemanager ();
  6. String [] cachenames = manager. getcachenames ();
  7. // Use the configuration file to create the specified cachemanager
  8. Cachemanager manager1 = new cachemanager ("src/config/ehcache1.xml ");
  9. Cachemanager manager2 = new cachemanager ("src/config/ehcache2.xml ");
  10. String [] cachenamesformanager1 = manager1.getcachenames ();
  11. String [] cachenamesformanager2 = manager2.getcachenames ();

Configuration File Loading Method
 

Java code
  1. Cachemanager manager = new cachemanager (); // The ehcache. xml configuration file is found in the classpath path.
  2. Cachemanager manager = new cachemanager ("src/config. ehcache. xml"); // You can also load the configuration file based on the relative file path.
  3. // Load by URL
  4. URL url = getclass (). getresource ("/anotherconfigurationname. xml ");
  5. Cachemanager manager = new cachemanager (URL );
  6. // Load by stream
  7. Inputsream FCM = new fileinputstream (new file ("src/config/ehcache. xml"). getabsolutepath ());
  8. Try {
  9. Cachemanager manager = new cachemanager (FCM );
  10. } Finally {
  11. FCM. Close ();
  12. }

Encoding implementation addition and caching
 

Java code
  1. // In ehcache, you can not only configure the cache using the configuration file, but also implement the same function in the code.
  2. Cachemanager singletonmanager = cachemanager. Create ();
  3. Cache memoryonlycache = new cache ("testcache", 50000, false, false, 8, 2 );
  4. Cache test = singletonmanager. getcache ("testcache ");
  5. // You only need to call
  6. Singletonmanager. removecache ("testcache ");

Shotdown cachemanager

After using ehcache, you must shutdown the cache. Ehcache has its own closing mechanism, but it is best to display the call to cachemanager. getinstance (). Shutdown () in your code ();

 

Ehcache Use instance (2)

Cache Usage
L get a cache reference

Obtain a reference of samplecache1 and download ehcache from the official website. XML, in ehcache. XML already has a configured cache, which can be directly used or tested. If the cache is actually used, it is better to manually configure it.
 

Java code
  1. Cache cache = manager. getcache ("samplecache1 ");

L use Cache

Put an element to the cache
 

Java code
  1. Cache cache = manager. getcache ("samplecache1 ");
  2. Element element = new element ("key1", "value1 ");
  3. Cache. Put (element );

When updating an element, you only need to input the same key when constructing the element and call the cache. put (element). This is where ehcache finds the corresponding element in the cache based on the key and updates it.
 

Java code
  1. Cache cache = manager. getcache ("samplecache1 ");
  2. Cache. Put (new element ("key1", "value1 "));
  3. // Update the element
  4. Cache. Put (new element ("key1", "value2 "));

Get the serialized value of the corresponding element based on the key
 

Java code
  1. Cache cache = manager. getcache ("samplecache1 ");
  2. Element element = cache. Get ("key1 ");
  3. Serializable value = element. getvalue ();

Obtains the non-serialized value of the corresponding element based on the key.
 

Java code
  1. Cache cache = manager. getcache ("samplecache1 ");
  2. Element element = cache. Get ("key1 ");
  3. Ojbect value = element. getobjectvalue ();

Remove the element corresponding to the key from the cache
 

Java code
  1. Cache cache = manager. getcache ("samplecache ");
  2. Element element = new element ("key1", "value1 ");
  3. Cache. reomve ("key1 ");

L disk persistence

The configuration of samplecache1 supports disk persistence. To ensure that the element is immediately output to the disk, you can call cache. Flush ();
 

Java code
  1. Cache cache = manager. getcache ("samplecache1 ");
  2. Cache. Flush ();

L cache sizes

Obtain the number of elements in the current cache.
 

Java code
  1. Cache cache = manager. getcache ("samplecache1 ");
  2. Int elementsinmemory = cache. getsize ();

Obtain the number of elements in the current memorystore.
 

Java code
  1. Cache cache = manager. getcache ("samplecache1 ");
  2. Long elementsinmemory = cache. getmemorystoresize ();

Obtain the number of elements in the current diskstore.
 

Java code
  1. Cache cache = manager. getcache ("samplecache1 ");
  2. Long elementsinmemory = cache. getdiskstoresize ();

L cache hits and misses

The so-called hits is the number of cache accesses, And the Misses is the number of loss of each element in the cache. These parameters are of great help for optimizing the cache configuration.

Obtains the number of times the requested element is found in the cache.
 

Java code
  1. Cache cache = manager. getcache ("samplecache1 ");
  2. Int hits = cache. gethitcount ();

Obtain the number of times the requested element is found in memorystore.
 

Java code
  1. Cache cache = manager. getcache ("samplecache1 ");
  2. Int hits = cache. getmemorystorehitcount ();

Obtain the number of times the requested element is found in diskstore.
 

Java code
  1. Cache cache = manager. getcache ("samplecache1 ");
  2. Int hits = cache. getdiskstorehitcount ();

Obtain the number of times that the requested element is not found in memorystore.
 

Java code
  1. Cache cache = manager. getcache ("samplecache1 ");
  2. Int hits = cache. getmisscountnotfound ();

Obtain the number of times that the requested element is not found in memorystore.
 

Java code
  1. Cache cache = manager. getcache ("samplecache1 ");
  2. Int hits = cache. getmisscountnotfound ();

Obtain the number of times that the invalid element in the cache is not found.
 

Java code
  1. Cache cache = manager. getcache ("samplecache1 ");
  2. Int hits = cache. getmisscountexpired ();

Create a default cache Configuration
Manager. addcache (string cachename); this method can be said to be a flexible embodiment of ehcache. Generally, when we want to add a new cache to cachemanager, we should call Manager. addcache (Cache cache); you can see that the input parameter is a cache type object. In the previous Code, ehcache automatically creates a cache named output parameter by default, is it very important.

Use custom parameters to create a cache
Ehcache allows you to create a custom cache by encoding, that is, call the constructor.
 

Java code
  1. Publiccache (string name, int maxelementsinmemory, bytes, Boolean overflowtodisk, Boolean eternal, long timetoliveseconds, long timetoidleseconds, Boolean isdkpersistent, long diskexpirythreadintervalseconds ){}
  2. // The above is the cache constructor.
  3. Cachemanager manager = cachemanager. Create ();
  4. Cache cache = new cache ("test", maxelements, memorystoreevictionpolicy. LFU, true, false, 60, 30, false, 0 );
  5. Manager. addcache (testcache );

The above Code creates a cache and adds it to cachemanager. The cache name is test, the memory eviction policy is LFU, And the cache name can be output to the disk. The element is not permanently valid, the maximum survival time of an element is 60 seconds, and the maximum idle time of an element is 30 seconds. It is not persisted to the disk. The running interval of the failed element cleanup thread is 0 seconds.

EhcacheUse instances (III)

Note: Like other blogs with source code, the ehcache series is also based on one example for debugging tracking. For example, see the attachment in the opening remarks of ehcache (1): Spring + ehcache. if there are no examples for reference, you may be confused during reading.
------------------------------

Ehcache (2): Starting from ehcachemanagerfactorybean, we can see the creation of an ehcachemanagerfactorybean and then enable a cachemanager instance. combined with the ehcache configuration file and the cachemanager name, it is difficult to guess that this instance manages the cache. so where is this cachemanager instance used? In the configuration file, the org. springframework. cache. ehcache. ehcachefactorybean instance is being created. So what does this cachemanager instance do? This depends on the afterpropertiesset method of the ehcachefactorybean class.

The afterpropertiesset method has the following code:

Java code
  1. If (this. cachemanager. cacheexists (this. cachename )){
  2. If (logger. isdebugenabled ()){
  3. Logger. debug ("using existing ehcache cache region '" + this. cachename + "'");
  4. }
  5. This. cache = This. cachemanager. getehcache (this. cachename );
  6. }

In other words, cachemanager checks whether the cache corresponding to the configured cachename (that is, the cache named com. rmn190.methodcache in the ehcache. xml file) already exists. If yes, get it directly.

This is just to get the existing cache in cachemanager. How is the existing cache created? That is, when/how to create the cache named com. rmn190.methodcache in this example?

After a while, I found the createcache (cacheconfiguration) method in the configurationhelper class. Here I really see the call of "new cache.

The above is a deep understanding of the cache instance creation and get through cachemanager. After the get is made, set is given to the methodcacheinterceptor class attribute cache in the example in spring. but there is a problem here: the methodcacheinterceptor class attribute cache is a net. SF. ehcache. the cache type, but the set instance in the spring configuration file is an org. springframework. cache. ehcache. ehcachefactorybean does not match the type. Does ehcachefactorybean have an inheritance or implementation relationship with the cache? Why does spring solve this Type Mismatch problem?

We found the answer on the Interface factorybean implemented by the ehcachefactorybean class: GetObject and getobjecttype. The factorybean interface is used to define the information provided by the two methods. Spring naturally and smoothly solved the type matching problem.

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.