EhcacheUse Case (1)
SingletonCreation Method
After ehcache1.2, you can use Singleton (factory creation method) to create a singleton cachemanager instance.
Cachemanager. Create ();
String [] cachenames = cachemanager. getinstance (). getcachenames ();
Use the default configuration to create a cachemanager
Cachemanager manager = new cachemanager ();
String [] cachenames = manager. getcachenames ();
Use the configuration file to create the specified cachemanager
Cachemanager manager1 = new cachemanager ("src/config/ehcache1.xml ");
Cachemanager manager2 = new cachemanager ("src/config/ehcache2.xml ");
String [] cachenamesformanager1 = manager1.getcachenames ();
String [] cachenamesformanager2 = manager2.getcachenames ();
Configuration File Loading Method
· Cachemanager manager = new cachemanager (); The ehcache. xml configuration file is found in the classpath path.
· Cachemanager manager = new cachemanager ("src/config. ehcache. xml"); you can also load the configuration file according to the relative file path.
· Load through URL
URL url = getclass (). getresource ("/anotherconfigurationname. xml ");
Cachemanager manager = new cachemanager (URL );
· Stream Loading
Inputsream FCM = new fileinputstream (new file ("src/config/ehcache. xml"). getabsolutepath ());
Try {
Cachemanager manager = new cachemanager (FCM );
} Finally {
FCM. Close ();
}
Encoding implementation addition and caching
In ehcache, you can not only use the configuration file to configure the cache, but alsoCodeYou can also implement the same function.
Cachemanager singletonmanager = cachemanager. Create ();
Cache memoryonlycache = new cache ("testcache", 50000, false, false, 8, 2 );
Cache test = singletonmanager. getcache ("testcache ");
To delete a file, you only need to call singletonmanager. removecache ("testcache ");
Shotdown cachemanager
After using ehcache, you must shutdown the cache. Ehcache has its own closing mechanism, but it is best to display the call to cachemanager. getinstance (). Shutdown () in your code ();
Http://blog.csdn.net/mgoann/archive/2009/04/20/4095155.aspx