SpringBoot缓存原理(整合Redis)

一、 Spring缓存抽象

在Spring Boot中,数据的缓存管理存储依赖于Spring框架中Cache相关的
org.springframework.cache.Cache接口和org.springframework.cache.CacheManager缓存管理器接口。

对于Cache接口,Spring提供了各种xxxCache的实现。比如RedisCache、EhCacheCache、ConcurrentMapCache等,对于使用不同的CacheManager,会选择不同的Cache组件

如果程序中没有定义类型为CacheManager的Bean组件或者是名为cacheResolver的CacheResolver缓存解析器,Spring Boot将尝试选择并启用以下缓存组件(按照指定的顺序):

(1)Generic
(2)JCache (JSR-107) (EhCache 3、Hazelcast、Infinispan等)
(3)EhCache 2.x
(4)Hazelcast
(5)Infinispan
(6)Couchbase
(7)Redis
(8)Caffeine
(9)Simple

上面按照Spring Boot缓存组件的加载顺序,列举了支持的9种缓存组件,在项目中添加某个缓存管理
组件(例如Redis)后,Spring Boot项目会选择并启用对应的缓存管理器。如果项目中同时添加了多个
缓存组件,且没有指定缓存管理器或者缓存解析器(CacheManager或者cacheResolver),那么
Spring Boot会按照上述顺序在添加的多个缓存中优先启用指定的缓存组件进行缓存管理。

二、SpringBoot默认缓存

引入坐标

引入spring-boot-starter-cache模块

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

@EnableCaching

使用@EnableCaching注解开启基于注解的缓存支持

@EnableCaching // 开启Spring Boot基于注解的缓存管理支持
@SpringBootApplication
public class Springboot04CacheApplication {
    public static void main(String[] args) {
    	SpringApplication.run(Springboot04CacheApplication.class, args);
    }
}

@Cacheable

使用@Cacheable注解对数据操作方法进行缓存管理。将@Cacheable注解标注在Service类的查询方法上,对查询结果进行缓存

    // #id ,#a0,#p0都代表参数值
    @Cacheable(cacheNames="emp",
            key = "#id",
            condition = "#a0>1",
            unless = "#p0==2"
    )
    public Employee getEmpById(Integer id) {
        System.out.println("查询员工:"+id);
        return employeeMapper.getEmpById(id);
    }

执行流程:方法运行之前,先去查询Cache(缓存组件),按照cacheNames指定的名字获取,(CacheManager先获取相应的缓存),第一次获取缓存如果没有Cache组件会自动创建;

@Cacheable注解提供了多个属性,用于对缓存存储进行相关配置

属性名说明
value/cacheNames指定缓存空间的名称,必配属性。这两个属性二选一使用
key指定缓存数据的key,默认使用方法参数值,可以使用SpEL表达式
keyGenerator指定缓存数据的key的生成器,与key属性二选一使用
cacheManager指定缓存管理器
cacheResolver指定缓存解析器,与cacheManager属性二选一使用
condition指定在符合某条件下,进行数据缓存
unless指定在符合某条件下,不进行数据缓存
sync指定是否使用异步缓存。默认false

Cache的key如果不指定,默认就是方法的参数,如果多个参数或者没有参数,使用SimpleKeyGenerator生成key

参数个数key
没有参数new SimpleKey()
有一个参数参数值
多个参数new SimpleKey(params)

常用的SPEL表达式

描述示例
当前被调用的方法名#root.mathodName
当前被调用的方法#root.mathod
当前被调用的目标对象#root.target
当前被调用的目标对象类#root.targetClass
当前被调用的方法的参数列表#root.args[0] 第一个参数, #root.args[1] 第二个参数…
根据参数名字取出值#参数名, 也可以使用 #p0 #a0 0是参数的下标索引
当前方法的返回值#result

@CachePut注解

目标方法执行完之后生效, @CachePut被使用于修改操作比较多,哪怕缓存中已经存在目标值了,但是这个
注解会保证执行之后的结果会被更新

