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: