이 논문의 주요 연구는 Spring의 인터셉터를 사용자 정의 캐시 구현에 사용하는 것이며 특정 구현 코드는 다음과 같습니다.
Memcached는 데이터베이스로드를 줄이기 위해 동적 웹 응용 프로그램을위한 고성능 분산 메모리 객체 캐싱 시스템입니다. 메모리의 캐시 된 데이터 및 객체에 의해 데이터베이스를 읽는 횟수를 줄여 동적 인 데이터베이스 구동 웹 사이트의 속도가 높아집니다. 이 기사는 MemCached 인스턴스 및 스프링 인터셉터를 사용하여 캐시 사용자 정의를 구현합니다. 인터셉터를 사용하여 사용자 정의 캐시 태그 및 키 값 생성 전략을 읽으십시오.
package com.jeex.sci; @target (elementtype.method) @retention (retentionpolicy.runtime) @inherited public @interface cachable {string namespace (); string key () default ""; int [] keyargs () default {}; String [] keyproperties [) default {); 만료 () 기본값 1800;} package com.jeex.sci; @target (elementtype.method) @retention (retentionpolicy.runtime) @inherited public @interface cacheevict {string namespace (); string key () default "; keyargs () default {) keyproperties [) default {) default {) "";} 공개 객체 호출 (MethodInvocation invoction)은 던질 수있는 {method method = invoction.getMethod (); 캐시 가능한 c = method.getAntotation (cachable.class); if (c! = null) {return handlecachable (return handlecachable (cacheevict, c);} cacheevict ce = methodAntantation (cacheevict.) HandleCacheEvict (Invoction, CE);} return injection.proceed ();} 개인 객체 핸드 레드 캐시 가능 (MethodInvocation invoction, Method Method, Cachable C) 던지기 가능 {String key = getKey (invoction, keyInfo.fromCachable (c)); if (key.equals ( "")) {if (log.isdebugenabled ()) {log.warn ( "empty cache key, method is is" + method); nstag = (long) memcachedget (c.namespace ()); if (nstag == null) {nstag = long.valueof (System.currentTimeMillis ()); memcachedSet (), 24*3600, long.valueof (nstag); 키); 개체 o = null; o = memcachedget (key); if (o! = null) {if (log.isdebugenabled ()) {log.debug ( "캐시 히트 : 캐시 key =" + key);}} else {if (log.isdebugenabled ()) {log.debug ( "cache miss : cache miss : cache miss : cache miss : cache miss 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 = makemcachedkey (ce.namespace (), nstag, key); if (log.isdebugenabled ()) {log.debug ( "Evicting" + key); } memcachedDelete (키); }} return injection.proceed (); } // 메소드를 가로채는 매개 변수를 사용하여 매개 변수를 생성합니다. 비공개 문자열 getKeyWitHargs (Object [] args, int [] argindex) {StringBuilder key = new StringBuilder (); 부울 첫 번째 = 참; for (int index : argindex) {if (index <0 || index> = args.length) {new new ImperalArgumentException ( "index of bound"); } if (! first) {key.append ( ':'); } else {first = false; } key = key.append (args [index]); } return key.toString (); } 개인 문자열 getKeyWithProperTies (Object O, String Propss [])은 예외 {StringBuilder key = new StringBuilder (); 부울 첫 번째 = 참; for (String prop : props) {// 콩의 속성을 메소드 문자열 이름으로 변환하십시오 MethodName = "get" + prop.SubString (0, 1) .toupperCase () + prop.Substring (1); 메소드 m = O.getClass (). getMethod (MethodName); Object 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)를 생성합니다. 예외 {class <?> ckg = class.forname (keyGenerator); CACHEKEYGENERATOR IKG = (CACHEYKEYGENERATOR) CKG.NEWINSTANCE (); return ikg.generate (invoction.getarguments ()); } 개인 정적 클래스 keyInfo {문자열 키; int [] keyArgs; String keyProperties []; String keygenerator; 정적 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;} 정적 keyInfo fromCacheevict (cacheevict ce) {keyInfo ki = new keyInfo (); ki.key = ce.key (); ki.keyargs = ce.keyargs (); ki.keygenerator =. ce.keyGenerator (); ki.keyProperties = ce.keyProperties (); return ki;} string key () {return key;} int [] keyArgs () {return keyArgs;} string [] keyProperties () {return keyProperties;} string keygenerator () {return keygenerator;}} // 매개 변수가있는 키를 설정하십시오 @Cachable (namespace = "blacklist", keyArgs = {0, 1}) public int Alothermmethond (int a, int b) {return 100; } package com.jeex.sci.test; import net.spy.memcached.memCachedClient; import org.junit.test; import org.sprameframework.context.applicationcontext; import org.springframework.context.support.support.filesystemxmlapplicationcontextaintaintaintaintaintaintaintaintaintextaintaintextaintaintext; Args [])는 InterruptedException {ApplicationContext CTX = 새로운 FileSyStemXmlApplicationContext ( "/src/test/resources/beans.xml"); memcachedclient mc = (MemcachedClient) ctx.getbean ( "memcachedclient"); BlackListDaoimpl Dao = (BlackListDaoimpl) CTX.GetBean ( "BlackListDaoImpl"); while (true) {System.out.println ( "########################################################### ######################################################## ################################################################################# ######################################## TH TO T을 ######################################################## ################################################################################# ######################################## TH TO T을 #################################################################### ######################################################################### #################################### ############################## TO TO TM 더 시작 #################################### "); Mc.flush (); BlackListQuery Query = New BlackListQuery (1,"222.231.23.13 "); DAO.SearchBlackListCount (Quearty); QuackblistCount2 New Blacklistquery (1, "123.231.23.14"); Dao.anothermond (333, 444.위는 Spring의 인터셉터를 사용하여 사용자 정의 캐시의 구현 예제 코드에 대한이 기사의 전체 내용이며 모든 사람에게 도움이되기를 바랍니다. 관심있는 친구는이 사이트의 다른 관련 주제를 계속 참조 할 수 있습니다. 단점이 있으면 메시지를 남겨 두십시오. 이 사이트를 지원해 주신 친구들에게 감사드립니다!