The use of Ehcache's CacheManager

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. 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

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.