@CachePut注解也提供了多个属性,这些属性与@Cacheable注解的属性完全相同。

由于执行时机是目标方法结束后执行, 所以可以使用SPEL表达式 key="#result.id" , 拿出返回值的id

    @CachePut(cacheNames="emp",key = "#result.id" )
    public Employee updateEmp(Employee employee) {
        System.out.println("更新员工"+employee);
        employeeMapper.updateEmp(employee);
        return employee;
    }

@CacheEvict注解

@CacheEvict注解是由Spring框架提供的,可以作用于类或方法(通常用在数据删除方法上),该注解
的作用是删除缓存数据。@CacheEvict注解的默认执行顺序是,先进行方法调用,然后将缓存进行清
除。


@CacheEvict(cacheNames="emp",key = "#id")
public Integer delEmp(Integer id){
    System.out.println("删除员工:"+id);
    employeeMapper.delEmp(id);
    return id;
}

@CacheEvict注解中的其他属性

  1. allEntries:默认为false,如果改为true,则cacheNames="emp"中的缓存都会被删除
  2. beforeInvocation: 缓存的清除是否在方法之前完成,默认为false

@Caching

它是一个组合注解,组合使用@Cacheable、@CachePut、@CacheEvict,定义复制缓存规则

 @Caching(
       cacheable = {
           @Cacheable(value="emp",key = "#lastName")
       },
       put = {
           @CachePut(value="emp",key = "#result.id"),
           @CachePut(value="emp",key = "#result.email")
       }
  )
  public Employee getEmpByLastName(String lastName){
      return employeeMapper.getEmpByLastName(lastName);
  }

@CacheConfig

标注在类上,用于抽取公共属性,比如缓存的名字,缓存keyGenerator 等

@CacheConfig(cacheNames="emp")
public class EmployeeService{
    @CacheEvict(key = "#id")
    public Integer delEmp(Integer id){
        System.out.println("删除员工:"+id);
        employeeMapper.delEmp(id);
        return id;
    }
    
}

自定义KeyGenerator

使用时在注解属性内指定KeyGenerator=“myKeyGenerator”

@Configuration
public class MyCacheConfig {
    @Bean("myKeyGenerator")
    public KeyGenerator myKeyGenerator() {
        return new KeyGenerator(){
            @Override
            public Object generate(Object target, Method method, Object... params) {
                return method.getName()+"["+ Arrays.asList(params).toString()+target+"]";
            }
        };
    }
}

三、整合Redis缓存实现

引入坐标

导入依赖

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

当我们添加进redis相关的启动器之后, SpringBoot会使用 RedisCacheConfigratioin 当做生效的自动
配置类进行缓存相关的自动装配,容器中使用的缓存管理器是RedisCacheManager ,
这个缓存管理器创建的Cache为 RedisCache , 进而操控redis进行数据的缓存

RedisAutoConfiguration向容器中导入了两个类RedisTemplate和StringRedisTemplate,
作为Redis客户端分别操作k-v都为对象和k-v都为字符串的值

Redis服务连接配置

在spring.properties指定Redis连接配置

# Redis服务地址
spring.redis.host=127.0.0.1
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码
spring.redis.username=root
# Redis服务器连接密码
spring.redis.password=123456

基于注解应用Redis缓存

与Spring Boot 默认缓存使用方式一致

同样使用@Cacheable、@CachePut、@CacheEvict注解进行增删改查操作

@Service
public class CommentService {
    @Autowired
    private CommentRepository commentRepository;
    @Cacheable(cacheNames = "comment",unless = "#result==null")
    public Comment findCommentById(Integer id){
        Optional<Comment> comment = commentRepository.findById(id);
        if(comment.isPresent()){
        	Comment comment1 = comment.get();
        	return comment1;
        }
        return null;
    } 
	@CachePut(cacheNames = "comment",key = "#result.id")
    public Comment updateComment(Comment comment) {
        commentRepository.updateComment(comment.getAuthor(), comment.getaId());
        return comment;
    } 
	@CacheEvict(cacheNames = "comment")
    public void deleteComment(int comment_id) {
    	commentRepository.deleteById(comment_id);
    }
}

