memcached I do not introduce more, that is, a distributed cache system! is a typical nosql.
Below I will use Spring AOP to implement a simple plug-in, to implement the annotation way, simple and convenient to get the cache
First, we have to define a annotation.
<strong>package Org.xiezhaodong.spring.annotation;import Java.lang.annotation.documented;import Java.lang.annotation.elementtype;import Java.lang.annotation.inherited;import java.lang.annotation.Retention; Import Java.lang.annotation.retentionpolicy;import java.lang.annotation.Target; @Documented @target ( Elementtype.method) @Retention (retentionpolicy.runtime) public @interface getcachevaule {public String key ();//key} </strong>
Because memcached has a lot of different clients, I will define an interface of my own. Let users do it themselves. Here is the interface, which defines some simple methods
Package org.xiezhaodong.spring.cache.cacheutil;/** * 2015-1-14 * @author Xiezhaodong * Cache interface, user self-implementation */public interface Cachesupport {Long cache_time=2*60*60;//default cache time is 2 hours/** * Add cache * @param key * @param value Value * @return Success */Boolean add Cache (String key,object value); /** * Add cache and set cache time * @param key * @param value value * @param time Cache times * @return Success */ Boolean ad Dcache (String key,object value,long cacheTime); /** * Delete Cache * @param key key * @return Success * /Boolean Deletecache (String key); /** * Get cached * @param key key * @return return value * /Object GetCache (String key); /** * Replace the corresponding values in the cache * @param key * @param value value * @return Replace Success * /Boolean replacecache (String K Ey,object value);}
Then there is the cut. The notes are very detailed inside .
Package Org.xiezhaodong.spring.cache.aop;import Java.lang.annotation.annotation;import Java.lang.reflect.Method; Import Java.util.map;import Java.util.concurrent.concurrenthashmap;import Org.apache.commons.logging.log;import Org.apache.commons.logging.logfactory;import Org.aspectj.lang.proceedingjoinpoint;import Org.aspectj.lang.reflect.methodsignature;import Org.springframework.core.localvariabletableparameternamediscoverer;import Org.springframework.core.parameternamediscoverer;import Org.xiezhaodong.spring.annotation.getcachevaule;import org.xiezhaodong.spring.cache.cacheutil.cachesupport;/** * AOP Facets class, actual function class * * @author Xiezhaodong * */public class Getcach EAOP {private static final log log = Logfactory.getlog (getcacheaop.class);p rivate cachesupport cachesupport;public void S Etcachesupport (Cachesupport cachesupport) {this.cachesupport = Cachesupport;} public Object Proxyinvoke (Proceedingjoinpoint pjp) {log.info ("Invoke Proxyinvoke Method"); object result = null;// Get the way to run the methods method = GetMethod (PJP); annotation[] annotations = method.getdeclaredannotations (); if (annotations.length = = 0) {//If no annotations exist, do not cache try {result = Pjp.proceed (); return result;} catch (Throwable e) {Log.warn ("your method" + method.getname () + "has some errors");}} --------------------------String CacheKey = Getcachekey (Pjp, method); result = Get_or_input_cache (PJP, result, CacheKey); return result;} /** * Get cached or found in the database and put into cache * @param PJP * @param result * @param cacheKey * @return */private Object Get_or_input_cache (Pro Ceedingjoinpoint PJP, Object result,string cacheKey) {if (CacheKey! = null) {result = Cachesupport.getcache (CacheKey); To the cache, modify the value of result if (result==null) {//If the cache is not inside, get result and cache to the cache service where try {result=pjp.proceed (); Cachesupport.addcache ( Cachekey,result); return result;} catch (Throwable e) {log.warn ("invoke Default");}} The return result;//cache exists, directly returning result}else{//if the annotation is not directly executed by the method try {result=pjp.proceed ();} catch (Throwable e) {Log.warn (" Invoke default ");}} return result;} /** * Get Cache key Value * * @paRam PJP * @param method * @return returns string */private string Getcachekey (Proceedingjoinpoint pjp, method) {if (metho D.isannotationpresent (Getcachevaule.class)) {//If there is the note, string key = Method.getannotation (Getcachevaule.class). Key () ;//Get the key value to cache object[] values = Pjp.getargs ();//Get order parameter value Parameternamediscoverer Discoverer = new Localvariabletableparameternamediscoverer (); string[] Names = Discoverer.getparameternames (method); map<string, integer> map = new concurrenthashmap<string, integer> (); for (int i = 0; i < names.length; i++) {Map.put (names[i], i);//Put the name and the corresponding sequence number into the hashmap}//to get the real key, the value, the try {Integer int_value = Map.get (key);//hash does not have a corresponding value, Indicates that the Getcachekey and name do not match if (int_value = = null) {Log.warn ("Your cachekey is not equals" + key+ "Please check this and change th Em ");} else {String cache_key_real = (string) values[int_value];//the true Cahe value of the key value to cache return cache_key_real;}} catch (Exception e) {Log.warn ("your filed" + key + "must be String.class");}} return null;} /** * Get run-time methodObject * * @param PJP * @return */private Method getmethod (proceedingjoinpoint pjp) {methodsignature methodsignature = (metho dsignature) pjp.getsignature (); method = Methodsignature.getmethod (); return method;}}
Then is the configuration of XML, note that the Cglib agent must be open
<bean id= "Aimpl" class= "Org.xie.service.Service_A_impl" ></bean><bean id= "Bimpl" class= " Org.xie.service.Service_B_impl "></bean><bean id=" Memdefault "class=" Org.xie.framework.memcached.spring.MemcachedDefault "><property name=" Cached "ref=" Memclient "></ Property></bean><bean id= "CACHEAOP" class= "Org.xie.framework.memcached.spring.GetCacheAop" >< Property Name= "Cachesupport" ref= "Memdefault" ></property></bean><aop:aspectj-autoproxy Proxy-target-class= "true"/> <!--cglib Agent open--><aop:config><aop:aspect id= "Myaspect" ref= "CacheAop "><aop:pointcut id=" businessservice "expression=" Execution (* org.xie.service.*.* (..)) "/><aop:around pointcut-ref= "Businessservice" method= "Proxyinvoke"/></aop:aspect></aop:config>
Now all the methods of the service layer have been proxied. So that's all we need to do, key means the key to be cached.
@Override @getcachevaule (key= "id")//means to cache idpublic Object TESTAOP (String ID) {return "sssss";}
In fact, there is no good to talk about, to talk about is in the code inside, you are pointing to it. But at present only realizes the drunk simple GetCache, later I will gradually improve the various APIs, please look forward to it!!
Reprint Please specify http://blog.csdn.net/a837199685
Spring Aop implements a simple memcached widget