Springboot整合SpringCache+redis简化缓存开发

使用步骤:

1.引入依赖

 <dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
 <dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-redis</artifactId>
 </dependency>

2.写配置,在配置文件配置使用redis作为缓存

spring.cache.type=redis
#指定缓存过期时间,单位毫秒
spring.redis.time-to-live=3600000

引入的上面依赖后CacheAutoConfiguration会导入RedisCacheConfiguration,自动配置RedisCacheManager; 

3.在启动类添加下面的注解开启缓存功能:

@EnableCaching

完成以上配置后,就可以使用注解为简化redis缓存管理,下面是一些常用到的注解:

@Cacheable:触发将数据保存到缓存中;

@CacheEvict:触发将数据从缓存中删除;

@CachePut:使用不影响方法执行的方式更新缓存;

@Caching:组合以上多个操作;

@CacheConfig:在类级别共享缓存的相同配置。

4.在需要缓存中的方法上添加对应的注解

@Cacheable({"category"})
@Override
public List<CategoryEntity> getLevel1Categorys() {
  //此处省略具体业务逻辑
}

如果使用上面的方式声明缓存,SpringCache会有以下默认行为:

  • 如果缓存中已存在该数据,方法不再执行,直接查询缓存返回;
  • key默认自动生成,生成的规则为缓存的名字::SimpleKey[](自主生成的key值),如下图
  • 缓存的value值,默认使用jdk序列化机制,将序列化的数据存到redis; 
  • 默认ttl时间为-1,即永不过期。

如果要想自己定义一些规则,SpringCashe是支持的:
指定key名字,使用key属性,接受spEl表达式,spEl支持的表达式详见官方文档;

注意spEl表达式如果是普通字符串,一定要带单引才生效

普通字符串不带单引号,不生效:

@Cacheable(value = {"category"},key = "level1Categorys") ✘

普通字符串带单引号,生效:

@Cacheable(value = {"category"},key = "'level1Categorys'") ✔

指定缓存的存活时间ttl,在appliaction配置文件中配置,参见上文描述;

将数据保存为json格式,方便不同编程语言解析,如果想实现这一步,需添加自定义配置,参考如下代码:

@Configuration
@EnableCaching  //将启动类的开启注解移到这方便统一管理
public class MyCacheConfig {
    @Bean
    public RedisCacheConfiguration cacheConfiguration(){
        RedisCacheConfiguration config=RedisCacheConfiguration.defaultCacheConfig();
        config=config.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()));
        config=config.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));
        return config;
    }
}

注意,如果开启了Spring Cache自定义缓存,那么Spring只会来读取自定义缓存的内容,对于自定义缓存中没有的内容,将会缺失。像上面代码只配置了key和value序列化规则,没有配置缓存过期时间,即使配置文件配置了,不会读取,为了避免这个问题,对上面的方法进行升级如下:

@Configuration
@EnableCaching
public class MyCacheConfig {
    @Bean
    public RedisCacheConfiguration cacheConfiguration(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();
        //如果配置文件配置了过期时间,则读取
        if(redisProperties.getTimeToLive()!=null){
            config=config.entryTtl(redisProperties.getTimeToLive());
        }if(redisProperties.getKeyPrefix()!=null){   //如果配置文件配置了key前缀,则读取
            config=config.prefixKeysWith(redisProperties.getKeyPrefix());
        }if(!redisProperties.isCacheNullValues()){// //如果配置文件配置了不缓存空值,则禁用空值缓存
            config=config.disableCachingNullValues();
        }if(!redisProperties.isUseKeyPrefix()){  //如果配置文件配置禁用禁用key,则禁用
            config=config.disableKeyPrefix();
        }
        return config;
    }
}

附SpringCashce 在application.yml中的完整配置:

#上面还有spring在最左侧
  cache:
    type: redis
    redis:
      #设置缓存过期时间,单位ms
      time-to-live: 3600000
      #开启缓存null值,可防止缓存穿透
      cache-null-values: true
      #开启key前缀 不推荐,建议设置成false
      use-key-prefix: true
       #定义key前缀,不推荐,建议使用缓存分区
      #key-prefix: CACHE_

如果想在修改数据时触发对缓存的删除,在方面上方添加@CacheEvict并批量缓存分区即可。

如果想在修改时对多个缓存进行批量操作,可以使用下面两种方法中任一种:

@Caching(evict = {
            @CacheEvict(value = {"category"},key = "'level1Categorys'"),
            @CacheEvict(value = {"category"},key = "'getCatelogJson'"),
 })