注意如果缓存的是对象要将对象序列化实现Serializable接口

另外,还可以在Spring Boot全局配置文件中配置Redis有效期,示例代码如下:

# 对基于注解的Redis缓存数据统一设置有效期为1分钟,单位毫秒
spring.cache.redis.time-to-live=60000

基于API应用Redis缓存

在Spring Boot整合Redis缓存实现中,除了基于注解形式的Redis缓存实现外,还有一种开发中常用
的方式——基于API的Redis缓存实现。这种基于API的Redis缓存实现,需要在某种业务需求下通过
Redis提供的API调用相关方法实现数据缓存管理;同时,这种方法还可以手动管理缓存的有效期。

下面,通过Redis API的方式讲解Spring Boot整合Redis缓存的具体实现
使用RedisTemplate 或者 StringRedisTemplate

@Service
public class ApiCommentService {
    @Autowired
    private CommentRepository commentRepository;
    @Autowired
    private RedisTemplate redisTemplate;
    
    public Comment findCommentById(Integer id){
        Object o = redisTemplate.opsForValue().get("comment_" + id);
        if(o!=null){
            return (Comment) o;
        }else {
            //缓存中没有,从数据库查询
            Optional<Comment> byId = commentRepository.findById(id);
            if(byId.isPresent()){
                Comment comment = byId.get();
                //将查询结果存入到缓存中,并设置有效期为1天
                redisTemplate.opsForValue().set("comment_"+id,comment,1,TimeUnit.DAYS);
                return comment;
            }else {
                return null;
            }
        }
    } 
    public Comment updateComment(Comment comment) {
        commentRepository.updateComment(comment.getAuthor(), comment.getaId());
        //更新数据后进行缓存更新
        redisTemplate.opsForValue().set("comment_"+comment.getId(),comment);
        return comment;
    } 
    public void deleteComment(int comment_id) {
        commentRepository.deleteById(comment_id);
        redisTemplate.delete("comment_"+comment_id);
    }
}

自定义Redis缓存序列化机制

上面完成了Spring Boot整合Redis进行了数据的缓存管理,
但缓存管理的实体类数据使用的是JDK序列化方式,不便于使用可视化管理工具进行查看和管理。

在这里插入图片描述

自定义RedisTemplate

基于API的Redis缓存实现是使用RedisTemplate模板进行数据缓存操作的,这里打开RedisTemplate类,查看该类的源码信息

public class RedisTemplate<K, V> extends RedisAccessor implements
										 RedisOperations<K, V>,BeanClassLoaderAware {
    // 声明了key、value的各种序列化方式,初始值为空
    @Nullable
    private RedisSerializer keySerializer = null;
    @Nullable
    private RedisSerializer valueSerializer = null;
    @Nullable
    private RedisSerializer hashKeySerializer = null;
    @Nullable
    private RedisSerializer hashValueSerializer = null;
    ...
    // 进行默认序列化方式设置,设置为JDK序列化方式
    public void afterPropertiesSet() {
        super.afterPropertiesSet();
        boolean defaultUsed = false;
        if(this.defaultSerializer == null) {
            this.defaultSerializer =
					 new JdkSerializationRedisSerializer( this.classLoader != null?
            											  this.classLoader:
                                                          this.getClass().getClassLoader());
        }
        ...
    }
     ...
}
    

根据上述源码信息的分析,可以得到以下两个重要的结论:

(1)使用RedisTemplate进行Redis数据缓存操作时,内部默认使用的是JdkSerializationRedisSerializer序列化方式,
所以进行数据缓存的实体类必须实现JDK自带的序列化接口(例如Serializable);

(2)使用RedisTemplate进行Redis数据缓存操作时,如果自定义了缓存序列化方式defaultSerializer,
那么将使用自定义的序列化方式。

自定义序列化器

