This article refers to the integration of multiple articles on the web.
1. Required JAR Package
Ehcache-core-2.5.2.jar Slf4j-api-1.6.1.jar Cglib-2.2.jar [----This package may not be needed] Ehcache-spring-annotations-1.1.2.jar Aopalliance.jar
2. Configure Ehcache in spring's configuration file Applicationcontext.xml
<ehcache:annotation-driven cache-manager= "Ehcachemanager"/>
<bean id= "Ehcachemanager" class= " Org.springframework.cache.ehcache.EhCacheManagerFactoryBean ">
<property name=" configlocation "value=" Classpath:conf/<span style= "color: #ff0000;" >ehcache.xml</span> "/>
Configuration of the 3.ehcache.xml
<?xml version= "1.0" encoding= "UTF-8"?> <ehcache
xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance "xsi:nonamespaceschemalocation=" http://ehcache.org/ehcache.xsd ">
<defaultcache eternal=" true " Maxelementsinmemory= "overflowtodisk=" false "/>
<cache name=" Messagecache "maxelementsinmemory=" 10 " Eternal= "true" overflowtodisk= "false"/>
<cache name= "Messagescache" maxelementsinmemory= "eternal=" True "overflowtodisk=" false "/>
</ehcache>
At this point, the configuration of the Ehcache is complete. Use Demo to test
Import java.io.Serializable;
Import Net.sf.ehcache.Cache;
Import Net.sf.ehcache.CacheManager;
Import net.sf.ehcache.Element;
Public abstract class Ehcache_demo {/** * @param args *///static CacheManager manager= new CacheManager (); /** *############################################################################## * * @DESCRIBE * @p Aram args * @throws interruptedexception * *############################################################### ############### */public static void main (string[] args) throws Interruptedexception {CacheManager
Manager = new CacheManager ("Resource/conf/ehcache.xml");
string[] Cachenames = Manager.getcachenames ();
System.out.println (System.getproperty ("Java.io.tmpdir"));
SYSTEM.OUT.PRINTLN ("Read Cache list is:");
for (int i=0;i<cachenames.length;i++) {System.out.println ("--" + (i+1) + "" +cachenames[i]); } Cache cache = Manager.geTcache ("Messagecache");
element element = new Element ("Key1", "value1");
Cache.put (Element);
element = Cache.get ("Key1");
Serializable value = Element.getvalue ();
System.out.println ("The serialized value is:" +value.tostring ());
element = Cache.get ("Key1");
Object value1 = Element.getobjectvalue ();
SYSTEM.OUT.PRINTLN ("Non-serialized value:" +value1.tostring ());
int elementsinmemory = Cache.getsize ();
System.out.println ("Number of objects Cached:" +elementsinmemory);
Long elementsInMemory1 = Cache.getmemorystoresize ();
SYSTEM.OUT.PRINTLN ("The amount of memory consumed by the cache object:" +elementsinmemory1);
Long ElementsInMemory2 = Cache.getdiskstoresize ();
System.out.println ("Gets the number of cached objects that are consumed by the object:" +elementsinmemory2); }
}
Output: c:\users\admini~1\appdata\local\temp\ Read Cache list is:--1 Messagecache--2 Messagescache The value after serialization is: value1 The value is not serialized: value1 Number of objects cached: 1 Gets the amount of memory consumed by the cache object: 1 Gets the number of cached objects that occupy the disk: 0