SpringBoot(十三)——————JSR-107、Spring缓存抽象、Spring缓存注解

  • 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从3.1开始定义了org.springframework.cache.Cache和org.springframework.cache.CacheManager接口来统一不同的缓存技术;并支持使用JCache(JSR-107)注解简化我们开发;
• Cache接口为缓存的组件规范定义,包含缓存的各种操作集合;
• Cache接口下Spring提供了各种xxxCache的实现;如RedisCache,EhCacheCache ,ConcurrentMapCache等;
• 每次调用需要缓存功能的方法时,Spring会检查检查指定参数的指定的目标方法是否已经被调用过;如果有就直接从缓存中获取方法调用后的结果,如果没有就调用方法并缓存结果后返回给用户。下次调用直接从缓存中获取。
• 使用Spring缓存抽象时我们需要关注以下两点;
1、确定方法需要被缓存以及他们的缓存策略
2、从缓存中读取之前缓存存储的数据

  • Spring缓存注解及相关概念

Cache

缓存接口,定义缓存操作。实现有:RedisCacheEhCacheCacheConcurrentMapCache

CacheManager

缓存管理器,管理各种缓存(Cache)组件

@Cacheable

主要针对方法配置,能够根据方法的请求参数对其结果进行缓存

@CacheEvict

清空缓存

@CachePut

保证方法被调用,又希望结果被缓存。

@EnableCaching

开启基于注解的缓存

keyGenerator

缓存数据时key生成策略

serialize

缓存数据时value序列化策略

  • SpringBooth缓存注解开发

1)、项目搭建

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.wmy.integrate</groupId>
    <artifactId>springboot-integrate-cache-redis</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-integrate-cache-redis</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.1</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
@RestController
public class EmployeeController {

    @Autowired
    EmployeeService employeeService;

    @GetMapping("/emp")
    public Employee insertEmp(Employee employee){
        employeeService.insertEmp(employee);
        return employee;
    }

    @GetMapping("/delEmp/{id}")
    public String delEmp(@PathVariable("id") Integer id){
        employeeService.delEmp(id);
        return "success";
    }

    @GetMapping("/upEpm")
    public Employee updateEmp(Employee employee){
        employeeService.updateEmp(employee);
        return employee;
    }

    @GetMapping("/emp/{id}")
    public Employee getEmp(@PathVariable("id") Integer id){
        Employee employee = employeeService.getEmp(id);
        return employee;
    }
}
@Mapper
public interface EmployeeMapper {

    @Insert("INSERT INTO employee(lastName,email,gender,d_id) VALUES(#{lastName},#{email},#{gender},#{dId})")
    public void insertEmp(Employee employee);

    @Delete("DELETE FROM employee WHERE id=#{id}")
    public void delEmp(Integer id) ;

    @Update("UPDATE employee SET lastName=#{lastName},email=#{email},gender=#{gender},d_id=#{dId} WHERE id=#{id}")
    public void updateEmp(Employee employee) ;

    @Select("SELECT * FROM employee WHERE id = #{id}")
    public Employee getEmp(Integer id) ;
}
@Service
public class EmployeeService {

    @Autowired
    EmployeeMapper employeeMapper;

    public void insertEmp(Employee employee) {
        System.out.println("添加:"+employee);
        employeeMapper.insertEmp(employee);
    }

    public void delEmp(Integer id) {
        System.out.println("删除:"+id);
        employeeMapper.delEmp(id);
    }

    public void updateEmp(Employee employee) {
        System.out.println("更新:"+employee);
        employeeMapper.updateEmp(employee);
    }

    public Employee getEmp(Integer id) {
        System.out.println("查询:"+id);
        Employee employee = employeeMapper.getEmp(id);
        return employee;
    }
}

2)、基于缓存注解开发

将方法运行的结果进行缓存,后续如果再需要相同的数据就不再调用方法直接到缓存里面取

1.注解@Cacheable管理多个cache组件,对缓存进行CRUD的操作在cache组件中。

cacheable的属性:

cacheNames/value指定缓存组件名称、key缓存数据使用的key,默认是方法参数的值,还可以使用SPEL表达式。这里如果一个类中使用同一个缓存名称还可以放在类头上

keyGenerator key的生成器,key和keyGenerator二选一。也可以自己制定keyGenerator

condition指定符合条件下才缓存condition="#id>0"

unless与condition相关,unless="#result==null"

