Spring Boot中使用Redis,并自定义缓存过期时间

1.首先引入依赖,在pom.xml中加入以下代码

<!-- redis -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
    <version>2.0.3.RELEASE</version>
</dependency>
<!-- lettuce -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-pool2</artifactId>
</dependency>

2.添加yml配置:


spring:
  redis:
    host: localhost
    port: 6379
    password: 123456
    timeout: 1000ms # 连接超时时间
    database: 12 #数据库索引
    lettuce:
      pool:
      # 连接池最大连接数(使用负值表示没有限制)
        max-active: 1000
        # 连接池中的最大空闲连接
        max-idle: 80
        # 连接池中的最小空闲连接
        min-idle: 10
        # 连接池最大阻塞等待时间(使用负值表示没有限制)
        max-wait: 100ms

3.然后创建一个配置类,设置redis的序列化方式,不然无法序列化对象等

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {

        RedisTemplate<String, Object> template = new RedisTemplate<>();
        // 配置连接工厂
        template.setConnectionFactory(factory);

        //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式)
        Jackson2JsonRedisSerializer jacksonSerial = new Jackson2JsonRedisSerializer(Object.class);

        ObjectMapper om = new ObjectMapper();
        // 指定要序列化的域,field,get和set,以及修饰符范围,ANY是都有包括private和public
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        // 指定序列化输入的类型,类必须是非final修饰的,final修饰的类,比如String,Integer等会跑出异常
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jacksonSerial.setObjectMapper(om);

        // 值采用json序列化
        template.setValueSerializer(jacksonSerial);
        //使用StringRedisSerializer来序列化和反序列化redis的key值,这样存储不会出现一些符号前缀
        template.setKeySerializer(new StringRedisSerializer());

        // 设置hash key 和value序列化模式
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(jacksonSerial);
        template.afterPropertiesSet();

        return template;
    }
}

4.在Service或者要使用的地方进行注入,


    @Autowired
    private RabbitTemplate rabbitTemplate;

之后就可以使用了

//插入redis中
redisTemplate.opsForList().rightPush("test", "123");

更多的数据类型操作可参考枣面包的博客,里面对String,List,Hash,Set,ZSet的操作有详细介绍

以上是手动的操作,也可以使用注解的方式,可参考nfcm的博客对注解的使用有初步了解

可以使用Spring 的aop自定义注解实现自定义缓存过期时间
1.定义一个注解


import java.lang.annotation.*;

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

@Retention(RetentionPolicy.RUNTIME)

@Documented

public @interface MyCache {

    //redis的key
    String key();

    //返回类型
    Class type();

    //默认缓存时间是一天

    long expire() default 60 ;

}

2.添加注解的拦截逻辑,在这里添加缓存逻辑


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.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Aspect
@Component
public class AopCacheHandle {

    @Autowired
    private RedisTemplate redisTemplate;

    @Pointcut(value = "@annotation(com.common.cache.MyCache)")
    public void pointcut() {

    }

    @Around(value = "pointcut() && @annotation(cache)")
    public Object around(ProceedingJoinPoint point, MyCache cache) {
        //根据参数生成key
        final String key = parseKey(cache.key(), point.getArgs());
        String value = null;
        try {
            //从redis中获取缓存
            value = (String) redisTemplate.opsForValue().get(key);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        if (null != value) {
            return JSON.parseObject(value, cache.type());
        }
        try {
            //等待返回结果
            Object proceed = point.proceed();
            if (proceed != null) {
                try {
                    //设置缓存
                    redisTemplate.opsForValue().set(key, JSON.toJSONString(proceed), cache.expire(), TimeUnit.SECONDS);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
            return proceed;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return null;
    }

    /**
     * redis的键值生成策略
     *
     * @param key  key前缀
     * @param args 方法参数列表
     */
    private String parseKey(String key, Object[] args) {
        //真正的key
        StringBuilder sb = new StringBuilder(key + "-");
        for (int i = 0; i < args.length; i++) {
            sb.append(args[i].toString() + "-");
        }
        sb.deleteCharAt(sb.length() - 1);
        return sb.toString();
    }

}

参考博客:
https://blog.csdn.net/weixin_34277853/article/details/87221807

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot 使用 Redis 作为数据缓存,需要以下步骤: 1. 添加 Redis 依赖 在 pom.xml 文件添加 Jedis 或 Lettuce 依赖,例如: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>3.6.0</version> </dependency> ``` 2. 配置 Redis 在 application.properties 或 application.yml 文件配置 Redis 相关信息,例如: ```properties spring.redis.host=localhost spring.redis.port=6379 spring.redis.password= spring.redis.database=0 ``` 3. 创建 RedisTemplate 在 Spring Boot ,可以使用 RedisTemplate 来操作 Redis 数据库。通常情况下,我们需要自己创建一个 RedisTemplate 的 Bean,并注入到需要使用 Redis 的地方。例如: ```java @Configuration public class RedisConfig { @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(connectionFactory); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); return redisTemplate; } } ``` 4. 使用 Redis 在需要使用 Redis 缓存的地方,注入 RedisTemplate,并使用其提供的操作方法,例如: ```java @Service public class UserServiceImpl implements UserService { @Autowired private RedisTemplate<String, Object> redisTemplate; @Override public User getUserById(Long id) { String key = "user:id:" + id; User user = (User) redisTemplate.opsForValue().get(key); if (user == null) { user = userRepository.findById(id).orElse(null); if (user != null) { redisTemplate.opsForValue().set(key, user); } } return user; } } ``` 在上述代码,我们通过 Redis 缓存了用户信息,如果缓存不存在该用户信息,则从数据库查询并将查询结果存入 Redis 缓存,下次查询时先从缓存查询。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值