The Ehcache of Java cache technology

Source: Internet
Author: User

1. EHCache 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. Direct access to Ehcache

Cache storage mode: Memory or disk

2. Use EHCache alone

So the approximate steps are:
First step: Generate CacheManager objects
Step two: Generate the Cache object
Step three: add element elements of a key-value pair composed of Key,value to the cache object

Specific one Test.java program:

  1. Package test;
  2. Import Net.sf.ehcache.Cache;
  3. Import Net.sf.ehcache.CacheManager;
  4. Import net.sf.ehcache.Element;
  5. /**
  6. * First step: Generate CacheManager objects
  7. * Step two: Generate the Cache object
  8. * Step Three: add element elements of key-value pairs composed of Key,value to the cache object
  9. * @author Mahaibo
  10. *
  11. */
  12. Public class Test {
  13. public static void Main (string[] args) {
  14. //Specify the location of the Ehcache.xml
  15. String filename="E:\\1008\\workspace\\ehcachetest\\ehcache.xml";
  16. CacheManager manager = new CacheManager (FileName);
  17. //Remove all CacheName
  18. String names[] = Manager.getcachenames ();
  19. For (int i=0;i<names.length;i++) {
  20. System.out.println (Names[i]);
  21. }
  22. //Generate a cache object based on CacheName
  23. //The first way:
  24. Cache Cache=manager.getcache (names[0]);
  25. ///The second way, Ehcache must have defaultcache exist, "test" can be replaced by any value
  26. Cache cache = New cache ("Test", 1, True, False, 5, 2);
  27. Manager.addcache (cache);
  28. //Add element elements to the cache object, element elements have key,value key-value pairs
  29. Cache.put (new Element ("Key1","values1"));
  30. Element element = Cache.get ("Key1");
  31. System.out.println (Element.getvalue ());
  32. Object obj = Element.getobjectvalue ();
  33. System.out.println ((String) obj);
  34. Manager.shutdown ();
  35. }
  36. }

The Ehcache of Java cache technology

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.