@Cacheable(cacheNames = "emp",key = "#id")  @Cacheable(cacheNames = {"emp","temp",key = "#a0")

@Cacheable/@CachePut/@CacheEvict 主要的参数

value

缓存的名称,在 spring 配置文件中定义,必须指定至少一个

例如:
@Cacheable(value=”mycache”) 或者 
@Cacheable(value={”cache1”,”cache2”}

key

 

缓存的 key,可以为空,如果指定要按照 SpEL 表达式编写,如果不指定,则缺省按照方法的所有参数进行组合

例如:
@Cacheable(value=”testcache”,key=”#userName”)

condition

缓存的条件,可以为空,使用 SpEL 编写,返回 true 或者 false,只有为 true 才进行缓存/清除缓存,在调用方法之前之后都能判断

例如:
@Cacheable(value=”testcache”,condition=”#userName.length()>2”)

allEntries

(@CacheEvict )

是否清空所有缓存内容,缺省为 false,如果指定为 true,则方法调用后将立即清空所有缓存

例如:
@CachEvict(value=”testcache”,allEntries=true)

beforeInvocation

(@CacheEvict)

是否在方法执行前就清空,缺省为 false,如果指定为 true,则在方法还没有执行的时候就清空缓存,缺省情况下,如果方法执行抛出异常,则不会清空缓存

例如:

@CachEvict(value=”testcache”beforeInvocation=true)

unless

(@CachePut)

(@Cacheable)

用于否决缓存的,不像condition,该表达式只在方法执行之后判断,此时可以拿到返回值result进行判断。条件为true不会缓存,fasle才缓存

例如:
@Cacheable(value=”testcache”,unless=”#result == null”)

名字

位置

描述

示例

methodName

root object

当前被调用的方法名

#root.methodName

method

root object

当前被调用的方法

#root.method.name

target

root object

当前被调用的目标对象

#root.target

targetClass

root object

当前被调用的目标对象类

#root.targetClass

args

root object

当前被调用的方法的参数列表

#root.args[0]

caches

root object

当前方法调用使用的缓存列表(如@Cacheable(value={"cache1", "cache2"})),则有两个cache

#root.caches[0].name

argument name

evaluation context

方法参数的名字. 可以直接 #参数名 ,也可以使用 #p0#a0 的形式,0代表参数的索引;

#iban  #a0   #p0 

result

evaluation context

方法执行后的返回值(仅当方法执行之后的判断有效,如‘unless’,’cache put’的表达式 ’cache evict’的表达式beforeInvocation=false)

#result

    @Cacheable(cacheNames = "emp",key = "#id")
    public Employee getEmp(Integer id) {
        System.out.println("查询:"+id);
        Employee employee = employeeMapper.getEmp(id);
        return employee;
    }

2.@Cacheable工作原理

      1.缓存的自动配置类:CacheAutoConfiguration

      2.自动配置类里面导入了CacheConfigurationImportSelector给容器中添加缓存的一些配置类

          0 = "org.springframework.boot.autoconfigure.cache.GenericCacheConfiguration"
                 1 = "org.springframework.boot.autoconfigure.cache.JCacheCacheConfiguration"
                 2 = "org.springframework.boot.autoconfigure.cache.EhCacheCacheConfiguration"
                 3 = "org.springframework.boot.autoconfigure.cache.HazelcastCacheConfiguration"
                 4 = "org.springframework.boot.autoconfigure.cache.InfinispanCacheConfiguration"
                 5 = "org.springframework.boot.autoconfigure.cache.CouchbaseCacheConfiguration"
                 6 = "org.springframework.boot.autoconfigure.cache.RedisCacheConfiguration"
                 7 = "org.springframework.boot.autoconfigure.cache.CaffeineCacheConfiguration"
                 8 = "org.springframework.boot.autoconfigure.cache.SimpleCacheConfiguration"
                 9 = "org.springframework.boot.autoconfigure.cache.NoOpCacheConfiguration"

      3.默认是SimpleCacheConfiguration配置类生效

      4.给容器中注册cacheManager(ConcurrentMapCacheManager)

      5.ConcurrentMapCacheManager获取根据缓存名称获取缓存组件,没有就创建。将缓存保存在ConcurrentMap中。

         方法运行之前会调用以 下方法

public Cache getCache(String name) {
		Cache cache = this.cacheMap.get(name);
		if (cache == null && this.dynamic) {
			synchronized (this.cacheMap) {
				cache = this.cacheMap.get(name);
				if (cache == null) {
					cache = createConcurrentMapCache(name);
					this.cacheMap.put(name, cache);
				}
			}
		}
		return cache;
	}

      6.Key是按照某种策略生成的,使用KeyGenerator生成,默认使用SimpleKeyGenerator

3.@CachePut 既调用方法又更新缓存

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

4.@CacheEvict清楚缓存

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

5.@Caching定义缓存的复杂规则

    @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);
    }

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值