springboot整合redis

本文介绍了如何在SpringBoot项目中整合Redis作为缓存,包括引入相关starter、配置文件设置、连接Redis、配置SpringCache及自定义序列化方式,以及在业务方法上使用@Cacheable注解和通过控制器测试缓存效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

springboot整合redis

  • 引入redis和SpringCache的starter
  <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>io.lettuce</groupId>
                    <artifactId>lettuce-core</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>
  • 在application.properties中指定缓存类型为redis
spring.cache.type=redis
spring.cache.redis.time-to-live=3600000
spring.cache.redis.use-key-prefix=true
#是否缓存空值,防止缓存穿透
spring.cache.redis.cache-null-values=true
  • 在application.yml中连接redis
spring:
	 redis:
	      host: 192.168.56.101
	      port: 6379
  • 编写SpringCache的配置类(默认使用jdk进行序列化(可读性差),默认ttl为-1永不过期,自定义序列化方式需要编写配置类),并在配置类上标注相关注解
@EnableConfigurationProperties(CacheProperties.class)
@EnableCaching
@Configuration
public class MyCacheConfig {

    @Bean
    public RedisCacheConfiguration redisCacheConfiguration(CacheProperties cacheProperties) {

        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
        config = config.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()));
        config = config.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));

        CacheProperties.Redis redisProperties = cacheProperties.getRedis();
        //指定缓存序列化方式为json
        //设置配置文件中的各项配置,如过期时间
        if (redisProperties.getTimeToLive() != null) {
            config = config.entryTtl(redisProperties.getTimeToLive());
        }

        if (redisProperties.getKeyPrefix() != null) {
            config = config.prefixKeysWith(redisProperties.getKeyPrefix());
        }
        if (!redisProperties.isCacheNullValues()) {
            config = config.disableCachingNullValues();
        }
        if (!redisProperties.isUseKeyPrefix()) {
            config = config.disableKeyPrefix();
        }
        return config;
    }
}
  • 在业务方法上标注@Cacheable注解
 @Cacheable(value = {"voList"}, key = "#root.method.name", sync = true)
    @Override
    public List<EmployeeVo> getList() {
        List<EmployeeEntity> employeeList = employeeDao.selectList(null);
        List<EmployeeVo> voList = new ArrayList<>();
        if (null != employeeList && employeeList.size() > 0) {
            for (EmployeeEntity employee : employeeList) {
                EmployeeVo employeeVo = new EmployeeVo();
                BeanUtils.copyProperties(employee, employeeVo);
                DepartEntity depart = departDao.selectById(employee.getDepid());
                employeeVo.setDepname(depart.getDepname());
                voList.add(employeeVo);
            }
        }
        return voList;
    }
  • 调用controller方法测试缓存是否保存在redis中
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值