深入理解Spring Redis的使用 (六)、用Spring Aop 实现注解Dao层的自动Spring Redis缓存
来源:互联网 发布:python 提取文件路径 编辑:程序博客网 时间:2024/05/21 22:34
原文:深入理解Spring Redis的使用 (六)、用Spring Aop 实现注解Dao层的自动Spring Redis缓存
摘要: 主要针对Dao层的一些数据库查询的操作,数据实时性不强,直接加入缓存。当缓存中有的时候,就使用缓存中的数据。这样的方法,最终仅仅使用一个注解实现。对于之前的hibernate二级缓存使用,比较陌生。比如是否支持Redis或者可以自己开发支持。是否支持针对部分需要加入缓存的方法配置,而不是所有的hibernate实体都加入缓存。可能我这种方法对于二级缓存来说,抛开代码差距,也是殊途同归的东西。
这几天工作中,突然遇到了对于有些个实体类,需要被缓存起来。但是这些个实体类数目庞大, 初始化加载的话,太耗费时间。所以初步的方案就是先查缓存,缓存没有就查询数据库,查完数据库再放入缓存。同时也方便设置过期时间。
但是针对目前的项目来说,Dao是作为独立的Maven Module,Redis也是独立的Maven Module,相互耦合的话,代码变得难以维护,结构不清晰。所以引入了注解,然后在Redis项目中,针对注解做AOP,这样的话,没有用到缓存的项目,就可以忽略这样的注解。如果用到了,可以自动加入缓存。
注解代码:
package com.ns.annotation;import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;import java.util.concurrent.TimeUnit;/** * 只能注解dao里面对应的get方法,传递的参数作为hashkey,返回的值作为value * @author Han */@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface Cached { /** * redis key * @return */ String key(); /** * 过期时间,默认为0即永不过期 * @return */ long timeout() default 0L; /** * 时间单位,默认为秒 * @return */ TimeUnit timeunit() default TimeUnit.SECONDS;}
Aop切面代码
package com.ns.redis.aop;import java.lang.reflect.Method;import java.util.HashMap;import java.util.LinkedHashMap;import java.util.Map;import org.apache.commons.collections.MapUtils;import org.apache.commons.lang.ArrayUtils;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Pointcut;import org.aspectj.lang.reflect.MethodSignature;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.dao.DataAccessException;import org.springframework.data.redis.connection.RedisConnection;import org.springframework.data.redis.core.RedisCallback;import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;import org.springframework.data.redis.serializer.RedisSerializer;import org.springframework.stereotype.Component;import org.springframework.util.Assert;import com.ns.annotation.Cached;import com.ns.redis.dao.base.BaseRedisDao;/** * 对dao的getbean的缓存处理 * @author Han */@Aspect@Componentpublic class AutoRedisCached extends BaseRedisDao<String, Object>{ /* * 约束任意包下的包含Dao的类的任意方法,并且被cached注解 */ @Pointcut("execution(* *..*Dao*.*(*,..) && @annotation(com.ns.annotation.Cached))") private void cacheMethod(){} @Around("cacheMethod()") public Object doArround(ProceedingJoinPoint pjp) throws Throwable{ Object[] args = pjp.getArgs(); //定义序列化器 final RedisSerializer<String> keySerializer = getKeySerializer(); final RedisSerializer<Object> hashValueSerializer = getHashValueSerializer(); final RedisSerializer<Object> hashKeySerializer = getHashKeySerializer(); //序列化参数,作为hashkey byte [] hashkeyBytesTmp = null; if(args.length == 1){ hashkeyBytesTmp = hashKeySerializer.serialize(args[0]); }else{ hashkeyBytesTmp = new byte[0]; for(Object arg : args){ hashkeyBytesTmp = ArrayUtils.addAll(hashkeyBytesTmp, hashKeySerializer.serialize(arg)); } } final byte [] hashkeyBytes = hashkeyBytesTmp; MethodSignature methodSignature = (MethodSignature) pjp.getSignature(); Method method = methodSignature.getMethod(); final Cached cacheinfo = method.getAnnotation(Cached.class); Object obj= null; obj = execute(new RedisCallback<Object>() { @Override public Object doInRedis(RedisConnection connection) throws DataAccessException { byte [] tmp = connection.hGet(keySerializer.serialize(cacheinfo.key()), hashkeyBytes); return hashValueSerializer.deserialize(tmp); } }); if(obj == null){ final Object objReturn = pjp.proceed(); if(objReturn != null){ execute(new RedisCallback<Boolean>() { @Override public Boolean doInRedis(RedisConnection connection) throws DataAccessException { return connection.hSet(keySerializer.serialize(cacheinfo.key()), hashkeyBytes,hashValueSerializer.serialize(objReturn)); } }); if(cacheinfo.timeout()>0){ expire(cacheinfo.key(), cacheinfo.timeout(), cacheinfo.timeunit()); } } obj = objReturn; } //从dao获取 return obj; }}
阅读全文
0 0
- 深入理解Spring Redis的使用 (六)、用Spring Aop 实现注解Dao层的自动Spring Redis缓存
- 深入理解Spring Redis的使用 (八)、Spring Redis实现 注解 自动缓存
- 分布式缓存技术redis学习系列(六)—— 深入理解Spring Redis的使用
- spring集成redis缓存的注解实现
- 分布式缓存技术redis学习系列----深入理解Spring Redis的使用
- 技术文章 | 分布式缓存技术redis学习系列----深入理解Spring Redis的使用
- 分布式缓存技术redis学习系列----深入理解Spring Redis的使用
- 利用Spring的aop整合redis缓存
- Spring Boot 基于注解的 Redis 缓存使用
- 使用spring自带的Cacheable注解处理Redis缓存
- 使用Spring AOP注解实现Redis缓存 适合复杂业务场合
- spring aop结合redis实现数据缓存
- 使用spring+redis实现缓存
- 深入理解Spring Redis的使用 (一)、Spring Redis基本使用
- 深入理解Spring Redis的使用 (一)、Spring Redis基本使用
- 深入理解Spring Redis的使用 (七)、Spring Redis 使用 jackson序列化 以及 BaseDao代码
- Spring Cache+Redis实现自定义注解缓存
- 深入理解Spring Redis的使用 (五)、常见问题汇总
- java基础--跳转控制语句return break continue
- 4886: [Lydsy2017年5月月赛]叠塔游戏
- 面试题集锦
- codeforces round #415 C.Do you want a Date?
- linux运维-postfix邮件的处理
- 深入理解Spring Redis的使用 (六)、用Spring Aop 实现注解Dao层的自动Spring Redis缓存
- 2017年 3月到2017年6月
- SystemUI 7.0学习笔记二-状态栏和导航栏
- docker入门安装
- 将access_token保存在哪?
- unity3d施加牛顿力
- 嵌入式Linux应用程序开发详解-笔记1
- 一个正整数分解为几个连续的正整数之和
- 集合类