SpringBoot与缓存

一. JSR107

Java Caching定义了5个核心接口,分别是CachingProvider, CacheManager, Cache, Entry 和 Expiry。

  • CachingProvider定义了创建、配置、获取、管理和控制多个CacheManager。一个应用可以在运行期访问多个CachingProvider。
  • CacheManager定义了创建、配置、获取、管理和控制多个唯一命名的Cache,这些Cache存在于CacheManager的上下文中。一个CacheManager仅被一个CachingProvider所拥有。
  • Cache是一个类似Map的数据结构并临时存储以Key为索引的值。一个Cache仅被一个CacheManager所拥有。
  • Entry是一个存储在Cache中的key-value对。
  • Expiry每一个存储在Cache中的条目有一个定义的有效期。一旦超过这个时间,条目将不可访问、更新和删除。缓存有效期可以通过ExpiryPolicy设置。

在这里插入图片描述

二. Spring缓存抽象

Spring定义了org.springframework.cache.Cache
和org.springframework.cache.CacheManager接口来统一不同的缓存技术;并支持使用JCache(JSR-107)注解简化我们开发;

  • Cache接口为缓存的组件规范定义,包含缓存的各种操作集合;
  • Cache接口下Spring提供了各种xxxCache的实现;如RedisCache,EhCacheCache , ConcurrentMapCache等;
  • 每次调用需要缓存功能的方法时,Spring会检查检查指定参数的指定的目标方法是否已经被调用过;如果有就直接从缓存中获取方法调用后的结果,如果没有就调用方法并缓存结果后返回给用户。下次调用直接从缓存中获取。
  • 使用Spring缓存抽象时我们需要关注以下两点;
    1. 确定方法需要被缓存以及他们的缓存策略
    2. 从缓存中读取之前缓存存储的数据

在这里插入图片描述

三. 缓存注解

概念解释
Cache缓存接口,定义缓存操作。实现有:RedisCache、EhCacheCache、ConcurrentMapCache等
CacheManager缓存管理器,管理各种缓存(Cache)组件
@Cacheable主要针对方法配置,能够根据方法的请求参数对其结果进行缓存
@CacheEvict清空缓存
@CachePut保证方法被调用
@EnableCaching开启基于注解的缓存
keyGenerator缓存数据时key生成策略
serialize缓存数据时value序列化策略

1. 代码演示