@Configuration
public class RedisConfig {
    @Bean
    public RedisTemplate<Object, Object> redisTemplate(
											RedisConnectionFactory  redisConnectionFactory) {
    RedisTemplate<Object, Object> template = new RedisTemplate();
    template.setConnectionFactory(redisConnectionFactory);
    // 使用JSON格式序列化对象,对缓存数据key和value进行转换
    Jackson2JsonRedisSerializer jacksonSeial = 
							new Jackson2JsonRedisSerializer(Object.class);
    // 解决查询缓存转换异常的问题
    ObjectMapper om = new ObjectMapper();
    om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
    jacksonSeial.setObjectMapper(om);
    // 设置RedisTemplate模板API的序列化方式为JSON
    template.setDefaultSerializer(jacksonSeial);
    return template;
    }
}
自定义RedisCacheManager

刚刚针对基于 API方式的RedisTemplate进行了自定义序列化方式的改进,从而实现了JSON序列化
方式缓存数据,但是这种自定义的RedisTemplate对于基于注解的Redis缓存来说,是没有作用的。

打开Spring Boot整合Redis组件提供的缓存自动配置类RedisCacheConfiguration,
查看该类的源码信息,其核心代码如下

@Configuration
@ConditionalOnClass(RedisConnectionFactory.class)
@AutoConfigureAfter(RedisAutoConfiguration.class)
@ConditionalOnBean(RedisConnectionFactory.class)
@ConditionalOnMissingBean(CacheManager.class)
@Conditional(CacheCondition.class)
class RedisCacheConfiguration {
	@Bean
	public RedisCacheManager cacheManager(RedisConnectionFactory redisConnectionFactory,
															ResourceLoader resourceLoader) {
        
		RedisCacheManagerBuilder builder = RedisCacheManager.builder(redisConnectionFactory)
				.cacheDefaults(determineConfiguration(resourceLoader.getClassLoader()));
        
		List<String> cacheNames = this.cacheProperties.getCacheNames();
		if (!cacheNames.isEmpty()) {
			builder.initialCacheNames(new LinkedHashSet<>(cacheNames));
		}
		return this.customizerInvoker.customize(builder.build());
        
	}
	private RedisCacheConfiguration determineConfiguration(ClassLoader classLoader) {
		if (this.redisCacheConfiguration != null) {
			return this.redisCacheConfiguration;
		}
		Redis redisProperties = this.cacheProperties.getRedis();
		RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
        //默认使用Jdk序列化器
		config = config.serializeValuesWith(
				SerializationPair.fromSerializer(new JdkSerializationRedisSerializer(classLoader)));
		
        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;
	}

}

从上述核心源码中可以看出,同RedisTemplate核心源码类似,
RedisCacheConfiguration内部同样使用了JdkSerializationRedisSerializer序列化方式

如果想要使用自定义序列化方式的RedisCacheManager进行数据缓存操作,可以参考上述核心代码
创建一个名为cacheManager的Bean组件,并在该组件中设置对应的序列化方式即可

	
@Bean
public RedisCacheManager cacheManager(RedisConnectionFactory  redisConnectionFactory) {
    // 分别创建String和JSON格式序列化对象,对缓存数据key和value进行转换
    RedisSerializer<String> strSerializer = new StringRedisSerializer();
    Jackson2JsonRedisSerializer jacksonSeial =
                                            new Jackson2JsonRedisSerializer(Object.class);
    // 解决查询缓存转换异常的问题
    ObjectMapper om = new ObjectMapper();
    om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
    jacksonSeial.setObjectMapper(om);
    // 定制缓存数据序列化方式及时效
    RedisCacheConfiguration config =
                            RedisCacheConfiguration.defaultCacheConfig()
                            .entryTtl(Duration.ofDays(1))
                            .serializeKeysWith(RedisSerializationContext.SerializationPair
                            .fromSerializer(strSerializer))
                            .serializeValuesWith(RedisSerializationContext.SerializationPair
                            .fromSerializer(jacksonSeial))
                            .disableCachingNullValues();
    RedisCacheManager cacheManager = RedisCacheManager
                    .builder(redisConnectionFactory).cacheDefaults(config).build();
    return cacheManager;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值