value为设置缓存时指定的分区的名字,key为设置缓存时定义的方法名

方法二:

    @CacheEvict(value = "category",allEntries = true)

value为设置缓存时指定的分区的名字,allEntries设置为true,当标注有上面注解的方法被调用,数据修改时,指定缓存分区categorys的缓存都会被删除,当有请求再次添加缓存时,缓存分区categorys的所有数据会再次添加到缓存中。附设置缓存的方法

    @Cacheable(value = {"category"},key = "'level1Categorys'")
    @Override
    public List<CategoryEntity> getLevel1Categorys() {
      //此处省略方法具体实现,重点在缓存注解声明
    }

    @Cacheable(value = {"category"},key = "#root.methodName")
    @Override
    public Map<String,  List<Catelog2Vo>> getCatelogJson() {
        //此处省略方法具体实现,重点在缓存注解声明
     }

PS:存储同一类型的数据,可放到到同一分区,即@Cacheable注解里value的值。如此在redis缓存分区就有层次分明的结构了,这在缓存多的情况下,非常有用,能快速找到相关缓存,方便统一管理。

@注意@CacheEvict采用的是缓存一致性里的失效模式,@CachePut属于双写模式。

SpringCache有其优越之处,但存在一定的不足。

如SpringCache默认是不加锁的,要想解决缓存击穿问题,在使用时只有@Cacheable注解可配置sync属性的值为true加锁,其他注解不支持配置加锁,示例:

    @Cacheable(value = {"category"},key = "'level1Categorys'",sync = true)
    @Override
    public List<CategoryEntity> getLevel1Categorys() {
      
     }

因此,要结合具体业务情况来看是否采用。

SpringCache适用场景:常规数据(读多写少,即时性、一致性要求不高的数据)

而对于即时性和数据一致性要求高的场景需要进行特殊设计,如引入读写锁,引入canal。

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一些指导。 首先,您需要在您的Spring Boot项目中添加Spring Security和Redis的依赖。 ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> ``` 然后,在您的Spring Boot项目中创建一个配置类,该类将配置Spring Security和Redis的集成。 ```java @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private RedisConnectionFactory redisConnectionFactory; @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/admin/**").hasRole("ADMIN") .antMatchers("/user/**").hasRole("USER") .anyRequest().authenticated() .and() .formLogin() .and() .logout() .logoutSuccessUrl("/"); } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { RedisUserDetailsService userDetailsService = new RedisUserDetailsService(redisConnectionFactory); auth.userDetailsService(userDetailsService); } @Bean public RedisTokenRepositoryImpl redisTokenRepository() { return new RedisTokenRepositoryImpl(redisConnectionFactory); } @Bean public TokenBasedRememberMeServices rememberMeServices() { TokenBasedRememberMeServices rememberMeServices = new TokenBasedRememberMeServices("remember-me", userDetailsService()); rememberMeServices.setTokenRepository(redisTokenRepository()); return rememberMeServices; } @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } } ``` 上述配置类中的configure(HttpSecurity http)方法指定了不同角色的用户可以访问哪些URL。configure(AuthenticationManagerBuilder auth)方法指定了如何从Redis中获取用户信息。redisTokenRepository()和rememberMeServices()方法指定了如何将Remember-Me令牌存储到Redis中。 最后,在您的Spring Boot项目中创建一个RedisUserDetailsService类,该类将从Redis中获取用户信息。 ```java public class RedisUserDetailsService implements UserDetailsService { private RedisConnectionFactory redisConnectionFactory; public RedisUserDetailsService(RedisConnectionFactory redisConnectionFactory) { this.redisConnectionFactory = redisConnectionFactory; } @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(redisConnectionFactory); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); redisTemplate.setHashValueSerializer(new GenericToStringSerializer<>(Object.class)); redisTemplate.afterPropertiesSet(); Map<Object, Object> userMap = redisTemplate.opsForHash().entries("user:" + username); if (userMap.isEmpty()) { throw new UsernameNotFoundException("User '" + username + "' not found"); } String password = (String) userMap.get("password"); List<String> authorities = (List<String>) userMap.get("authorities"); List<GrantedAuthority> grantedAuthorities = new ArrayList<>(); for (String authority : authorities) { grantedAuthorities.add(new SimpleGrantedAuthority(authority)); } return new User(username, password, grantedAuthorities); } } ``` 上述类中的loadUserByUsername(String username)方法从Redis中获取用户信息。 希望这些信息能对您有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值