spring boot.2x 集成redis--自定义注解实现过期时间

背景

spring boot当前开发版本为2.1.2,集成redis使用@Cacheable注解无法设置过期时间,真是一大痛点!也始终想不通,万能的spring为什么没有满足这一点呢?两种解决方案:1.改源码,重新实现SimpleCacheManager;2.放弃@Cacheable,自定义注解。接下来要讲讲怎么实现后者。

实现

1.引入依赖


```javascript

<dependency>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-data-redis</artifactId>

</dependency>

<!--spring2.0集成redis所需common-pool2-->

<dependency>

    <groupId>org.apache.commons</groupId>

    <artifactId>commons-pool2</artifactId>

    <version>2.4.2</version>

</dependency>

```


2.配置redis

import org.springframework.boot.autoconfigure.AutoConfigureAfter;

import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;

import org.springframework.data.redis.core.RedisTemplate;

import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;

import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.io.Serializable;

@Configuration

@AutoConfigureAfter(RedisAutoConfiguration.class)

public class RedisCacheAutoConfiguration {

    @Bean

    public RedisTemplate<String,Serializable> redisCacheTemplate(LettuceConnectionFactory redisConnectionFactory){

        RedisTemplate<String,Serializable> template = new RedisTemplate<String,Serializable>();

        template.setKeySerializer(new StringRedisSerializer());

        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());

        template.setConnectionFactory(redisConnectionFactory);

        return template;

    }

}

3.主角-自定义annotaion

key便是redis的主键,当然后续还要解析;type便是方法或接口的出参;expire即是过期时间,一切都是为了它呀

import java.lang.annotation.*;

@Target({ ElementType.METHOD, ElementType.TYPE })

@Retention(RetentionPolicy.RUNTIME)

@Documented

public @interface Cach {

    String key() default "";

    Class type();

    //默认缓存时间是一天

    long expire() default 60*60*24L;

}

4.将注解使用在方法上

这还没完,主要的解析工作还没做呢!

5.使用AOP拦截打有自定义注解@Cach的方法

import com.alibaba.fastjson.JSON;

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.core.LocalVariableTableParameterNameDiscoverer;

import org.springframework.data.redis.core.StringRedisTemplate;

import org.springframework.stereotype.Component;

import org.springframework.util.StringUtils;

import javax.annotation.Resource;

import java.lang.reflect.Method;

import java.util.concurrent.TimeUnit;

@Aspect

@Component

public class AopCachHandle {

    @Resource

    private StringRedisTemplate stringRedisTemplate;

    @Pointcut(value = "@annotation(com.atm.servicehi.util.Cach)")

    public void pointcut(){

    }

    @Around(value = "pointcut() && @annotation(cach)")

    public Object around(ProceedingJoinPoint point, Cach cach){

        Method method = getMethod(point);

        //根据类名、方法名和参数生成key

        final String key = parseKey(cach.key(), method, point.getArgs());

        String value = stringRedisTemplate.opsForValue().get(key);

        if(null != value){

            return JSON.parseObject(value,cach.type());

        }

        try {

            Object proceed = point.proceed();

            stringRedisTemplate.opsForValue().set(key, JSON.toJSONString(proceed),cach.expire(),TimeUnit.SECONDS);

            return proceed;

        } catch (Throwable throwable) {

            throwable.printStackTrace();

        }

        return null;

    }

    /**

    * 获取被拦截方法对象

    * MethodSignature.getMethod() 获取的是顶层接口或者父类的方法对象

    * 而缓存的注解在实现类的方法上

    * 所以应该使用反射获取当前对象的方法对象

    */

    private Method getMethod(ProceedingJoinPoint pjp) {

        //获取参数的类型

        Class[] argTypes = ((MethodSignature) pjp.getSignature()).getParameterTypes();

        Method method = null;

        try {

            method = pjp.getTarget().getClass().getMethod(pjp.getSignature().getName(), argTypes);

        } catch (NoSuchMethodException e) {

            e.printStackTrace();

        }

        return method;

    }

    private String parseKey(String key, Method method, Object[] args) {

        if (StringUtils.isEmpty(key)) {

            return method.getName();

        }

        //获得被拦截方法参数列表

        LocalVariableTableParameterNameDiscoverer nd = new LocalVariableTableParameterNameDiscoverer();

        String[] parameterNames = nd.getParameterNames(method);

        for (int i = 0; i < parameterNames.length; i++) {

            key = key.replace(parameterNames[i] + "", args[i] + "");

        }

        return method.getName() + key;

    }

}

好了大功告成!

不要忘记配置redis的数据源呦!

spring:

  redis:

      host: 10.17.1.61 #redis服务器地址

      #timeout: 10000 #超时时间

      database: 9 #0-15 16个库 默认0

      port: 6379

      lettuce:

        pool:

          max-active: 8 #最大连接数

          #max-wait: -1 #默认-1 最大连接阻塞等待时间

          max-idle: 8 #最大空闲连接 默认8

          min-idle: 0 #最小空闲连接

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,991评论 19 139
  • 前言 开心一刻 小白问小明:“你前面有一个5米深的坑,里面没有水,如果你跳进去后该怎样出来了?”小明:“躺着出来呗...
    fad2aa506f5e阅读 1,507评论 0 1
  • 2018.9.24星期一天气晴 亲子日记第201天 《父母规》第176天 八月十五,团圆节!祝大家节日快乐!...
    爱腾睿阅读 516评论 4 10
  • 喜欢你的一切 无论好坏,我都喜欢 可是,你已经放开我的手 不再喜欢我 喜欢一个不喜欢自己的人 进一步没资格 退一步...
    辞旧时辞旧时辞旧时阅读 227评论 0 0
  • 我有。 “你又去图书馆啊。” “是啊。” “天天去图书馆又有什么用,希望你六级的成绩,能和我拉大距离哦!” 躺在床...
    困兽233阅读 2,097评论 33 49