自己寫的CacheAble和CacheEvict

引言:

之前在開發(fā)中,使用了spring-redis-data中的@CacheAble和@CacheEvict,但是在使用過程中發(fā)現(xiàn),存在key串掉或失效的情況,同時,在redis客戶端直接 get key查詢時,并不是直接返回value,因此,抽空自己寫了這個注解。

思路:

使用了jedis客戶端,然后再用aop切入service所有使用緩存的方法,緩存數(shù)據(jù)至redis

代碼:
1、spring-redis.xml

<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
    <property name="maxTotal" value="300" />
    <property name="maxIdle" value="50" />
    <property name="minIdle" value="1" />
    <property name="maxWaitMillis" value="3000" />
    <property name="testOnBorrow" value="true" />
</bean>
<!-- jedis pool配置 -->
<!-- redis的連接池pool,不是必選項:timeout/password -->
<bean id="jedisPool" class="redis.clients.jedis.JedisPool">
    <constructor-arg index="0" ref="jedisPoolConfig" />
    <constructor-arg index="1" value="${redishost}" />
    <constructor-arg index="2" value="${redisport}" type="int" />
    <constructor-arg index="3" value="300000" type="int" />
    <constructor-arg index="4" value="${redispwd}"></constructor-arg>
</bean>
<bean id="redisCache" class="com.netease.mail.vip.commenta.service.redis.RedisCache">
    <property name="jedisPool" ref="jedisPool"></property>
</bean>

2、RedisCache.java

public class RedisCache {

    private JedisPool jedisPool;

    public JedisPool getJedisPool() {
        return jedisPool;
    }

    public void setJedisPool(JedisPool jedisPool) {
        this.jedisPool = jedisPool;
    }

    public void set(String key, Object value, int seconds) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.set(key, String.valueOf(value));
            jedis.expire(key, seconds);
        } catch (Exception e) {
            e.printStackTrace();
            if (jedis != null) {
                jedisPool.returnBrokenResource(jedis);
            }
        } finally {
            if (jedis != null) {
                jedisPool.returnResource(jedis);
            }
        }
    }

    public void set(String key, Object value) {
        set(key, value, 3600);
    }

    public String get(String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            String value = jedis.get(key);
            return value;
        } catch (Exception e) {
            e.printStackTrace();
            if (jedis != null) {
                jedisPool.returnBrokenResource(jedis);
            }
            return null;
        } finally {
            if (jedis != null) {
                jedisPool.returnResource(jedis);
            }
        }

    }

    public void del(String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.del(key);
        } catch (Exception e) {
            e.printStackTrace();
            if (jedis != null) {
                jedisPool.returnBrokenResource(jedis);
            }
        } finally {
            if (jedis != null) {
                jedisPool.returnResource(jedis);
            }
        }

    }

    public Object getObject(String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            byte[] value = jedis.get(key.getBytes());
            return SerializeUtil.unserialize(value);
        } catch (Exception e) {
            e.printStackTrace();
            if (jedis != null) {
                jedisPool.returnBrokenResource(jedis);
            }
            return null;
        } finally {
            if (jedis != null) {
                jedisPool.returnResource(jedis);
            }
        }

    }

    public void setObject(String key, Object value, Integer seconds) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.set(key.getBytes(), SerializeUtil.serialize(value));
            jedis.expire(key.getBytes(), seconds);
        } catch (Exception e) {
            e.printStackTrace();
            if (jedis != null) {
                jedisPool.returnBrokenResource(jedis);
            }
        } finally {
            if (jedis != null) {
                jedisPool.returnResource(jedis);
            }
        }

    }

    public void setObject(String key, Object value) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.set(key.getBytes(), SerializeUtil.serialize(value));
        } catch (Exception e) {
            e.printStackTrace();
            if (jedis != null) {
                jedisPool.returnBrokenResource(jedis);
            }
        } finally {
            if (jedis != null) {
                jedisPool.returnResource(jedis);
            }
        }

    }
    
    public void delObject(String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.del(key.getBytes());
        } catch (Exception e) {
            e.printStackTrace();
            if (jedis != null) {
                jedisPool.returnBrokenResource(jedis);
            }
        } finally {
            if (jedis != null) {
                jedisPool.returnResource(jedis);
            }
        }
    }
}

SerializeUtil.java

