springboot2.0使用redis缓存问题汇总

springboot2.0使用redis缓存踩坑

本人使用的是springboot 2.0版本,缓存也是第一次使用,可以算得上是个小白了

配置springboot使用redis缓存


由于第一次使用缓存功能,基本上是按照网上的一些大神们的博客或者教程来完成

  • 首先需要配置pom文件引入redis
        <!--redis和缓存依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>
  • 属性配置文件(这里需要注意如果你的pom中还引入了其他缓存比如ehcache,这里需要设置一个属性 spring.cache.type=redis,缓存是不会进入redis的
spring:
  redis:
    host: localhost
    port: 6379
    jedis:
      pool:
        max-active: 20
        max-idle: 20
        min-idle: 0
        max-wait: 60s

使用缓存

这里我使用的就是网上的方式

  • 在启动类上开启缓存
@SpringBootApplication
@EnableCaching
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
  • 然后在你需要使用缓存的dao层也好service层也好 添加缓存注解 @Cacheable @CachePut @CacheEvict

网上大多数教程都是教到这里基本也就结束了,剩下基本就是我踩得坑了…
1. 首先是上面配置文件的那个,由于我原来的项目中是有ehcache的,但是当我想用redis作为缓存的时候如果不配置cache.type缓存是不会生效的!
2. 这个坑可能是由于我太小白了造成的,就是你要缓存的实体类一定要实现 Serializable接口,否则在你获取的时候是无法序列化的,其实这个问题想一下好了毕竟你把一个类存到redis中要再取出来是需要序列化操作的…可能是我脑子愚钝,报错了之后才发现这个问题。
3. 如果你是在接口中使用cache相关的注解,key要是使用spel表达式的话要使用 #p0这样的,不要直接#name这种的表达式。否则会因为代理的问题导致报错传入的key为空,可以看一下这个StackOverflow上别人的提问 参考文章


以上就是本人初次尝试使用redis遇到的问题,希望对一些像我这样的小白能有些帮助。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在Spring Boot中使用Redis缓存,你需要进行以下几个步骤: 1. 添加依赖:在pom.xml文件中添加Spring Data Redis依赖。 ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> ``` 2. 配置Redis连接信息:在application.properties或application.yml文件中配置Redis连接信息,如下所示: ```yaml spring.redis.host=your_redis_host spring.redis.port=your_redis_port spring.redis.password=your_redis_password (如果有密码的话) ``` 3. 创建Redis配置类:创建一个Redis配置类,用于配置RedisTemplate和CacheManager。可以参考下面的示例: ```java @Configuration @EnableCaching public class RedisConfig { @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) { RedisTemplate<String, Object> template = new RedisTemplate<>(); template.setConnectionFactory(redisConnectionFactory); // 设置key和value的序列化器 template.setKeySerializer(new StringRedisSerializer()); template.setValueSerializer(new GenericJackson2JsonRedisSerializer()); return template; } @Bean public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) { RedisCacheConfiguration cacheConfiguration = RedisCacheConfiguration.defaultCacheConfig() .disableCachingNullValues() .entryTtl(Duration.ofMinutes(10)) // 设置缓存过期时间为10分钟 .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer())) .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer())); return RedisCacheManager.builder(redisConnectionFactory) .cacheDefaults(cacheConfiguration) .build(); } } ``` 4. 使用缓存注解:在需要使用缓存的方法上添加缓存注解,如@Cacheable、@CachePut、@CacheEvict等。例如: ```java @Service public class UserService { @Autowired private UserRepository userRepository; @Cacheable(value = "users", key = "#id") public User getUserById(Integer id) { return userRepository.findById(id).orElse(null); } @CachePut(value = "users", key = "#user.id") public User saveUser(User user) { return userRepository.save(user); } @CacheEvict(value = "users", key = "#id") public void deleteUser(Integer id) { userRepository.deleteById(id); } } ``` 以上就是在Spring Boot中使用Redis缓存的基本步骤。你可以根据实际需求进行配置和使用不同的缓存注解。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mirt_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值