激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - 利用spring的攔截器自定義緩存的實現(xiàn)實例代碼

利用spring的攔截器自定義緩存的實現(xiàn)實例代碼

2021-03-30 14:44txxs Java教程

這篇文章主要介紹了利用spring的攔截器自定義緩存的實現(xiàn)實例代碼,分享了相關代碼示例,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下

本文研究的主要是利用spring攔截器自定義緩存的實現(xiàn),具體實現(xiàn)代碼如下所示。

Memcached 是一個高性能的分布式內(nèi)存對象緩存系統(tǒng),用于動態(tài)Web應用以減輕數(shù)據(jù)庫負載。它通過在內(nèi)存中緩存數(shù)據(jù)和對象來減少讀取數(shù)據(jù)庫的次數(shù),從而提高動態(tài)、數(shù)據(jù)庫驅動網(wǎng)站的速度。本文利用Memcached 的實例和spring的攔截器實現(xiàn)緩存自定義的實現(xiàn)。利用攔截器讀取自定義的緩存標簽,key值的生成策略。

自定義的Cacheable

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.jeex.sci;
@Target(ElementType.METHOD) 
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Cacheable {
    String namespace();
    String key() default "";
    int[] keyArgs() default {
    }
    ;
    String[] keyProperties() default {
    }
    ;
    String keyGenerator() default "";
    int expires() default 1800;
}

自定義的CacheEvict

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.jeex.sci;
@Target(ElementType.METHOD) 
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface CacheEvict {
    String namespace();
    String key() default "";
    int[] keyArgs() default {
    }
    ;
    String[] keyProperties() default {
    }
    ;
    String keyGenerator() default "";
}

spring如果需要前后通知的話,一般會實現(xiàn)MethodInterceptor public Object invoke(MethodInvocation invocation) throws Throwable

?
1
2
3
4
5
6
7
8
9
10
11
12
public Object invoke(MethodInvocation invoction) throws Throwable {
    Method method = invoction.getMethod();
    Cacheable c = method.getAnnotation(Cacheable.class);
    if (c != null) {
        return handleCacheable(invoction, method, c);
    }
    CacheEvict ce = method.getAnnotation(CacheEvict.class);
    if (ce != null) {
        return handleCacheEvict(invoction, ce);
    }
    return invoction.proceed();
}

處理cacheable標簽

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
private Object handleCacheable(MethodInvocation invoction, Method method,
    Cacheable c) throws Throwable {
    String key = getKey(invoction, KeyInfo.fromCacheable(c));
    if (key.equals("")) {
        if (log.isDebugEnabled()){
            log.warn("Empty cache key, the method is " + method);
        }
        return invoction.proceed();
    }
    long nsTag = (long) memcachedGet(c.namespace());
    if (nsTag == null) {
        nsTag = long.valueOf(System.currentTimeMillis());
        memcachedSet(c.namespace(), 24*3600, long.valueOf(nsTag));
    }
    key = makeMemcachedKey(c.namespace(), nsTag, key);
    Object o = null;
    o = memcachedGet(key);
    if (o != null) {
        if (log.isDebugEnabled()) {
            log.debug("CACHE HIT: Cache Key = " + key);
        }
    } else {
        if (log.isDebugEnabled()) {
            log.debug("CACHE MISS: Cache Key = " + key);
        }
        o = invoction.proceed();
        memcachedSet(key, c.expires(), o);
    }
    return o;
}

處理cacheEvit標簽

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
private Object handleCacheEvict(MethodInvocation invoction, 
    CacheEvict ce) throws Throwable {
  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 invoction.proceed();
}

根據(jù)參數(shù)生成key

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//使用攔截到方法的參數(shù)生成參數(shù)
private String 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 of bound");
    }
    if (!first) {
      key.append(':');
    } else {
      first = false;
    }
    key = key.append(args[index]);
  }
  return key.toString();
}

根據(jù)屬性生成key

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
private String getKeyWithProperties(Object o, String props[]) 
    throws Exception {
  StringBuilder key = new StringBuilder();
  boolean first = true;
  for (String prop: props) {
    //把bean的屬性轉為獲取方法的名字
    String methodName = "get"
        + prop.substring(0, 1).toUpperCase() 
        + prop.substring(1);
    Method 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();
}

利用自定義的生成器生成key

1
2
3
4
5
6
7
//使用生成器生成key
private String getKeyWithGenerator(MethodInvocation invoction, String keyGenerator) 
    throws Exception {
  Class<?> ckg = Class.forName(keyGenerator);
  CacheKeyGenerator ikg = (CacheKeyGenerator)ckg.newInstance();
  return ikg.generate(invoction.getArguments());
}

保存key信息的幫助類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
private static class KeyInfo {
    String key;
    int[] keyArgs;
    String keyProperties[];
    String keyGenerator;
    static KeyInfo fromCacheable(Cacheable 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 = 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;
    }
}

參數(shù)的設置

?
1
2
3
4
5
//使用參數(shù)設置key
@Cacheable(namespace="BlackList", keyArgs={0, 1})
public int anotherMethond(int a, int b) {
  return 100;
}

測試類:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package com.jeex.sci.test;
import net.spy.memcached.MemcachedClient;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class TestMain {
    public static void main(String args[]) throws InterruptedException{
        ApplicationContext ctx = new FileSystemXmlApplicationContext("/src/test/resources/beans.xml");
        MemcachedClient mc = (MemcachedClient) ctx.getBean("memcachedClient");
        BlackListDaoImpl dao = (BlackListDaoImpl)ctx.getBean("blackListDaoImpl");
        while (true) {
            System.out.println("################################GETTING START######################");
            mc.flush();
            BlackListQuery query = new BlackListQuery(1, "222.231.23.13");
            dao.searchBlackListCount(query);
            dao.searchBlackListCount2(query);
            BlackListQuery query2 = 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.searchBlackListCount3(query2);
            Thread.sleep(300);
        }
    }
}

總結

以上就是本文關于利用spring的攔截器自定義緩存的實現(xiàn)實例代碼的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!

原文鏈接:http://blog.csdn.net/maoyeqiu/article/details/50325779

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美日韩国产一区二区三区在线观看 | 直接在线观看的三级网址 | 精品久久www| 免费播放欧美毛片 | 久久免费视频一区 | 被啪羞羞视频在线观看 | 99re热视频这里只精品 | 久久精品国产99久久6动漫亮点 | 国产一区精品在线观看 | 久久看视频| 久久91久久久久麻豆精品 | 午夜免费网 | 久久成人视屏 | 成人小视频免费在线观看 | 最新久久免费视频 | 一级成人免费 | 黄色网战在线看 | 国产亚洲精品成人a | 欧美性受ⅹ╳╳╳黑人a性爽 | 国产精品爱久久久久久久 | 爱逼av | 一区二区免费网站 | 久久艹逼 | 欧美一级视频网站 | 精品一区二区三区中文字幕老牛 | 日韩在线观看视频一区 | 精品三级内地国产在线观看 | 欧美成人性生活片 | 久久久久久久免费看 | 色毛片 | 欧美三级欧美成人高清www | 蜜桃视频最新网址 | 国产人成免费爽爽爽视频 | 国产亚洲精品久久午夜玫瑰园 | 黄色大片免费看 | 12av毛片 | 亚洲精品一区二区三区大胸 | 午夜爱爱福利 | 久久久久久久久久久久久久久伊免 | 亚洲aⅴ免费在线观看 | 91午夜视频 |