このペーパーの主な研究は、Springのインターセプターを使用してカスタムキャッシュ実装を使用することであり、特定の実装コードは次のとおりです。
Memcachedは、データベースの負荷を削減するためのダイナミックWebアプリケーション用の高性能分散メモリオブジェクトキャッシュシステムです。メモリ内のキャッシュデータとオブジェクトによってデータベースが読み取られる回数を削減し、それにより、ダイナミックのデータベース駆動型Webサイトの速度が向上します。この記事では、MemcachedインスタンスとSpringインターセプターを使用して、キャッシュのカスタマイズを実装しています。インターセプターを使用して、カスタムキャッシュタグとキー値生成戦略を読み取ります。
パッケージcom.jeex.sci; @target(elementtype.method)@retention(retentionpolicy.runtime)@inherited @documented public @interface @interface cachable {string namespace(); string key(); default ""; int [] keyargs()default {}; intcproperties [] defirtis() "期限切れ()デフォルト1800;}パッケージcom.jeex.sci; @target(elementtype.method)@retention(retentionpolicy.runtime)@inherited @documented public @interface cacheevict {string namespace(); string key()default ";パブリックオブジェクトInvoke(MethodInvocation Invoction)Throws Throws {Method Method = Invoction.getMethod(); cachable c = method.getAnnotation(cachable.class); if(c!= null){return handlecachaible(invoction、method、c);} cacheevict ce = null.classion( Handlecacheevict(invoction、CE);} return Injection.proceed();}プライベートオブジェクトハンドレカブル(MethodInvocation Invoction、Method Method、Cachable C)Throws Throws {string key = getKey(invoction、keyinfo.fromcachaible(c)); if(key.equals( "")){if(log.isdebugenabled()){log.warn(ematy cache kache is " + method); injection.proceed();} long nstag =(long)memcachedget(c.namespace()); if(nstag == null){nstag = long.valueof(system.currenttimemillis()); memcachedset(c.namespace()、24*3600、long.valueof(nstag); makememcachedkey(c.namespace()、nstag、key); object o = null; o = memcachedget(key); if(o!= null){if(log.isdebugenabled()){log.debug(log.debug( "キャッシュヒット:キャッシュキー=" + key); {log.debug( "Cache Miss:Cache key =" + key);} o = invoction.proceed(); memcachedset(key、c.expires()、o);} return o;}プライベートオブジェクトHandleCacheevict(MethodInvocation Invoction、Cacheevict CE)スロー可能{string key = getKey(Invoction、keyInfo.fromCacheevict(CE)); if(key.equals( "")){if(log.isdebugenabled()){log.debug( "evicting" + ce.namespace()); } memcacheddelete(ce.namespace()); } else {long nstag =(long)memcachedget(ce.namespace()); if(nstag!= null){key = makememcachedkey(ce.namespace()、nstag、key); if(log.isdebugenabled()){log.debug( "evicting" + key); } memcacheddelete(key); }} return injection.proceed(); }//メソッドにインターセプトされたパラメーターを使用してパラメーターを生成しますプライベート文字列getKeyWithargs(object [] args、int [] argindex){stringbuilder key = new StringBuilder(); boolean first = true; for(int index:argindex){if(index <0 || index> = args.length){throw new IllegalargumentException( "index out out out bound"); } if(!first){key.append( ':'); } else {first = false; } key = key.append(args [index]); } return key.toString(); }private string getKeyWithProperties(Object O、String Props [])スロー例外{stringbuilder key = new StringBuilder(); boolean first = true; for(string prop:props){// beanのプロパティをメソッド文字列methodname = "get" + prop.substring(0、1).touppercase() + prop.substring(1);方法m = o.getClass()。getMethod(methodName);オブジェクトr = m.invoke(o、(object [])null); if(!first){key.append( ':'); } else {first = false; } key = key.append(r); } return key.toString(); }//ジェネレーターを使用して、キープライベート文字列getKeyWithGenerator(MethodInvocation Injection、String keygenerator)を生成します。 cachekeygenerator ikg =(cachekeygenerator)ckg.newinstance(); return ikg.generate(invoction.getarguments()); }
private static class keyInfo {string key; int [] keyargs; string keyproperties []; string keygenerator; static keyinfo fromcachable(cachable c){keyinfo ki = new keyinfo(); ki.key = c.key(); ki.keyargs = c.keyargs(); ki.keygenerator c.keygenerator(); ki.keyproperties = c.keyproperties(); return ki;} static keyinfo fromcacheevict(cacheevict ce){keyinfo ki = new keyinfo(); ki.key = ce.key(); ki.keyargs = ke.keyargs(); ki.keyargs(); ce.keygenerator(); ki.keyproperties = ce.keyproperties(); return ki;} string key(){return key;} int [] keyargs(){return keyargs;} string [] keyproperties(){return keyproperties;} string keynerator(){return keynerator;}}}//パラメーターでキーを設定@cachable(namespace = "blacklist"、keyArgs = {0、1})public int ateanmethond(int a、int b){return 100; }パッケージcom.jeex.sci.test; Import net.spy.memcached.memcachedclient; Import org.junit.test; import org.springframework.context.applicationcontext; Import org.springframework.context.support.filesyxmlaptlicationcontext; public bid; public class(Public Class) args [])arturtedexception {applicationContext ctx = new FilessystemxmlapplicationContext( "/src/test/resources/beans.xml"); memcachedClient MC =(memcachedClient)ctx.getbean( "memcachedClient") (blacklistdaoimpl)ctx.getbean( "blacklistdaoimpl"); while(true) {system.out.println( "################################################################################################################################################################################################################################# ############################################################################################################################################################################################ ############################################################################################################################################################################################ ############################################################################################################################################################################################################################# ########################################### "); Mc.Flush(); BlackListquery Query = new BlackListquery(1、" 222.231.23.13 "); = new BlackListquery(1、 "123.231.23.14"); dao.anothermethond(333、 444); dao.searchblacklistcount2(query2); dao.searchblacklistcount3(query2); dao.evict(query); dao.searchblacklistcount2(query); dao.evictall(); dao.evictall();上記は、Springのインターセプターを使用したカスタムキャッシュの実装例コードに関するこの記事の全体的な内容です。すべての人に役立つことを願っています。興味のある友人は、このサイトの他の関連トピックを引き続き参照できます。欠点がある場合は、それを指摘するためにメッセージを残してください。このサイトへのご支援をありがとうございました!