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:
- Package test;
- Import Net.sf.ehcache.Cache;
- Import Net.sf.ehcache.CacheManager;
- Import net.sf.ehcache.Element;
- /**
- * First step: Generate CacheManager objects
- * Step two: Generate the Cache object
- * Step Three: add element elements of key-value pairs composed of Key,value to the cache object
- * @author Mahaibo
- *
- */
- Public class Test {
- public static void Main (string[] args) {
- //Specify the location of the Ehcache.xml
- String filename="E:\\1008\\workspace\\ehcachetest\\ehcache.xml";
- CacheManager manager = new CacheManager (FileName);
- //Remove all CacheName
- String names[] = Manager.getcachenames ();
- For (int i=0;i<names.length;i++) {
- System.out.println (Names[i]);
- }
- //Generate a cache object based on CacheName
- //The first way:
- Cache Cache=manager.getcache (names[0]);
- ///The second way, Ehcache must have defaultcache exist, "test" can be replaced by any value
- Cache cache = New cache ("Test", 1, True, False, 5, 2);
- Manager.addcache (cache);
- //Add element elements to the cache object, element elements have key,value key-value pairs
- Cache.put (new Element ("Key1","values1"));
- Element element = Cache.get ("Key1");
- System.out.println (Element.getvalue ());
- Object obj = Element.getobjectvalue ();
- System.out.println ((String) obj);
- Manager.shutdown ();
- }
- }
The Ehcache of Java cache technology