1.1 导入相应的maven依赖
<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
1.2 开启基于注解的缓存
@SpringBootApplication
@EnableCaching //开启基于注解的缓存
public class Springboot01CacheApplication {
    public static void main(String[] args) {
        SpringApplication.run(Springboot01CacheApplication.class, args);
    }
}
1.3 在service层上添加相应注解
  • @Cacheable: 默认根据方法的请求参数对结果进行缓存,属性:

    • cacheNames/value:指定缓存组件的名字;将方法的返回结果放在哪个缓存中,是数组的方式,可以指定多个缓存;
    • key:缓存数据使用的key;可以用它来指定。默认是使用方法参数的值。可以编写SpEL表达式;
    • keyGenerator:key的生成器;可以自己指定key的生成器的组件id。key/keyGenerator:二选一使用;
    • cacheManager:指定缓存管理器;或者cacheResolver指定获取解析器;
    • condition:指定符合条件的情况下才缓存;(例如: condition = “#a0>1”:第一个参数的值>1的时候才进行缓存)
    • unless:否定缓存;当unless指定的条件为true,方法的返回值就不会被缓存;(例如:unless = “#a0==2”:如果第一个参数的值是2,结果不缓存;)
    • sync:是否使用异步模式
  • @Cacheable标注的方法执行之前先来检查缓存中有没有这个数据,默认按照参数的值作为key去查询缓存,如果没有就运行方法并将结果放入缓存;以后再来调用就可以直接使用缓存中的数据;

@Cacheable(value = {"emp"}/*,keyGenerator = "myKeyGenerator",condition = "#a0>1",unless = "#a0==2"*/)
public Employee getEmp(Integer id){
    System.out.println("查询"+id+"号员工");
    Employee emp = employeeMapper.getEmpById(id);
    return emp;
}
  • @CachePut: 既调用方法,又更新缓存数据;同步更新缓存,修改了数据库的某个数据,同时更新缓存;运行时机:①先调用目标方法,②将目标方法的结果缓存起来。注意:更新时是根据指定的key属性进行的更新。
@CachePut(/*value = "emp",*/key = "#result.id")
public Employee updateEmp(Employee employee){
    System.out.println("updateEmp:"+employee);
    employeeMapper.updateEmp(employee);
    return employee;
}
  • @CacheEvict: 缓存清除,属性:
    • key:指定要清除的数据
    • allEntries = true:指定清除这个缓存中所有的数据
    • beforeInvocation = false:缓存的清除是否在方法之前执行, 默认代表缓存清除操作是在方法执行之后执行;如果出现异常缓存就不会清除
    • beforeInvocation = true:代表清除缓存操作是在方法运行之前执行,无论方法是否出现异常,缓存都清除
@CacheEvict(value="emp",beforeInvocation = true/*key = "#id",*/)
public void deleteEmp(Integer id){
    System.out.println("deleteEmp:"+id);
    //employeeMapper.deleteEmpById(id);
    int i = 10/0;
}
  • @Caching: 定义复杂的缓存规则
  • 以该例说明,因为指定了@CachePut,所以该方法一定会被执行。
@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: 可以抽取缓存的公共配置
@CacheConfig(cacheNames="emp"/*,cacheManager = "employeeCacheManager"*/) //抽取缓存的公共配置
@Service
public class EmployeeService {}

注意:
@Cacheable:是在方法执行之前先检查缓存
@CachePut:是在方法执行之后再将结果放入缓存

四. 缓存原理

1. 原理

  • 1.自动配置类;CacheAutoConfiguration
  • 2.缓存的配置类:
    • org.springframework.boot.autoconfigure.cache.GenericCacheConfiguration
    • org.springframework.boot.autoconfigure.cache.JCacheCacheConfiguration
    • org.springframework.boot.autoconfigure.cache.EhCacheCacheConfiguration
    • org.springframework.boot.autoconfigure.cache.HazelcastCacheConfiguration
    • org.springframework.boot.autoconfigure.cache.InfinispanCacheConfiguration
    • org.springframework.boot.autoconfigure.cache.CouchbaseCacheConfiguration
    • org.springframework.boot.autoconfigure.cache.RedisCacheConfiguration
    • org.springframework.boot.autoconfigure.cache.CaffeineCacheConfiguration
    • org.springframework.boot.autoconfigure.cache.GuavaCacheConfiguration
    • org.springframework.boot.autoconfigure.cache.SimpleCacheConfiguration【默认】
    • org.springframework.boot.autoconfigure.cache.NoOpCacheConfiguration
  • 3.默认配置类生效:SimpleCacheConfiguration
  • 4.当没有CacheManager时,容器默认使用的是ConcurrentMapCacheManager
  • 5.可以获取和创建ConcurrentMapCache类型的缓存组件;他的作用将数据保存在ConcurrentMap中

2. @Cacheable运行流程

  • 1.方法运行之前,先去查询Cache(缓存组件),按照cacheNames指定的名字获取;(CacheManager先获取相应的缓存),第一次获取缓存如果没有Cache组件会自动创建。
  • 2.去Cache中查找缓存的内容,使用一个key,默认就是方法的参数;key是按照某种策略生成的;默认是使用keyGenerator生成的,默认使用SimpleKeyGenerator生成key;
  • 3.没有查到缓存就调用目标方法;
  • 4.将目标方法返回的结果,放进缓存中

五. 整合Redis

1. 导入相应的maven依赖

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

2. 配置相应的Redis连接地址

spring.redis.host=XXX.XXX.XXX.XXX(ip地址)

3. 操作Redis

3.1 StringRedisTemplate(k-v都是字符串)
stringRedisTemplate.opsForValue()//[String(字符串)]
stringRedisTemplate.opsForList()//[List(列表)]
stringRedisTemplate.opsForSet()//[Set(集合)]
stringRedisTemplate.opsForHash()//[Hash(散列)]
stringRedisTemplate.opsForZSet()//[ZSet(有序集合)]
3.2 RedisTemplat(k-v都是对象的)
redisTemplate.opsForValue()
redisTemplate.opsForList()
redisTemplate.opsForSet()
redisTemplate.opsForHash()
redisTemplate.opsForZSet()

4. 配置缓存

4.1 切换Redis的序列化规则
  • Redis默认的序列化默认使用的是JDK的序列化,因此我们需要转换为json的序列化规则,方便我们查看。
@Bean
public RedisTemplate<Object, Employee> empRedisTemplate(
        RedisConnectionFactory redisConnectionFactory)
        throws UnknownHostException {
    RedisTemplate<Object, Employee> template = new RedisTemplate<Object, Employee>();
    template.setConnectionFactory(redisConnectionFactory);
    Jackson2JsonRedisSerializer<Employee> ser = new Jackson2JsonRedisSerializer<Employee>(Employee.class); //切换为json的序列化器
    template.setDefaultSerializer(ser);
    return template;
}
4.2 自定义CacheManager
  • 当我们自定义CacheManager时,默认的CacheManager就失效了
  • 当有多个CacheManager时,使用@Primary注解标注某个缓存管理器是默认的。
@Primary  //将某个缓存管理器作为默认的
@Bean
public RedisCacheManager employeeCacheManager(RedisTemplate<Object, Employee> empRedisTemplate){
    RedisCacheManager cacheManager = new RedisCacheManager(empRedisTemplate);
    //key多了一个前缀
    //使用前缀,默认会将CacheName作为key的前缀
    cacheManager.setUsePrefix(true);
    return cacheManager;
}
@Bean
public RedisCacheManager deptCacheManager(RedisTemplate<Object, Department> deptRedisTemplate){
    RedisCacheManager cacheManager = new RedisCacheManager(deptRedisTemplate);
    cacheManager.setUsePrefix(true);
    return cacheManager;
}
Bean
public RedisCacheManager deptCacheManager(RedisTemplate<Object, Department> deptRedisTemplate){
    RedisCacheManager cacheManager = new RedisCacheManager(deptRedisTemplate);
    cacheManager.setUsePrefix(true);
    return cacheManager;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值