spring与redis集成之aop整合方案
java使用redis缓存可以使用jedis框架,jedis操作简单,没有什么复杂的东西需要学习,网上资料很多,随便看看就会了.
将spring与redis缓存集成,其实也是使用jedis框架,只不过spring对它进行了一层封装,并将这层封装库命名为spring-data-redis.
下面将要使用spring-data-redis与jedis的jar包,并通过spring的aop功能,将redis缓存无缝无侵入的整合进来.
1.先下载好依赖包
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-core</artifactId>
- <version>4.1.1.RELEASE</version>
- </dependency>
- <!-- 还有spring的其它包,这里不一一贴出-->
- <dependency>
- <groupId>org.springframework.data</groupId>
- <artifactId>spring-data-redis</artifactId>
- <version>1.4.1.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>redis.clients</groupId>
- <artifactId>jedis</artifactId>
- <version>2.6.0</version>
- </dependency>
2.再配置spring文件
- <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
- <property name="minIdle" value="${redis.minIdle}" />
- <property name="maxIdle" value="${redis.maxIdle}" />
- <property name="maxTotal" value="${redis.maxActive}" />
- <property name="maxWaitMillis" value="${redis.maxWait}" />
- <property name="testOnBorrow" value="${redis.testOnBorrow}" />
- </bean>
- <bean id="jedisConnFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
- <property name="hostName" value="${redis.host}" />
- <property name="port" value="${redis.port}" />
- <property name="password" value="${redis.password}" />
- <property name="usePool" value="true" />
- <property name="poolConfig" ref="poolConfig" />
- </bean>
- <!-- redis template definition -->
- <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
- <property name="connectionFactory" ref="jedisConnFactory" />
- <property name="keySerializer">
- <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
- </property>
- <property name="valueSerializer">
- <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
- </property>
- <property name="hashKeySerializer">
- <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
- </property>
- <property name="hashValueSerializer">
- <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
- </property>
- </bean>
3.开始编写aop代码
3.1 声明两个注解类,用于定义哪些方法将使用缓存
- @Retention(RetentionPolicy.RUNTIME)
- @Target({ElementType.METHOD})
- public @interface Cacheable {
- public enum KeyMode{
- DEFAULT, //只有加了@CacheKey的参数,才加入key后缀中
- BASIC, //只有基本类型参数,才加入key后缀中,如:String,Integer,Long,Short,Boolean
- ALL; //所有参数都加入key后缀
- }
- public String key() default ""; //缓存key
- public KeyMode keyMode() default KeyMode.DEFAULT; //key的后缀模式
- public int expire() default 0; //缓存多少秒,默认无限期
- }
- @Retention(RetentionPolicy.RUNTIME)
- @Target({ElementType.PARAMETER})
- public @interface CacheKey {}
3.2 创建一个Aop拦截器的处理类,用于拦截加了@Cacheable的方法
- @Aspect
- @Component
- public class CacheableAop {
- @Autowired private RedisTemplate redisTemplate;
- @Around("@annotation(cache)")
- public Object cached(final ProceedingJoinPoint pjp,Cacheable cache) throws Throwable {
- String key=getCacheKey(pjp, cache);
- ValueOperations<String, Object> valueOper=redisTemplate.opsForValue();
- Object value=valueOper.get(key); //从缓存获取数据
- if(value!=null) return value; //如果有数据,则直接返回
- value = pjp.proceed(); //跳过缓存,到后端查询数据
- if(cache.expire()<=0) { //如果没有设置过期时间,则无限期缓存
- valueOper.set(key, value);
- } else { //否则设置缓存时间
- valueOper.set(key, value,cache.expire(),TimeUnit.SECONDS);
- }
- return value;
- }
- /**
- * 获取缓存的key值
- * @param pjp
- * @param cache
- * @return
- */
- private String getCacheKey(ProceedingJoinPoint pjp,Cacheable cache) {
- StringBuilder buf=new StringBuilder();
- buf.append(pjp.getSignature().getDeclaringTypeName()).append(".").append(pjp.getSignature().getName());
- if(cache.key().length()>0) {
- buf.append(".").append(cache.key());
- }
- Object[] args=pjp.getArgs();
- if(cache.keyMode()==KeyMode.DEFAULT) {
- Annotation[][] pas=((MethodSignature)pjp.getSignature()).getMethod().getParameterAnnotations();
- for(int i=0;i<pas.length;i++) {
- for(Annotation an:pas[i]) {
- if(an instanceof CacheKey) {
- buf.append(".").append(args[i].toString());
- break;
- }
- }
- }
- } else if(cache.keyMode()==KeyMode.BASIC) {
- for(Object arg:args) {
- if(arg instanceof String) {
- buf.append(".").append(arg);
- } else if(arg instanceof Integer || arg instanceof Long || arg instanceof Short) {
- buf.append(".").append(arg.toString());
- } else if(arg instanceof Boolean) {
- buf.append(".").append(arg.toString());
- }
- }
- } else if(cache.keyMode()==KeyMode.ALL) {
- for(Object arg:args) {
- buf.append(".").append(arg.toString());
- }
- }
- return buf.toString();
- }
- }
4.使用缓存示例
- @Service
- @Transactional
- public class DemoServiceImpl implements DemoService {
- @Autowired private DemoDao demoDao;
- public List<Demo> findAll() {
- return demoDao.findAll();
- }
- /*
- 对get()方法配置使用缓存,缓存有效期为3600秒,缓存的key格式为:{package_name}.DemoServiceImpl.get
- 同时为参数配置了@CacheKey后,表示此参数的值将做为key的后缀,此例的key,最终是:{package_name}.DemoServiceImpl.get.{id}
- 可以为多个参数配置@CacheKey,拦截器会调用参数的toString()做为key的后缀
- 若配置多个@CacheKey参数,那么最终的key格式为:{package_name}.{class_name}.{method}.{arg1}.{arg2}.{...}
- */
- @Cacheable(expire=3600)
- public Demo get(@CacheKey String id) {
- return demoDao.get(id);
- }
- public Demo getByName(String name) {
- return demoDao.getByName(name);
- }
- }
- 若为名称相同的方法配置缓存,可以在@Cacheable中加入key属性,追加额外的key后缀
- @Cacheable还有一个KeyMode属性,用于配置哪些参数可以追加到key后缀中,
默认取值 DEFAULT:表示只有加了@CacheKey的参数才能追加到key后缀
BASIC:自动将基本类型追加到key后缀,而无需再配置@CacheKey
ALL:自动将所有参数追加到lkey后缀,而无需再配置@CacheKey
这只是一个初步整合方案,测试可行,还未在生产中使用,实际效果待验正.
spring与redis集成之aop整合方案的更多相关文章
- Spring Boot Redis 集成配置(转)
Spring Boot Redis 集成配置 .embody{ padding:10px 10px 10px; margin:0 -20px; border-bottom:solid 1px #ede ...
- mybatis与Spring集成(Aop整合PagerAspect插件)
目的: Mybatis与spring集成 Aop整合pagehelper插件 Mybatis与spring集成 导入pom依赖 <?xml version="1.0" enc ...
- spring boot通过Spring Data Redis集成redis
在spring boot中,默认集成的redis是Spring Data Redis,Spring Data Redis针对redis提供了非常方便的操作模版RedisTemplate idea中新建 ...
- Spring Boot Redis 集成 Error creating bean with name 'enableRedisKeyspaceNotificationsInitializer'
一.原因:redis集群环境没有开启Keyspace notifications 二.解决办法 @Configuration public class HttpSessionConfig { /** ...
- Spring+ehcache+redis两级缓存
问题描述 场景:我们的应用系统是分布式集群的,可横向扩展的.应用中某个接口操作满足以下一个或多个条件: 1. 接口运行复杂代价大, 2. 接口返回数据量大, 3. 接口的数据基本不会更改, 4. 接口 ...
- 【转】Spring的中IoC及AOP
1. Spring介绍 Spring是轻量级的J2EE应用程序框架.Spring的核心是个轻量级容器(container),实现了IoC(Inversion of Control)模式的容器,Spri ...
- spring data redis使用1——连接的创建
spring data redis集成了几个Redis客户端框架,Jedis , JRedis (Deprecated since 1.7), SRP (Deprecated since 1.7) a ...
- Spring Data Redis配置项有多少(不列举具体,只提供找的方法)
首先,要说明Spring Data Redis集成了很多款客户端,比如Jedis这些. 而如果在注入Bean时,我们一般是可以设置一些项的,比如hostName和port等,对于这些项一般的查找方式通 ...
- 使用Spring Data Redis操作Redis(单机版)
说明:请注意Spring Data Redis的版本以及Spring的版本!最新版本的Spring Data Redis已经去除Jedis的依赖包,需要自行引入,这个是个坑点.并且会与一些低版本的Sp ...
随机推荐
- sql server 规则
1.规则的创建 create rule rule_no1 as @e_salary>500 2.把自定义规则绑定到列 exec sp_bindrule 'rule_no1','employee. ...
- springmvc+mybatis集成配置
简单之美,springmvc,mybatis就是一个很好的简单集成方案,能够满足一般的项目需求.闲暇时间把项目配置文件共享出来,供大家参看: 1.首先我们来看下依赖的pom: <!-- spri ...
- STL模板_map
map -key - value -键值无法重复 multimap -键值可以重复 声明: -map<int, string> m -multimap<int, string> ...
- BZOJ 1146: [CTSC2008]网络管理Network( 树链剖分 + 树状数组套主席树 )
树链剖分完就成了一道主席树裸题了, 每次树链剖分找出相应区间然后用BIT+(可持久化)权值线段树就可以完成计数. 但是空间问题很严重....在修改时不必要的就不要新建, 直接修改原来的..详见代码. ...
- js基础参数获取
1 获取浏览器中url中的参数,会自动把问号"?"去掉 function getParamsFromHref() { //调试用 var wyl_ = window.locatio ...
- c++读取文件内容并保存到二维数组
每行数据最后需要Tab处理 #include <iostream> #include <fstream> #include <string> using names ...
- Android中计算时间差的实现方法
今天为“至简天气”增加了一项功能:在启动时根据上次更新数据的时间判断是否有必要更新数据,因为 weather.com.cn 的实况数据貌似是25分钟才会刷新一次,只有在据上次更新的时间达25分钟以上才 ...
- Bluetooth 2.1+EDR是什么
目前应用最为广泛的是 Bluetooth 2.0+EDR标准,该标准在2004年已经推出,支持Bluetooth 2.0+EDR标准的产品也于2006年大量出现.虽然Bluetooth 2.0+EDR ...
- java.lang.RuntimeException: Unable to start activity ComponentInfo
异常:java.lang.RuntimeException: Unable to start activity ComponentInfo{com.william/com.william.Result ...
- hdu 2563 统计问题
统计问题 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submissi ...
