zoukankan      html  css  js  c++  java
  • SpringBoot:基于注解的@CachePut

    SpringBoot基于注解的@CachePut

    几个重要的概念及注解

    名称解释
    Cache 缓存接口,定义缓存操作。实现有:RedisCache、EhCacheCache、ConcurrentMapCache等
    CacheManager 缓存管理器,管理各种缓存(cache)组件
    @Cacheable 主要针对方法配置,能够根据方法的请求参数对其进行缓存
    @CacheEvict 清空缓存
    @CachePut

    保证方法被调用,又希望结果被缓存。
    与@Cacheable区别在于是否每次都调用方法,常用于更新
    无论怎样,@CachePut总将方法的返回值放到缓存中

    @EnableCaching 开启基于注解的缓存
    keyGenerator 缓存数据时key生成策略
    serialize 缓存数据时value序列化策略
    @CacheConfig 统一配置本类的缓存注解的属性
     

      部分代码:

      
     1 //Controller层
     2     @RequestMapping("/select")
     3     @ResponseBody
     4     public String select(int id) {
     5         return gson.toJson(telephonebookService.selectByPrimaryKey(id));
     6     }
     7 
     8     @RequestMapping("/update")
     9     @ResponseBody
    10     public String update(String dp, String dt) {
    11         Telephonebook tbook = new Telephonebook(1,"李四",dp,dt,"FSS");
    12         return gson.toJson(telephonebookService.updateByPrimaryKey(tbook));
    13     }
    14 //ServiceImpl
    15 @CacheConfig(cacheNames = "tbook")
    16 class ……{
    17     ……
    18 
    19     @Override
    20     @CachePut(key = "#record.getId()")
    21     public Telephonebook updateByPrimaryKey(Telephonebook record) {
    22         telephonebookMapper.updateByPrimaryKey(record);
    23         return record;
    24     }
    25     @Override
    26     @Cacheable(key = "#id")
    27     public Telephonebook selectByPrimaryKey(Integer id) {
    28         return telephonebookMapper.selectByPrimaryKey(id);
    29     }
    30 }
    View Code

      @CacheConfig(cacheNames = "tbook") 抽取本类的公共value

      @CachePut 是将“方法返回”的值放入缓存中,被注解的方法返回的值是什么,则它存放的值就是什么

      

  • 相关阅读:
    表单校验神器
    插入排序
    数组去重的几种常使用的方式
    day44 mysql高级部分内容
    day43 多表查询和pymysql
    day42 字段的增删改查详细操作
    day41 mysql详细操作
    day40 mysql数据类型
    day39 mysql数据库基本操作
    day37 异步回调和协程
  • 原文地址:https://www.cnblogs.com/itcod/p/12511126.html
Copyright © 2011-2022 走看看