These days, I designed the SNA architecture and got in touch with some remote caching, cluster, session replication, and other things.EnterpriseI don't feel very useful when using the application. Now the design is very useful for the Internet system architecture, and the pressure is shown after debugging.TestThe situation is still quite good.
4uv ^ m * B. W (D0 has been thinking a lot about cache selection, althoughMemcachedIntegrationJavaThe performance in serialization is not good, but there is no better cache solution in the cluster environment, so we chose memcached. We planned to install servers purchased by other companies in place.LinuxI will study memcached again, but I foundWindowsIn the memcached version, you can start to adjust the existing framework.
WindowsServerEasy to useInstall, Double-click the default service port 11211 after running, and did not try to change the port, because the unix version will be used later, and then record the installation steps. The interface for downloading the client's Java API package is very simple. There is a ready-made example in the API manual.
Objective: to transform the cache part of the old framework:
1. cache Tool
2. hibernate provider
3. Use cache to implement the session mechanism
Today, we will first study the transformation of the cache tool class. In the old framework, some functions use ehcache to cache the execution results. The goal is to provide a cache tool class, configure the cache (memcached or ehcached) in the configuration file so that other programs do not depend on the specific cache, and use AOP to cache the method execution result.
First, implement the tool class:
Configure in spring
<! -- Ehcache manager --> <br/> <bean id = "cachemanager" <br/> class = "org. springframework. cache. ehcache. ehcachemanagerfactorybean "> <br/> <property name =" configlocation "> <br/> <value> classpath: ehcache. XML </value> <br/> </property> <br/> </bean> </P> <p> <bean id = "localcache" <br/> class = "org. springframework. cache. ehcache. ehcachefactorybean "> <br/> <property name =" cachemanager "ref =" cachemanager "/> <br/> <Property name = "cachename" <br/> value = "×××. cache. local_cache "/> <br/> </bean> </P> <p> <bean id =" cacheservice "<br/> class =" ×××. core. cache. cacheservice "init-method =" init "Destroy-method =" destory "> <br/> <property name =" cacheserverlist "value =" $ {cache. servers} "/> <br/> <property name =" cacheserverweights "value =" $ {cache. cacheserverweights} "/> <br/> <property name =" cachecluster "value =" $ {cache. cluste R} "/> <br/> <property name =" localcache "ref =" localcache "/> <br/> </bean> <br/> <! -- Ehcache manager --> <br/> <bean id = "cachemanager" <br/> class = "org. springframework. cache. ehcache. ehcachemanagerfactorybean "> <br/> <property name =" configlocation "> <br/> <value> classpath: ehcache. XML </value> <br/> </property> <br/> </bean> <br/> <bean id = "localcache" <br/> class = "org. springframework. cache. ehcache. ehcachefactorybean "> <br/> <property name =" cachemanager "ref =" cachemanager "/> <br/> <property name =" cachename "<br/> value =" × ××. cache. local_cache "/> <br/> </bean> </P> <p> <bean id =" cacheservice "<br/> class =" ×××. core. cache. cacheservice "init-method =" init "Destroy-method =" destory "> <br/> <property name =" cacheserverlist "value =" $ {cache. servers} "/> <br/> <property name =" cacheserverweights "value =" $ {cache. cacheserverweights} "/> <br/> <property name =" cachecluster "value =" $ {cache. cluster} "/> <br/> <property name =" localcache "ref =" localcache "/> <br/> </bean> Configure $ {cache in the properties file. servers }$ {cache. cacheserverweights }$ {cache. cluster}
Java
Code /** <Br/> * @ author Marc <br/> */<br/> public class cacheservice {<br/> private log logger = logfactory. getlog (getclass (); </P> <p> private cache localcache; </P> <p> string cacheserverlist; </P> <p> string cacheserverweights; </P> <p> Boolean cachecluster = false; </P> <p> int initialconnections = 10; </P> <p> int minspareconnections = 5; </P> <p> int maxspareconnections = 50; </P> <p> long maxi Dletime = 1000*60*30; // 30 minutes </P> <p> long maxbusytime = 1000*60*5; // 5 minutes </P> <p> long maintthreadsleep = 1000*5; // 5 seconds </P> <p> int sockettimeout = 1000*3; // 3 seconds to block on reads </P> <p> int socketconnectto = 1000*3; // 3 seconds to block on initial <br/> // connections. if 0, then will use blocking <br/> // connect (default) </P> <p> Boolean failover = Fal Se; // turn off auto-failover in event of server <br/> // down </P> <p> Boolean naglealg = false; // turn off Nagle's algorithm on all sockets in <br/> // pool </P> <p> memcachedclient MC; </P> <p> Public cacheservice () {<br/> MC = new memcachedclient (); <br/> MC. setcompressenable (false ); <br/>}< br/>/** <br/> * put in <br/> */<br/> Public void put (string key, object OBJ) {<br/> assert. hastex T (key); <br/> assert. notnull (OBJ); <br/> assert. notnull (localcache); <br/> If (this. cachecluster) {<br/> MC. set (key, OBJ); <br/>}else {<br/> element = new element (Key, (serializable) OBJ); <br/> localcache. put (element ); <br/>}< br/>/** <br/> * Delete <br/> */<br/> Public void remove (string key) {<br/> assert. hastext (key); <br/> assert. notnull (localcache); <br/> If (this. cacheclu Ster) {<br/> MC. delete (key); <br/>}else {<br/> localcache. remove (key ); <br/>}< br/>/** <br/> * Get <br/> */<br/> Public object get (string key) {<br/> assert. hastext (key); <br/> assert. notnull (localcache); <br/> Object RT = NULL; <br/> If (this. cachecluster) {<br/> RT = MC. get (key); <br/>}else {<br/> element = NULL; <br/> try {<br/> element = localcache. get (key); <br/>} c Atch (cacheexception) {<br/> throw new dataretrievalfailureexception ("cache failure:" <br/> + cacheexception. getmessage (); <br/>}< br/> If (element! = NULL) <br/> RT = element. getvalue (); <br/>}< br/> return RT; <br/>}< br/>/** <br/> * determine whether the existence exists <br/> */<br/> Public Boolean exist (string key) {<br/> assert. hastext (key); <br/> assert. notnull (localcache); <br/> If (this. cachecluster) {<br/> return MC. keyexists (key); <br/>}else {<br/> return this. localcache. iskeyincache (key); <br/>}< br/> private void Init () {<br/> If (this. CAC Hecluster) {<br/> string [] serverlist = cacheserverlist. split (","); <br/> integer [] weights = This. split (cacheserverweights); <br/> // initialize the pool for memcache servers <br/> sockiopool pool = sockiopool. getinstance (); <br/> pool. setservers (serverlist); <br/> pool. setweights (weights); <br/> pool. setinitconn (initialconnections); <br/> pool. setminconn (minspareconnections); <br/> pool. setmaxc ONN (maxspareconnections); <br/> pool. setmaxidle (maxidletime); <br/> pool. setmaxbusytime (maxbusytime); <br/> pool. setmaintsleep (maintthreadsleep); <br/> pool. setsocketto (sockettimeout); <br/> pool. setsocketconnectto (socketconnectto); <br/> pool. setnagle (naglealg); <br/> pool. sethashingalg (sockiopool. new_compat_hash); <br/> pool. initialize (); <br/> logger.info ("initialize memcached pool! "); <Br/>}</P> <p> private void destory () {<br/> If (this. cachecluster) {<br/> sockiopool. getinstance (). shutdown (); <br/>}< br/>}AOP interception class Public class cachinginterceptor implements methodinterceptor {</P> <p> private cacheservice; <br/> private string cachekey; </P> <p> Public void setcachekey (string cachekey) {<br/> This. cachekey = cachekey; <br/>}</P> <p> Public void setcacheservice (cacheservice) {<br/> This. cacheservice = cacheservice; <br/>}</P> <p> Public object invoke (methodinvocation Invocation) throws throwable {<br/> Object result = cacheservice. get (cachekey); <br/> // if the result returned by the function is not in the cache, execute the function and put the result into the cache. <br/> If (result = NULL) {<br/> result = invocation. proceed (); <br/> cacheservice. put (cachekey, result); <br/>}< br/> return result; <br/>}< br/> public class cachinginterceptor implements methodinterceptor {<br/> private cacheservice; <br/> private string cachekey; <br/> Public void setcachekey (string cachekey) {<br/> This. cachekey = cachekey; <br/>}< br/> Public void setcacheservice (cacheservice) {<br/> This. cacheservice = cacheservice; <br/>}< br/> Public object invoke (methodinvocation Invocation) throws throwable {<br/> Object result = cacheservice. get (cachekey); <br/> // if the result returned by the function is not in the cache, execute the function and put the result into the cache. <br/> If (result = NULL) {<br/> result = invocation. proceed (); <br/> cacheservice. put (cachekey, result); <br/>}< br/> return result; <br/>}< br/>}The configuration of Spring AOP is as follows: <AOP: config proxy-target-class = "true"> <br/> <AOP: Advisor <br/> pointcut = "execution. assumervice. getone (..)) "<br/> advice-ref =" assumervicecachingadvice "/> <br/> </AOP: config> </P> <p> <bean id = "baspoiservicecachingadvice" <br/> class = "×××. core. cache. cachinginterceptor "> <br/> <property name =" cachekey "value =" assumervice "/> <br/> <property name =" cacheservice "ref =" cacheservice "/> <br/> </bean> <br/>Http://space.itpub.net/583467/viewspace-614201