zoukankan      html  css  js  c++  java
  • springboot缓存之@CachePut注解

    接上一节。

    @CachePut:既调用方法,又更新缓存。

    使用场景:当更改了数据库的某个数据,同时也更新缓存。

    运行时机:先调用目标方法,然后将结果放入缓存。

    package com.gong.springbootcache.controller;
    
    import com.gong.springbootcache.bean.Employee;
    import com.gong.springbootcache.service.EmployeeService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.cache.annotation.CachePut;
    import org.springframework.cache.annotation.Cacheable;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    @Controller
    public class EmployeeController {
    
        @Autowired
        EmployeeService employeeService;
    
        //value:指定缓存的名字,每个缓存组件有一个唯一的名字。缓存组件由CacheManager进行管理。
        //key:缓存数据时用到的key,默认使用方法参数的值,1-方法返回值 key =
        //#id也可这么表示:#root.args[0](第一个参数)
        //keyGenerator:指定生成缓存的组件id,使用key或keyGenerator其中一个即可
        //cacheManager,cacheResolver:指定交由哪个缓存管理器,使用其中一个参数即可
        //condition:指定符合条件时才进行缓存
        //unless:当unless指定的条件为true,方法的返回值就不会被缓存
        //sync:是否使用异步模式
        //"#root.methodName+'[+#id+]'"
        @Cacheable(value = "emp")
        @ResponseBody
        @RequestMapping("/emp/{id}")
        public Employee getEmp(@PathVariable("id") Integer id){
            Employee emp = employeeService.getEmp(id);
            return emp;
        }
    
        @CachePut(value = "emp")
        @GetMapping("/emp")
        public Employee updateEmp(Employee employee){
            Employee emp =  employeeService.updateEmp(employee);
            return emp;
        }
    }

    第一次查询:

    没有使用缓存。

    第二次查询:使用到了缓存,不必再发送sql。

    然后进行更新:

    先执行了方法,也就是会发送sql:

    然后我们再执行一次查询:

    此时我们查询出来的是从缓存中获取的,但是,为什么缓存没有进行更新呢?

    这是因为getEmp中参数为id,updateEmp参数中为employee。我们知道,如果使用缓存时不指定缓存的key,也就是设置key或者keyGenerator属性,那么,缓存的key就是方法中参数的组合,所以,若果要实现同步的话,我们要指定缓存的key为相同的值。

    我们只需要将updateEmp方法中的CachePut注解中加上:key="#employee.id"。

    此时再按照之前的步骤再来一次,记得先把2号员工的数据先改回去,在修改后再进行查询:

    得到了我们想要的值,同时也没有发送sql。 

  • 相关阅读:
    windows(64位)下使用curl命令
    ThinkPHP 3.2 性能优化,实现高性能API开发
    如何摆脱恨死人的低价竞争对手
    火狐浏览器如何js关闭窗口的几种解决方法
    当 Swoole 遇上 ThinkPHP5 世界你好
    TCP网络编程杂谈
    SQL语句操作优先级顺序
    记一次常规的Mysql数据库访问的时间分析
    CSS艺术字
    Eclipse Oxygen创建maven web项目(二)
  • 原文地址:https://www.cnblogs.com/xiximayou/p/12290877.html
Copyright © 2011-2022 走看看