Since the previous article has introduced the memcached cache, and cluster deployment, here we do not introduce, we directly introduce memcached and Spring AOP to build a distributed database front-end caching framework
A. Java Implement Memcached client, Implementing distributed
(1) Need for Jar
1)Commons-pool-1.5.6.jar
2)Java_memcached-release_2.6.3.jar
3)Slf4j-api-1.6.1.jar
4)Slf4j-simple-1.6.1.jar
(2) J Ava implements the Memcached client code as follows:
Import Com.danga.memcached.memcachedclient;import Com.danga.memcached.sockiopool;public class TestCache {/** * @param args */public static void main (string[] args) { string[] servers = {"192.168.74.129:12000,192.168.74.130:13000"};< C1/>sockiopool pool = sockiopool.getinstance (); Pool.setservers (servers); Pool.setfailover (true); Pool.setinitconn (ten); Pool.setminconn (5); Pool.setmaxconn (+); Pool.setmaintsleep (+); Pool.setnagle (false); Pool.setsocketto (+); Pool.setalivecheck (true); Pool.initialize (); Memcachedclient memcachedclient = new Memcachedclient (); Boolean success = Memcachedclient.set ("Zcy", "DataValue"); SYSTEM.OUT.PRINTLN (success); Object obj= memcachedclient.get ("Zcy"); System.out.println (obj);}}
Two.
Memcached and Spring AOP Build a database front-end caching framework and use Maven to manage projects
(1) Let's brush up on Spring AOP
AOP(Aspect Orient Programming) is programming for facets , which can be used in transaction management, security checks, caches, object pool management, and so on. One of the main aspects of aspect-oriented programming is the Advice, the Advice defined pointcut where the connection point executes the enhanced code or the connection point executes before executing the enhanced code.
(2) We use Maven to manage springmvc jar and jar of Memcached
We will introduce Memcached jar, add Memcached corresponding jar package in Pom.xml
<dependency> <groupId>com.danga</groupId> <artifactid>java-memcached</ artifactid> <version>2.6.3</version> </dependency> <dependency> < groupid>commons-pool</groupid> <artifactId>commons-pool</artifactId> <version >1.5.6</version> </dependency> <dependency> <groupid>org.slf4j</ groupid> <artifactId>slf4j-simple</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>org.slf4j</groupId> < artifactid>slf4j-api</artifactid> <version>1.6.1</version> </dependency>
(3) using Spring to manage The configuration and implementation of Memcached connection information
1)global.properties file
The number of connections to each server that were established when the memcached.server=192.168.74.129:12000memcached.server2=192.168.74.130:13000# was initialized #服务器地址 memcached.initconn=20# each server establishes the minimum number of connections memcached.minconn=10# each server establishes the maximum number of connections memcached.maxconn=50# Self-examination thread cycle to work, its every sleep time memcached.maintsleep=3000#socket parameters, if true in writing data is not buffered, immediately send out memcached.nagle=false# Timeout for socket blocking read Data memcached.socketto=3000memcached.alivecheck=truememcached.failover=true
2) Spring configuration file ( Applicationcontext-memached.xml
<!--injection Properties file--><bean id= "Propertyconfigurer" class= " Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer "><property name=" Locations "> <list><value>classpath:global.properties</value></list></property></bean> <bean id= "Memcachedpool" class= "Com.danga.MemCached.SockIOPool" factory-method= "getinstance" init-method= " Initialize "destroy-method=" ShutDown "> <constructor-arg> <value>memcachedpool</value> ; </constructor-arg> <property name= "Servers" > <list> <valu E>${memcached.server}</value> <value>${memcached.server2}</value> </li st> </property> <property name= "Initconn" > <value>${memcached.initConn}< /value> </property> <property name= "Minconn" > <value>${memcached.minconn}</value> </property> <property name= "Maxconn" > <value>${me mcached.maxconn}</value> </property> <property name= "Maintsleep" > <value> ${memcached.maintsleep}</value> </property> <property name= "Nagle" > <value& gt;${memcached.nagle}</value> </property> <property name= "Socketto" > <value >${memcached.socketTO}</value> </property> <property name= "Alivecheck" > < ;value>${memcached.alivecheck}</value> </property> <property name= "Failover" > <value>${memcached.failover}</value> </property> </bean><bean id= "MemCache Dclient "class=" com.danga.MemCached.MemCachedClient "> <constructor-arg> <span style=" color:# ff0000; " ><value>memcachedpool</value></span> </constructor-arg> </bean>
3) JUnit Test This configuration is correct, the code is as follows:
Import Org.junit.before;import Org.junit.test;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;import com.danga.MemCached.MemCachedClient; public class Memcachedservicetest {private Memcachedclient memcachedclient, @Before public void Beforetest () { ApplicationContext ATX = new Classpathxmlapplicationcontext ("Classpath:applicationcontext-memached.xml"); Memcachedclient = (memcachedclient) atx.getbean ("Memcachedclient"); } @Test public void MemCached () {Boolean b=memcachedclient.set ("Hello", "DataValue"); System.out.println (b); Object obj=memcachedclient.get ("Hello"); System.out.println (obj);}}
Able to save and get Data Description configuration Success
(4) implementing MVC business logic
DAO layer @service ("Datadao") public class Datadaoimpl implements Datadao {@Overridepublic string getData (string name) { Return name+ "Get Data";}} Server tier @Service ("DataService") public class Dataserviceimpl implements dataservice{@Autowiredprivate Datadao Datadao;public string Finddatabyparams (string name) {return datadao.getdata (name);}} Controller layer @controllerpublic class Indexcontroller {@Autowiredprivate DataService DataService; @RequestMapping ("/ Index ") public string index (model model, @RequestParam (value=" name ") string name) {string datavalue= Dataservice.finddatabyparams (name); System.out.println (DataValue); return "";}}
(5)Spring AOP and Memcached Building a database front-end caching framework
1) Implementing The logical code for AOP
@Service ("Datainterceptor") public class Datainterceptor implements methodinterceptor{@Autowiredprivate Memcachedclient memcachedclient; @Overridepublic Object invoke (Methodinvocation invocation) throws Throwable {object[] args = Invocation.getarguments (); String param=args[0].tostring (); Object object = null; if (param!=null&&memcachedclient!=null) { object=memcachedclient.get (param); System.out.println ("Executes the value obtained from memcached =======" +object); if (object==null) { object =invocation.proceed (); SYSTEM.OUT.PRINTLN ("Execute database operation gets the value =======" +object); if (Object! = null) {Boolean b=memcachedclient.set (Param,object); (b) {System.out.println ("===== Save Value" +object+ "to memcached success ===========");} else{System.out.println ("===== Save Value" +object+ "to memcached failed ===========");}} System.out.println ("Not getting =========== from memcached"); } else{ System.out.println ("Get ======= from memcached" +object); return object;}}
2) Spring configuration file configuration AOP
<bean id= "Datainterceptoradvisor" class= "Org.springframework.aop.support.NameMatchMethodPointcutAdvisor" > <property name= "Mappedname" ><value>getData</value></property> <property name= " Advice "ref=" Datainterceptor "/></bean><bean class=" Org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator "><property name=" Beannames "value=" Datadao "/><property name=" Interceptornames "value=" Datainterceptoradvisor "/></bean>
Configuring which method to intercept, here we intercept the getData method
(6) testing Spring AOP and Memcached Building a database front-end caching framework
1) The first execution of our parameter Hello,Memcached inside certainly not, because is the first execution, shown from the database to obtain the corresponding value, and save to Memcached service side,:
2) Execute the second time, the parameter is Hello, this time not to operate the database, directly from the Memcached server to obtain the corresponding value
3) We're doing a cluster deployment on the Memcached server, so let's take a look at the data saved to one of them:
Three Building memcached and Spring AOP to build a database front-end caching framework error
Error:com.schooner.memcached.schoonersockiopool-attempting to get Sockio from uninitialized pool!
The reason is that we set memcachedclient and sockiopool proxname not the same cause, so two names to be the same:
Four. Summary
in the previous article, we introduced the memcached cluster deployment, with two memcached.server=192.168.74.129:12000 and memcached.server2= 192.168.74.130:13000, we implement the distributed cache through the memcached client, the memcached server is unable to communicate between the server, as shown by the Memcached client implementation of the distributed cache.
Memcached and Spring AOP build number distributed data base front-end caching framework