Java ehcache distributed cache configuration instance

Source: Internet
Author: User

Reference: Introduction to http://www.open-open.com/lib//view/open1342696876495.html ehcache

Let's start with the project. [RMI mode];

Basic Environment: A creates two web projects respectively. C1 and C2 are respectively added to the jar package of echcache;

B In this example, two Tomcat servers are used to deploy C1 and C2 respectively.

Project configuration: C1 Configuration

A

Ehcache. xml [Path = SRC]

<Ehcache updatecheck = "false" dynamicconfig = "false">
<Diskstore Path = "Java. Io. tmpdir/ehcache"/>
<Cache name = "cache"
Maxelementsinmemory = "19"
Eternal = "false"
Timetoidleseconds = "120"
Timetoliveseconds = "120"
Overflowtodisk = "true">

<Cacheeventlistenerfactory
Class = "net. SF. ehcache. Distribution. rmicachereplicatorfactory"
Properties = "replicateasynchronously = true, replicateputs = true, replicateupdates = true, replicateupdatesviacopy = false, replicateremovals = true"/>

</Cache>

<Cachemanagerpeerlistenerfactory class = "net. SF. ehcache. Distribution. rmicachemanagerpeerlistenerfactory"
Properties = "hostname = 192.168.10.114, Port = 40001, sockettimeoutmillis = 2000"/>
<Cachemanagerpeerproviderfactory class = "net. SF. ehcache. Distribution. rmicachemanagerpeerproviderfactory"
Properties = "peerdiscovery = manual, hostname = 192.168.10.114, Port = 40002, rmiurls = // 192.168.10.114: 40001/cache | // 192.168.10.114: 40002/cache, timet
Oliv E = 32 "/>
<Defaultcache
Maxelementsinmemory = "1000"
Eternal = "false"
Timetoidleseconds = "120"
Timetoliveseconds = "120"
Overflowtodisk = "true"
/>

</Ehcache>

In this way, 1 is configured.92.1610.114: 40001 and 192.1610.114: 40002 two ehcache cache instances

Properties = "replicateasynchronously = true, replicateputs = true, replicateupdates = true, replicateupdatesviacopy = false, replicateremovals = true" These configuration policies
It can be synchronized to another cache to achieve distributed cache synchronization.

B web. xml

<Web-app xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xmlns = "http://java.sun.com/xml/ns/j2ee"
Xmlns: Web = "http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
Xsi: schemalocation = "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
Version = "2.4">

<Display-Name> ehcache cluster demo </display-Name>

<! -- Listeners -->
<Display-Name> ehcache cluster demo </display-Name>
<Context-param>
<Param-Name> eccache </param-Name>
<Param-value> ehcache. xml </param-value>
</Context-param>

<Welcome-file-List>
<Welcome-File> index. jsp </welcome-File>
</Welcome-file-List>

</Web-app>

C. Test. jsp

<% @ Page contenttype = "text/html; charset = UTF-8" Language = "Java" pageencoding = "UTF-8" %>
<% @ Page import = "net. SF. ehcache. cachemanager" %>
<% @ Page import = "net. SF. ehcache. element" %>
<% @ Page import = "net. SF. ehcache. cache" %>
<H1> ehcache cluster tester

<%
Cachemanager M = cachemanager. Create ();
Cache c = M. getcache ("cache ");
C. Put (new element ("999", "YY "));
Out. Print (C. getsize () + "_" + C. getkeys (). tostring ());
For (Object O: C. getkeys ()){
Object v = C. Get (O). getvalue ();
Out. println (O + ":" + V );
}
%>


C2 configurations are consistent with P1. The differences are as follows:

<Cachemanagerpeerlistenerfactory class = "net. SF. ehcache. Distribution. rmicachemanagerpeerlistenerfactory"
Properties = "hostname = 192.168.10.114, Port = 40002, sockettimeoutmillis = 2000"/>
<Cachemanagerpeerproviderfactory class = "net. SF. ehcache. Distribution. rmicachemanagerpeerproviderfactory"
Properties = "peerdiscovery = manual, hostname = 192.168.10.114, Port = 40001, rmiurls = // 192.168.10.114: 40002/cache | // 192.168.10.114: 40001/cache, timetolive = 32"/>


Custom text. JSP code.


Test method:

Start two projects: localhost: 8001/C1 and localhost: 8002/C2. C2 puts content into its cache for the first time. then, when you access C1, C1 reads the content directly from its own cache.

You can constantly change the code for testing.

* ** The above is only a simple practice for optimizing the parameter configuration to suit local conditions. How can this problem be solved. Thank you!

Zookeeper ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

The configuration of the exception hibernate + ehcahce is basically the same as the configuration of the hibernate practical ehcache. After the code and HbF configuration file can be specified to use the cache [setcached (true)]

Spring also provides the integration of ehcache, giving the application level the ability to cache the signature (key) of the call method, and the return result (value)

Ehcahe + terracotta may provide a complete solution [

Terracotta acquires ehcache and Terracotta provides JVM-level distributed cache]

These are post-scripts. We will continue to use them in the future.

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.