public class SerializeUtil {
      public static byte[] serialize(Object object) {
           ObjectOutputStream oos = null;
            ByteArrayOutputStream baos = null;
            try {
                 // 序列化
                baos = new ByteArrayOutputStream();
                oos = new ObjectOutputStream(baos);
                oos.writeObject(object);
                 byte[] bytes = baos.toByteArray();
                 return bytes;
           } catch (Exception e) {

           }
            return null;
     }

      public static Object unserialize( byte[] bytes) {
           ByteArrayInputStream bais = null;
            try {
                 // 反序列化
                bais = new ByteArrayInputStream(bytes);
                ObjectInputStream ois = new ObjectInputStream(bais);
                 return ois.readObject();
           } catch (Exception e) {

           }
            return null;
     }
}

@CacheAble

@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Cacheable{

    public static String DEFAULT_KEY = "d_key";

    /**
     * 前綴標(biāo)志位
     * 
     * @return
     */
    @AliasFor("value")
    public abstract String value() default "default_prefix";

    /**
     * 當(dāng)key為空,或非#開頭,或沒有該參數(shù),則將這個注解對應(yīng)的方法名作為key,否則以數(shù)#標(biāo)志位,從1開始
     * 
     * @return
     */
    public abstract String key() default DEFAULT_KEY;

    public abstract int expiretime() default 3600;

}

@CacheEvict

@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface CacheEvict {
    
    public static String DEFAULT_KEY = "d_key";

    /**
     * 前綴標(biāo)志位
     * 
     * @return
     */
    @AliasFor("value")
    public abstract String value() default "default_prefix";

    /**
     * 當(dāng)key為空,或非#開頭,或沒有該參數(shù),則默認(rèn)為無參數(shù)(僅存儲一個key)
     * 
     * @return
     */
    public abstract String key() default DEFAULT_KEY;
}

CacheUtils.java

@Aspect
@Component
public class CacheUtils {

    static Logger logger = Logger.getLogger(CacheUtils.class);
    @Autowired
    RedisCache redisCache;

    @Around(value = "execution (* com.cxjhihihi.service..*Service.*(..)) && @annotation(cache)")
    public Object cacheableMethod(ProceedingJoinPoint pjd, Cacheable cache) throws Throwable {
        Object result = null;
        String key = cache.key();
        String value = cache.value();
        String realKey = getRealKey(pjd, key, value);
        result = redisCache.getObject(realKey.toString());
        if (ObjectUtils.isEmpty(result)) {
            logger.info("[cacheable] key=" + realKey.toString() + " result is null");
            result = pjd.proceed();
            redisCache.setObject(realKey.toString(), result, cache.expiretime());
            logger.info("[cacheable] set cache key=" + realKey.toString() + " expiretime=" + cache.expiretime());
        }
        return result;
    }

    @Around(value = "execution (* com.cxjhihihi.service..*Service.*(..)) && @annotation(cache)")
    public void cacheEvitMethod(ProceedingJoinPoint pjd, CacheEvict cache) throws Throwable {
        String key = cache.key();
        String value = cache.value();
        String realKey = getRealKey(pjd, key, value);
        logger.info("[cacheEvict] key=" + realKey.toString());
        redisCache.delObject(realKey.toString());
    }

    private String getRealKey(ProceedingJoinPoint pjd, String key, String value)
            throws ClassNotFoundException, NotFoundException {
        logger.info("[cache realkey] key=" + key + " value=" + value);
        Object[] args = pjd.getArgs();

        String methodName = pjd.getSignature().getName();
        StringBuffer realKey = new StringBuffer().append(value).append("_");
        if (key.equals(Cacheable.DEFAULT_KEY) || !key.startsWith("#")) {
            realKey.append(methodName);
        } else {
            Integer key_index = Integer.parseInt(key.substring(1)) - 1;

            realKey.append(args[key_index]);
        }
        return realKey.toString();
    }
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,554評論 19 139
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,273評論 6 342
  • 這是我第三次在火車開車前幾分鐘上車了。今天坐上地鐵的時候,我就在反復(fù)查看地圖,心里反復(fù)盤算每一站大概需要幾分鐘,共...
    寫下即永恒閱讀 263評論 0 0
  • 楊牟牟閱讀 155評論 0 0
  • 文/宋麗 一 袁最近結(jié)婚了。 想起那天她發(fā)給我的word文檔,不一樣的境遇里,我們居然描述著一樣的心情。 時隔多年...
    宋小麗閱讀 449評論 0 1

友情鏈接更多精彩內(nèi)容