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. You can use Ehcache Cache storage directly: Memory or disk.
2. Use EHCache alone to create and manage the cache using CacheManager
1. There are 4 ways of creating CacheManager:
A: Create Java code with default profile CacheManager Manager = Cachemanager.create ();
B: Create Java code using the specified configuration file CacheManager manager = cachemanager.create ("Src/config/ehcache.xml");
C: Find the configuration file from Classpath and create
Java code:
URL url = getclass (). GetResource ("/anothername.xml");
CacheManager manager = cachemanager.create (URL);
D: Created from the input stream
Java code:
InputStream fis = new FileInputStream (New File ("Src/config/ehcache.xml"). GetAbsolutePath ());
try {
Manager = Cachemanager.create (FIS);
}
finally {
Fis.close ();
}
Uninstall CacheManager, close cache Java code manager.shutdown ();
Use caches to get pre-defined sampleCache1 settings in the configuration file,
Generate a cache from CacheManager
Java code:
Cache cache = Manager.getcache ("SampleCache1");
Set a new Cache,test property named Test as the default
Java code:
CacheManager manager = Cachemanager.create (); Manager.addcache ("test");
Set a new cache named Test and define its properties
Java code CacheManager Manager = Cachemanager.create ();
Cache cache = New cache ("Test", 1, True, False, 5, 2);
Manager.addcache (cache);
Adding elements to the cache
Java code element Element = new Element ("Key1", "value1");
Cache.put (Element);
Get the element Java code from the cache
Element element = Cache.get ("Key1");
Transferred from: http://my.oschina.net/meSpace/blog/94933
The use of Ehcache's CacheManager