Ehcache Cache Usage

Source: Internet
Author: User

A: Detailed configuration steps

1. Add Ehcache.xml File

Add the Ehcache.xml file to the SRC path. Ehcache.xml file contents are as follows

[HTML]View Plaincopyprint?
  1. <ehcache>
  2. <diskstore path="Java.io.tempdir" />
  3. <defaultcache maxelementsinmemory="$" eternal="false"
  4. timetoidleseconds="timetoliveseconds=" overflowtodisk= " true" />
  5. <cache name="Ehcachename" maxelementsinmemory="10000 "
  6. eternal="false" timetoidleseconds="300000" timetoliveseconds="600000"
  7. overflowtodisk="true" />
  8. </ehcache>

2. Add Spring configuration file

Add in the Appliccontext.xml file

[HTML]View Plaincopyprint?
  1. <Bean id="cachemanagerfactory"
  2. class="Org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
  3. p:configlocation="Classpath:ehcache.xml"></beans>
  4. <!--statement CacheManager--
  5. <Bean id="CacheManager" class= "Org.springframework.cache.ehcache.EhCacheCacheManager "   
  6. p:cachemanager-ref="Cachemanagerfactory" ></beans>

Two: Use

1, defining the Ehcache tool method

[Java]View Plaincopyprint?
  1. Public class EHCache {
  2. private static final CacheManager CacheManager = new CacheManager ();
  3. private cache cache;
  4. Public Ehcacheservice () {
  5. This.cache=cachemanager.getcache ("Ehcachename")
  6. }
  7. Public Cache GetCache () {
  8. return cache;
  9. }
  10. public void Setcache (cache cache) {
  11. This.cache = cache;
  12. }
  13. /* 
  14. * Get data from the cache by name
  15. */
  16. Public Object getcacheelement (String CacheKey) throws Exception {
  17. Net.sf.ehcache.Element e = Cache.get (CacheKey);
  18. if (e = = null) {
  19. return null;
  20. }
  21. return E.getvalue ();
  22. }
  23. /* 
  24. * Add an object to the cache
  25. */
  26. public void Addtocache (String cacheKey, Object result) throws Exception {
  27. Element element = new Element (CacheKey, result);
  28. Cache.put (Element);
  29. }
  30. }

2, test

[Java]View Plaincopyprint?
  1. Public class test{
  2. EHCache EHCache = new EHCache ();
  3. public void Test () {
  4. //test storing JSON objects in cache
  5. Jsonobject obj = new Jsonobject ();
  6. Obj.put ("name","LSZ");
  7. Ehcache.addtocache ("Cache_json", obj);
  8. //Get from the cache
  9. Jsonobject getobj = (jsonobject) ehcache.getcacheelement ("Cache_json");
  10. System.out.println (Getobj.tostring ());
  11. }
  12. }

Three: Problem solving

1, the framework environment is self-built, add Ehcache after the run error:

Org.springframework.beans.factory.parsing.BeanDefinitionParsingException:Configuration problem:unable to locate Spring Namespacehandler for XML schema namespace [Http://www.springframework.org/schema/cache]
Offending Resource:class path resource [Applicationcontext.xml]

This problem occurs because in the Applicationcontext.xml file, you add more

<cache:annotation-driven cache-manager= "CacheManager"/> Remove it


2, the framework needs to add a jar package

Spring-context-support-3.2.0.release.jar

Spring-context-3.2.0.release.jar

Source: http://blog.csdn.net/lishuangzhe7047/article/details/41680921

Ehcache Cache Usage

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.