Ehcache Synchronous Cache Using Terracotta method

Source: Internet
Author: User

1. Download and install terracotta

http://www.terracotta.org/

2. Start Terracotta

Bin/start-tc-server.bat or bin/start-tc-server.sh

3. Code
Set Value class:

Package My.test.terracotta; Import Java.io.InputStream; Import My.test.ehcache1.EHCacheTest; Import Net.sf.ehcache.Cache; Import Net.sf.ehcache.CacheManager; Import net.sf.ehcache.Element; public class Testterracottaset {public Testterracottaset () {InputStream is = EHCacheTest.class.getResourceAsStream ("/ My/test/terracotta/ehcache.xml "); CacheManager CacheManager = new CacheManager (IS); Cache cache = Cachemanager.getcache ("Sampleterracottacache"); for (int i = 0; i < 1001 i++) {cache.put (New Element ("key" + I, "value" + i));} public static void Main (string[] args) throws Exception {new Testterracottaset ();}}

Get value class:

Package My.test.terracotta; Import Java.io.InputStream; Import My.test.ehcache1.EHCacheTest; Import Net.sf.ehcache.Cache; Import Net.sf.ehcache.CacheManager; Import net.sf.ehcache.Element; public class Testterracottaget {public Testterracottaget () {InputStream is = EHCacheTest.class.getResourceAsStream ("/ My/test/terracotta/ehcache.xml "); CacheManager CacheManager = new CacheManager (IS); Cache cache = Cachemanager.getcache ("Sampleterracottacache"); for (int i = 0; i < 1001 i++) {Element elem = cache.get ("key" + i); if (Elem = null) {System.out.println ("key" + I + "not Found"); else {System.out.println ("key" + i + "=" + Elem.getvalue ());} public static void Main (string[] args) throws Exception {new Testterracottaget ();}}

Profiles: Modifying an official sample profile, adding

<terracottaconfig url= "localhost:9510"/>

<cache name= "Sampleterracottacache" maxelementsinmemory= "1000" eternal= "false" timetoidleseconds= "3600" timetoliveseconds= "1800" overflowtodisk= "false" > <terracotta/> </cache>

This is to use the terracotta server for cache synchronization

4. Run

(1) Run the Testterracottaset class first

(2) Run the Testterracottaget class again

(3) Operation result:

Key0=value0
Key1=value1
Key2=value2
Key3=value3
Key4=value4
Key5=value5
Key6=value6
Key7=value7
Key8=value8
Key9=value9
Key10=value10
......

key1000=value1000

The cache situation can be viewed through the Terrocotta console, as shown in figure:

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.