After the holiday, I wish you a happy Dragon Boat Festival. It rained today and I did nothing to study ehcache. After checking it on the ehcache official website, I looked at other people's blogs on the internet, without the detailed usage of ehcache, so I tried it myself. The first time I used ehcache, it may be incorrect. I hope you can help me correct it and make progress together.
I use struts2 + ehcache. The Code is as follows:
1. index. jsp
<% @ Page Language = "Java" Import = "Java. util. * "pageencoding =" UTF-8 "%> <br/> <% <br/> string Path = request. getcontextpath (); <br/> string basepath = request. getscheme () + ": //" + request. getservername () + ":" + request. getserverport () + path + "/"; <br/>%> </P> <p> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en"> <br/> <HTML> <br/> <pead> <br/> <base href = "<% = basepath %>"> </P> <p> <title> my JSP 'index. JSP 'starting page </title> <br/> <meta http-equiv = "Pragma" content = "no-Cache"> <br/> <meta http-equiv =" cache-control "content =" no-Cache "> <br/> <meta http-equiv =" expires "content =" 0 "> <br/> <meta http-equiv = "keywords" content = "keyword1, keyword2, keyword3 "> <Br/> <meta http-equiv = "Description" content = "this is my page"> <br/> <! -- <Br/> <LINK rel = "stylesheet" type = "text/CSS" href = "styles.css" mce_href = "styles.css"> <br/> --> <br/> </pead> </P> <p> <body> <br/> <a href = "colpartition tion. action "mce_href =" colpartition tion. action "> View All colors </a> <br/> </body> <br/> </ptml> <br/>
2. colexception. Java
Package COM. ehcache. servlet; </P> <p> Import Java. AWT. color; <br/> Import Java. util. arraylist; <br/> Import Java. util. list; <br/> Import Java. util. map; </P> <p> Import net. SF. ehcache. cache; <br/> Import net. SF. ehcache. cachemanager; <br/> Import net. SF. ehcache. element; <br/> Import COM. ehcache. database. colordatabase; <br/> Import COM. ehcache. util. ehcacheutil; <br/> Import COM. opensymphony. xwork2.actionsupport; </P> <p> @ suppresswarnings ("serial ") <br/> public class colpartition tion extends actionsupport {</P> <p> private list <string> List = NULL; </P> <p> @ suppresswarnings ("unchecked ") <br/> Public String allcolor () throws exception {<br/> // obtain cachemanager <br/> cachemanager = ehcacheutil. getcachemanager (); </P> <p> List = new arraylist <string> (); </P> <p> // use the colorcache configured in the configuration file to create a cache <br/> cache = cachemanager. getcache ("colorcache"); </P> <p> // check whether allcolor cache exists. <br/> element = cache. get ("allcolor"); </P> <p> // query from the database if it does not exist <br/> If (element = NULL) {<br/> Map <string, color> maps = colordatabase. getallcolor (); <br/> JAVA. util. iterator <string> iter = maps. keyset (). iterator (); <br/> while (ITER. hasnext () {<br/> color = maps. get (ITER. next (); <br/> list. add (color. tostring (); <br/>}< br/> cache. put (new element ("allcolor", list); <br/>} else {// If yes, load <br/> List = (list <string>) element from the cache. getvalue (); <br/>}< br/> return "allcolor"; <br/>}</P> <p> public list <string> getlist () {<br/> return list; <br/>}</P> <p> Public void setlist (list <string> List) {<br/> This. list = List; <br/>}</P> <p >}< br/>
3. colordatabase. Java replaces the database.
Package COM. ehcache. database; </P> <p> Import Java. AWT. color; <br/> Import Java. util. hashmap; <br/> Import Java. util. map; </P> <p>/** <br/> * modified in the sample of samples/colorcache in the ehcache-2.1.0, which replaces the database. <Br/> */<br/> public class colordatabase {</P> <p> Private Static Map <string, color> colormap = new hashmap <string, color> (); <br/> static {<br/> colormap. put ("red", color. red); <br/> colormap. put ("blue", color. blue); <br/> colormap. put ("green", color. green); <br/> colormap. put ("white", color. white); <br/> colormap. put ("black", color. black); <br/> colormap. put ("lightgray", color. lightgray); <br/> colormap. put ("gray", color. gray); <br/> colormap. put ("darkgray", color. darkgray); <br/> colormap. put ("pink", color. pink); <br/> colormap. put ("orange", color. orange); <br/> colormap. put ("yellow", color. yellow); <br/> colormap. put ("Magenta", color. magenta); <br/> colormap. put ("cyan", color. cyan); </P> <p >}</P> <p> Public static Map <string, color> getallcolor () {<br/> return colormap; <br/>}< br/>
4. allcolor. jsp
<% @ Page Language = "Java" Import = "Java. util. * "pageencoding =" UTF-8 "%> <br/> <% @ taglib prefix =" S "uri ="/Struts-tags "%> <br/> <% <br /> string Path = request. getcontextpath (); <br/> string basepath = request. getscheme () + ": //" + request. getservername () + ":" + request. getserverport () + path + "/"; <br/>%> </P> <p> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en"> <br/> <HTML> <br/> <pead> <br/> <base href = "<% = basepath %>"> </P> <p> <title> my JSP 'allcolor. JSP 'starting page </title> </P> <p> <meta http-equiv = "Pragma" content = "no-Cache"> <br/> <meta http- equiv = "cache-control" content = "no-Cache"> <br/> <meta http-equiv = "expires" content = "0"> <br/> <Meta HTTP-equiv = "keywords" content = "keyword1, keyword2, keyword 3 "> <br/> <meta http-equiv =" Description "content =" this is my page "> <br/> <! -- <Br/> <LINK rel = "stylesheet" type = "text/CSS" href = "styles.css" mce_href = "styles.css"> <br/> --> </P> <p> </pead> </P> <p> <body> <br/> <s: iterator value = "list"> <br/> <s: property/> <br/> </s: iterator> <br/> </body> <br/> </ptml> <br/>
5. ehcache. xml
<Ehcache> <br/> <diskstore Path = "Java. io. tmpdir "/> <br/> <defaultcache maxelementsinmemory =" 10000 "Eternal =" false "<br/> timetoidleseconds =" 120 "timetoliveseconds =" 120 "overflowtodisk =" true "/> </P> <p> <cache name = "colorcache" maxelementsinmemory = "10000" Eternal = "false" <br/> timetoidleseconds = "300" timetoliveseconds = "600" overflowtodisk = "true"/> <br/> </ehcache> <br/>
6. Struts. xml
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <! Doctype struts Public <br/> "-// Apache Software Foundation/DTD struts configuration 2.0/EN" <br/> "http://struts.apache.org/dtds/struts-2.0.dtd"> </P> <p> <struts> </P> <p> <package name = "struts" extends = "struts-Default"> </P> <p> <action name = "colpartition tion" class = "com. ehcache. servlet. colequaltion "method =" allcolor "> <br/> <result name =" allcolor ">/allcolor. JSP </result> <br/> </Action> <br/> </package> <br/> </struts> <br/>
7. ehcacheutil. Java
Package COM. ehcache. util; </P> <p> Import net. SF. ehcache. cachemanager; </P> <p> public class ehcacheutil {</P> <p> private ehcacheutil () {}</P> <p> Private Static cachemanager = NULL; </P> <p> static {</P> <p> cachemanager = cachemanager. create (); <br/>}</P> <p> Public static cachemanager getcachemanager () {<br/> return cachemanager; <br/>}</P> <p >}< br/>