zoukankan      html  css  js  c++  java
  • Spring Boot的缓存之旅(一)

    使用Spring Cache

    1.Spring Cache简介

    Spring Cache是 Spring3.1以后引入的新技术。它井不像正常缓存那样存储数据,其核心思想是这样的:当我们在调用一个缓存方法时,会把该方法参数和返回结果作为一个键值对存放在缓存 中,等到下次利用同样的参数来调用该方法时将不再执行该方法,而是直接从缓存中获取结果进行 返回,从而实现缓存的功能。 Spring Cache的使用和 Spring对于事务管理的使用类似,可以基于注 解使用或者基于XML配置方式使用。

    2.基于注解学习Spring Cache

    在 Spring中提供了3个注解来使用 Spring Cache,下面分别进行介绍。

    ①@Cacheable注解

    :用于标记缓存,也就是对使用 @Cacheable注解的位置进行缓存。

    Cacheable可以在方法或者类上进行标记,当对方法进行标记时,表示此方法支持缓存:当对类进行标记时, 表明当前类中所有的方法都支持缓存

    在支持 Spring Cache的环境下,对于使用 a cacheable标记 的方法, Spring在每次调用方法前都会根据key查询当前 Cache中是否存在相同key的缓存元素, 如果存在,就不再执行该方法,而是直接从缓存中获取结果进行返回,否则执行该方法并将返回结果存入指定的缓存中。

    在使用 @Cacheable时通常会搭配3个属性进行使用,分别介绍如下。

    ·value:在使用@ Cacheable注解的时候,val属性是必须要指定的,这个属性用于指定 Cache 的名称,也就是说表明当前缓存的返回值用于哪个缓存上

    ·key:和名称一样,用于指定缓存对应的key.key属性不是必须指定的,如果我们没有指定 key,spring就会为我们使用默认策略生成的key,其中默认策略是这样规定的:如果当前缓存方法没有参数,那么当前key为0;如果当前缓存方法有一个参数,那么以key为参数值 ;如果当前缓存方法有多个参数,那么key为所有参数的 hashcode值。当然,我们也可以通过 Spring提供的EL表达式来指定当前缓存方法的key.通常来说,我们可以使用当前缓存方法 的参数指定key,一般为“#参数名”,如果参数为对象,就可以使用对象的属性指定key,例如:

    @Cacheable(value = "users", key = "#user.id")
        public User findUser(User user){
            return new User();
        }

    同时, Spring框架还为我们提供了root对象来使用key属性,在指定key属性时可以忽略#root
    因为 Spring默认调用的就是root对象的属性,其中root对象分别内置如下几个属性
    (1) method Name:当前方法的名称,key值为# rootmetho. Name或 methodName.
    (2) method:指定当前方法,key值为# root.method.name或 method.name
    (3) target:当前被调用的对象,key值为 #root.target或 target
    (4) targetClass:当前被调用的对象的class,key值为 #root. targetClass或 targetClass
    (5)angs:当前方法参数组成的数组,key值为 #roo. args[o]或 args[0]
    (6) caches:当前被调用的方法使用的 Cache,key值为 #root.caches[0].name或 caches[0].name②
    ·condition:主要用于指定当前缓存的触发条件,很多情况下可能并不需要使用所有缓存的方法进行缓存,所以 Spring Cache为我们提供了这种属性来排除一些特定的情况。例如

    @Cacheable(value = "users", key = "#user.id",condition="#user.id%2==0")
        public User findUser(User user){
            return new User();
        }

    ②@CachePut 注解

    从名称上来看, @CachePut只是用于将标记该注解的方法的返回值放入缓存中,无论缓存中是否包含当前缓存,只是以键值的形式将执行结果放入缓存中。在使用方面,@CachePut注解和 @Cacheable注解一致,这里就不具体介绍了

    ③@CacheEvict 注解

    Spring Cache提供了@CacheEvict注解用于清除缓存数据,与@Cacheable类似,不过 @CacheEvict用于方法时清除当前方法的缓存,用于类时清除当前类所有方法的缓存,@CacheEvict 除了提供与 @CacheEvict一致的3个属性外,还提供了一个常用的属性 allEntries,这个属性的默认值为 false,如果指定属性值为true,就会清除当前 value值的所有缓存。

    3.正式学习

    3.1 配置依赖

    3.2 在启动类上开启缓存@EnableCaching

    3.3 编写Controller

    @RestController
    public class UserController {
    
        @Autowired
        private UserRepository userRepository;
    
    //localhost:8080/saveUser?id=1&userName=dalaoyang&userPassword=123   
        @GetMapping("/saveUser")
        @CachePut(value = "user", key = "#id")
        public User saveUser(Long id, String userName, String userPassword){
            User user = new User(id,userName, userPassword);
            userRepository.save(user);
            return user;
        }
      //localhost:8080/queryUser?id=1
        @GetMapping("/queryUser")
        @Cacheable(value = "user", key = "#id")
        public Optional<User> queryUser(Long id){
            return userRepository.findById(id);
        }
      //localhost:8080/deleteUser?id=1
        @GetMapping("/deleteUser")
        @CacheEvict(value = "user", key = "#id")
        public String deleteUser(Long id){
            userRepository.deleteById(id);
            return "success";
        }
      //localhost:8080/deleteCache
        @GetMapping("/deleteCache")
        @CacheEvict(value = "user", allEntries = true)
        public void deleteCache() {
        }
    }
    经测试,效果都有了。
  • 相关阅读:
    nginx下pagespeed使用详解
    letsencrypt证书-使用certbot申请wildcard证书
    letsencrypt证书-管理工具certbot
    tcpdump使用
    elasticsearch增删改查操作
    elasticsearch安装中文分词器
    dragstart drag dragend dragenter dragover dragleave drop
    js如何准确获取当前页面url网址信息
    /touch滑屏事件
    监听 手机back键和顶部的回退
  • 原文地址:https://www.cnblogs.com/xc-xinxue/p/12417918.html
Copyright © 2011-2022 走看看