springboot整合redis总结

废话不多说,直接进入正题

1、引入maven依赖项

<dependency>
   <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-pool2 -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-pool2</artifactId>
    <version>2.9.0</version>
</dependency>

2、添加redis的配置类,属于固定写法

@EnableCaching
@Configuration
public class RedisConfig extends CachingConfigurerSupport {

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        RedisSerializer<String> redisSerializer = new StringRedisSerializer();
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        template.setConnectionFactory(factory);
        //key序列化方式
        template.setKeySerializer(redisSerializer);
        //value序列化
        template.setValueSerializer(jackson2JsonRedisSerializer);
        //value hashmap序列化
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        return template;
    }

    @Bean
    public CacheManager cacheManager(RedisConnectionFactory factory) {
        RedisSerializer<String> redisSerializer = new StringRedisSerializer();
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        //解决查询缓存转换异常的问题
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        // 配置序列化(解决乱码的问题),过期时间600秒
        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
                .entryTtl(Duration.ofSeconds(600))
                .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer))
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer))
                .disableCachingNullValues();
        RedisCacheManager cacheManager = RedisCacheManager.builder(factory)
                .cacheDefaults(config)
                .build();
        return cacheManager;
    }
}

3、添加redis的配置项

spring:
  redis:
    host: 192.168.209.128
    port: 6379
    timeout: 1800000

4、在相应的方法上面添加注解

  @Cacheable(value = "banner",key = "'selectIndexList'")
  @Override
  public List<CrmBanner> selectAllBanner() {
      QueryWrapper<CrmBanner> wrapper=new QueryWrapper<>();
      wrapper.orderByDesc("id");
      wrapper.last("limit 2");
      List<CrmBanner> list = baseMapper.selectList(wrapper);
      return list;
  }

  @Override
  @CacheEvict(value = "banner",allEntries = true)
  public void saveBanner(CrmBanner crmBanner) {
      baseMapper.insert(crmBanner);
  }

  @Override
  @CacheEvict(value = "banner",allEntries = true)
  public void updateBannerById(CrmBanner crmBanner) {
      baseMapper.updateById(crmBanner);
  }

  @Override
  @CacheEvict(value = "banner",allEntries = true)
  public void removeBannerById(String id) {
      baseMapper.deleteById(id);
  }

其中,@Cacheable注解会先检查是否有键为banner::selectIndexList的缓存,有的话就直接得到缓存中的数据,没有的话,就查询数据库,并添加相应的缓存。
@CacheEvict注解会根据键名来清理缓存
@CachePut注解会根据方法的返回值来更新缓存

5、代码写好了,下一步要配置Linux服务器。
修改redis.conf配置文件,
注释bind 127.0.0.1,
修改protected-mode no,
关闭Linux防火墙

systemctl stop firewalld

6、最终测试
得到了三个key,

  1. “teacher::getTeacherIndex”
  2. “banner::selectIndexList”
  3. “course::getCourseIndex”
    和course::getCourseIndex对应的值,
    页面正常显示
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值