springboot-cache

首先要在主配置类标上**@EnableCaching**注解,开启cache

@EnableCaching
@MapperScan("com.test.cache.mapper")
@SpringBootApplication
public class Springboot01CacheApplication {

	public static void main(String[] args) {
		SpringApplication.run(Springboot01CacheApplication.class, args);
	}

}

组成

在这里插入图片描述
在这里插入图片描述
CacheManager负责创建,管理,获取各个Cache。可以是Radis一类的缓存。

Cache就是CacheManager里面所存储的缓存。里面存储着各种KV键值对。cacheNames可以指定缓存名字。里面存储与该cacheNames相关的KV键值对。

各个注解

在这里插入图片描述

@Cacheable

  1. 注解先执行(先去缓存查看该cacheNames 下,key值是否存在,存在则直接调用缓存,不去执行该方法
  2. key值默认使用方法参数,可以使用key字段进行设置。
  3. 使用CacheManager指定缓存组件。
  4. key使用keyGenerator生成的,默认是SimpleKeyGenerator
@Cacheable(cacheNames = "emp"/*, keyGenerator = "myKeyGenerator" , condition = "#a0>1"*/)
    public Employee getEmp(Integer id) {
        System.out.println("查询" + id + "号员工");
        Employee empById = employeeMapper.getEmpById(id);
        return empById;

    }
    

几个属性:
* cacheNames/value:指定缓存组件的名字;将方法的返回结果放在哪个缓存中,是数组的方式,可以指定多个缓存;
*
* key:缓存数据使用的key;可以用它来指定。默认是使用方法参数的值 1-方法的返回值
* 编写SpEL; #i d;参数id的值 #a0 #p0 #root.args[0]
* getEmp[2]
*
* keyGenerator:key的生成器;可以自己指定key的生成器的组件id
* key/keyGenerator:二选一使用;
*
*
* cacheManager:指定缓存管理器;或者cacheResolver指定获取解析器
*
* condition:指定符合条件的情况下才缓存;
* ,condition = “#id>0”
* condition = “#a0>1”:第一个参数的值》1的时候才进行缓存
*
* unless:否定缓存;当unless指定的条件为true,方法的返回值就不会被缓存;可以获取到结果进行判断
* unless = “#result == null”
* unless = “#a0==2”:如果第一个参数的值是2,结果不缓存;
* sync:是否使用异步模式

@CachePut

  1. 先调用目标方法
  2. 将目标方法的结果缓存起来
  3. 既调用方法,又更新缓存数据;同步更新缓存;修改了数据库的某个数据,同时更新缓存;
@CachePut(value = "emp"/*,key = "#employee.id"*/ , key = "#result.id")
    public Employee updateEmp(Employee employee){
        System.out.println("update : " + employee);
        employeeMapper.updateEmp(employee);
        return employee;
    }

@CacheEvict

  1. 默认方法执行后再执行,可以使用 beforeInvocation = true 修改。
  2. 可以指定value和key清理,也可以使用allEntries = true,对value进行清理。
@CacheEvict(value = "emp"/*, key = "#id"*/ , allEntries = true/*, beforeInvocation = true*/)
    public void deleteEmp(Integer id){
        System.out.println("deleteEmp : " + id);
//        employeeMapper.deleteEmpById(id);
    }
  • @CacheEvict:缓存清除
    • key:指定要清除的数据
    • allEntries = true:指定清除这个缓存中所有的数据
    • beforeInvocation = false:缓存的清除是否在方法之前执行
    •  默认代表缓存清除操作是在方法执行之后执行;如果出现异常缓存就不会清除
      
    • beforeInvocation = true:
    •  代表清除缓存操作是在方法运行之前执行,无论方法是否出现异常,缓存都清除
      

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

    }

@CacheConfig

可以在类的开头指定全局配置


@CacheConfig(cacheNames="emp"/*,cacheManager = "employeeCacheManager"*/) //抽取缓存的公共配置
@Service
public class EmployeeService {}

配置文件

#设置驼峰命名转换(数据库是d_id,读取数据是可以自动转换为dId)
mybatis.configuration.map-underscore-to-camel-case=true

#设置日志级别
logging.level.com.test.cache.mapper=debug

#在控制台查看springboot自动配置的服务
debug=true
发布了15 篇原创文章 · 获赞 1 · 访问量 374

猜你喜欢

转载自blog.csdn.net/qq_24819841/article/details/105026924
今日推荐