一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專(zhuān)注于服務(wù)器技術(shù)及軟件下載分享
分類(lèi)導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

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

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

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

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

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

Memcached 是一個(gè)高性能的分布式內(nèi)存對(duì)象緩存系統(tǒng),用于動(dòng)態(tài)Web應(yīng)用以減輕數(shù)據(jù)庫(kù)負(fù)載。它通過(guò)在內(nèi)存中緩存數(shù)據(jù)和對(duì)象來(lái)減少讀取數(shù)據(jù)庫(kù)的次數(shù),從而提高動(dòng)態(tài)、數(shù)據(jù)庫(kù)驅(qū)動(dòng)網(wǎng)站的速度。本文利用Memcached 的實(shí)例和spring的攔截器實(shí)現(xiàn)緩存自定義的實(shí)現(xiàn)。利用攔截器讀取自定義的緩存標(biāo)簽,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如果需要前后通知的話,一般會(huì)實(shí)現(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標(biāo)簽

?
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標(biāo)簽

?
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的屬性轉(zhuǎn)為獲取方法的名字
    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信息的幫助類(lèi)

?
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ù)的設(shè)置

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

測(cè)試類(lèi):

?
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);
        }
    }
}

總結(jié)

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

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲国产成人精品不卡青青草原 | 嫩草影院国产 | 深夜草莓视频 | 5g996未满十八 | 国产精品俺来也在线观看了 | 天天黄视频 | 天海翼三级 | 国产裸露片段精华合集链接 | 亚洲香蕉伊在人在线观看9 亚洲系列国产系列 | 4hc44四虎永久地址链接 | 99爱在线精品视频免费观看9 | 亚洲国产精品福利片在线观看 | 强行扒开美女大腿挺进 | 欧美高清在线 | 色中色软件 | 五月婷婷俺来也 | 色屁屁www | 国内精品露脸在线视频播放 | 操穴勤 | 久久精品国产在热亚洲 | 亚洲精品一区在线观看 | 青草青草久热精品视频在线网站 | 日本欧美一二三区色视频 | 色啪久久婷婷综合激情 | bl双性受乖调教改造身体 | 亚洲成色www久久网站 | 国产激情影院 | 国产香蕉一区二区在线网站 | 性欧美高清理论片 | 九九热在线免费观看 | 黄 色 大 片 网站 | 色亚州| 波多洁野衣一二区三区 | 色操网| 风间由美在线播放 | 国产精品日本亚洲777 | 亚洲成人影院在线观看 | 日本精品一区二区在线播放 | 国产无套在线播放 | 波多野结衣在线观看视频 | 2019理论韩国理论中文 |