首页 > 其他分享 >Spring Cache使用

Spring Cache使用

时间:2023-04-06 20:47:43浏览次数:35  
标签:缓存 Spring Cache value user key 使用 import id

package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.entity.User;
import com.itheima.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private CacheManager cacheManager;

    @Autowired
    private UserService userService;


    /**
     * CachePut:将方法返回值放入缓存【基础环境使用map缓存】
     * value:缓存名称,每个缓存名称下面可以有多个key
     * key:缓存的key
     * @param user
     * @return
     */
    @CachePut(value = "userCache", key = "#result.id")
    @PostMapping
    public User save(User user){
        userService.save(user);
        return user;
    }

    /**
     * CacheEvict:清理指定缓存
     * value:缓存的名称,每个缓存名称下面可以有多个key
     * key:缓存的key
     * @param id
     */
    @CacheEvict(value = "userCache", key = "#id")
    //@CacheEvict(value = "userCache", key = "#p0")
    //@CacheEvict(value = "userCache", key = "#root.args[0]")
    @DeleteMapping("/{id}")
    public void delete(@PathVariable Long id){
        userService.removeById(id);
    }

    @CacheEvict(value = "userCache", key = "#result.id")
    //@CacheEvict(value = "userCache", key = "#p0.id")
    //@CacheEvict(value = "userCache", key = "#user.id")
    //@CacheEvict(value = "userCache", key = "#root.args[0].id")
    @PutMapping
    public User update(User user){
        userService.updateById(user);
        return user;
    }

    /**
     * Cacheable:在方法执行前spring先查看缓存中是否有数据,如果有数据,则直接返回缓存数据;若没有数据,调用方法并将方法返回值放到缓存中
     * value:缓存的名称,每个缓存名称下面可以有多个key
     * key:缓存的key
     * condition:满足条件时才缓存数据
     *  @param id
     * @return
     */
    @Cacheable(value = "userCache", key = "#id", condition = "#result != null")
    @GetMapping("/{id}")
    public User getById(@PathVariable Long id){
        User user = userService.getById(id);
        return user;
    }

    @Cacheable(value = "userCache", key = "#user.id + '_' + #user.name")
    @GetMapping("/list")
    public List<User> list(User user){
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(user.getId()!=null, User::getId, user.getId());
        queryWrapper.eq(user.getName()!=null, User::getName, user.getName());
        List<User> list = userService.list(queryWrapper);
        return list;
    }
}

 

标签:缓存,Spring,Cache,value,user,key,使用,import,id
From: https://www.cnblogs.com/fxzm/p/17294095.html

相关文章

  • 使用open live writer客户端写博客(亲测有效)
    博客都开了这么久了,才开始将资料上传,但是每次都要登录网页确实很麻烦,所以就用openlivewriter,使用起来真的是挺方便的,所以将我在安装配置时,发现的问题汇总起来以便日后再次碰到忘记怎么处理了,哈哈,我记性不好 一:安装                              ......
  • 使用TableLayout应该注意的地方
    4.0之后推荐使用GridLayout代替TableLayout详情见:浅谈android4.0开发之GridLayout布局TableLayout和我们平时在网页上见到的Table有所不同,TableLayout没有边框的,它是由多个TableRow对象组成,每个TableRow可以有0个或多个单元格,每个单元格就是一个View。......
  • 使用DbContext.Set<TEntity>()方法也可以对数据库实体进行CRUD操作
    我们可以用DbContext.Set<TEntity>()方法获取到一个DbSet<TEntity>对象,从而对泛型TEntity类所代表的数据库表进行CRUD操作。例如我们现在有数据库表和TEntity类Person,那么下面两种写法是完全等价的:dbContext.Persons.Take(10).ToList();//dbContext.Persons等于dbContext.Set<P......
  • 使用Kettle定时从数据库A刷新数据到数据库B
    一、需求背景由于项目场景原因,需要将A库(MySQL)中的表a、表b、表c中的数据定时T+1增量的同步到B库(MySQL)。这里说明一下,不是数据库的主从备份,就是普通的数据同步。经过技术调研,发现Kettle挺合适的,原因如下:Kettle(数据抽取、清洗、转换、装载)是由java编写,可以在Window、Lin......
  • SpringBoot 启动优化
    0背景公司SpringBoot项目在日常开发过程中发现服务启动过程异常缓慢,常常需要6-7分钟才能暴露端口,严重降低开发效率。通过SpringBoot的SpringApplicationRunListener、BeanPostProcessor原理和源码调试等手段排查发现,在Bean扫描和Bean注入这个两个阶段有很大的性能瓶颈......
  • 快速学习反假币教程(仅供交流使用)
    仅供交流使用,如有侵权请联系[email protected] 反假币跳过十五分钟(仅供交流使用)以edge浏览器为例1.打开edge浏览器输入:http://wechat.renzhenwh.com/studyExam/examLogin2.按电脑F12打开开发者工具3.在edge上输入账号密码登录4.查看开发者工具,进行如下操作,复制Cookie后......
  • Springboot+HTML5+Layui2.7.6上传文件【请求上传接口出现异常】
    1.最近两天在springboot+html5项目中发现在用layui框架时报请求上传接口出现异常这个错误。2.将代码全部整理了一遍,发现前端后台都没错!!!但是还是【请求上传接口出现异常】,于是跑去翻看layui官网。 3.最终最终将错误锁定到了返回的JSON字符串中,我是返回的String,所以一直都会......
  • WPF的控件字符串内容使用StringFormat进行字符串转换
    在WPF中TextBlock的Text有时内容只需要改变个别数字,而不需要所以内容都修改,这时候就要使用StringFormat, 如:<TextBlockText="Ihavexxxfriends"/>这里面的xxx是个变量,那在Binding时应该怎样写呢<TextBlockText="{BindingFirendNumber,StringFormat='Ihave{0}firends......
  • 使用 LifseaOS 体验 ACK 千节点分钟级扩容
    作者:阿里云ACK和操作系统团队三年前的云栖大会上,LifseaOS正式发布,这是一款专为云原生场景而垂直优化的操作系统发行版,即业界统称的ContainerOS。初始发布时,它提供了如下几个突出的特性:轻量(Lightweight)、快速(Fast)、安全(Secure)、镜像原子管理(Atomic)。如今LifseaOS已在阿里云容......
  • vim编辑器的使用
    1、vi\vim编辑器介绍vi\vim是visualinterface的简称,是linux中最经典的文本编辑器同同行界面中的文编编辑器一样,vi\vim是命令行下对文本进行编辑的绝佳选择vim是vi的加强版本,兼容所有指令,不仅能编辑文本,而且还能执行shell程序编辑的功能,可以不同颜色的字体来辨别语......