Ehcache Learning (7) instance 1

Source: Internet
Author: User

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

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.