首页 > 数据库 >Spring Cache使用方式——不用默认,使用redis进行缓存

Spring Cache使用方式——不用默认,使用redis进行缓存

时间:2023-04-06 21:11:49浏览次数:41  
标签:缓存 Spring Cache redis value user key import id

在Spring Boot项目中使用Spring Cache的操作步骤(使用redis缓存技术)

1、导入Maven坐标

  spring-boot-starter-data-redis、sping-boot-starter-cache

2、配置application.yml

   spring:

    cache:

      redis:

        time-to-live: 1800000 #设置缓存有效期单位:ms

3、在启动类上加入@EnableCaching注解,开启缓存注解功能

4、在Controller的方法上加入@Cacheable、@CacheEvict等注解,进行缓存操作

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")
    //unless:满足条件时不缓存
    @Cacheable(value = "userCache", key = "#id", unless = "#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,redis,value,user,key,import,id
From: https://www.cnblogs.com/fxzm/p/17294167.html

相关文章

  • Spring Cache使用
    packagecom.itheima.controller;importcom.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;importcom.itheima.entity.User;importcom.itheima.service.UserService;importlombok.extern.slf4j.Slf4j;importorg.springframework.beans.factory.a......
  • SpringBoot 启动优化
    0背景公司SpringBoot项目在日常开发过程中发现服务启动过程异常缓慢,常常需要6-7分钟才能暴露端口,严重降低开发效率。通过SpringBoot的SpringApplicationRunListener、BeanPostProcessor原理和源码调试等手段排查发现,在Bean扫描和Bean注入这个两个阶段有很大的性能瓶颈......
  • Springboot+HTML5+Layui2.7.6上传文件【请求上传接口出现异常】
    1.最近两天在springboot+html5项目中发现在用layui框架时报请求上传接口出现异常这个错误。2.将代码全部整理了一遍,发现前端后台都没错!!!但是还是【请求上传接口出现异常】,于是跑去翻看layui官网。 3.最终最终将错误锁定到了返回的JSON字符串中,我是返回的String,所以一直都会......
  • redis基础数据结构详解
    一.redis为什么快基于内存的存储虽然是单线程,但是采取了多路复用,可以高效的处理网络并发良好的数据结构设计二.redis基础数据结构redis有五种基础的数据结构string,list,set,zset,hashredis所有的数据结构的key都是string类型,我们所说的数据结构都是指value的数据结构......
  • SpringBoot之整合Druid数据源
    1.引入依赖<!--jdbc相关的依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency><!--druid数据源依赖--><dependency><grou......
  • spring属性注入常用注解
       ......
  • SpringBoot中集成Dubbo,实现服务注册和发现
    SpringBoot中集成DubboDubbo实现服务注册和发现用于理解和学习Dubbo工作原理、和集成方法版本说明Springboot版本2.3.9.RELEASEdubbo-spring-boot-starter版本<dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-spring-boot-starter</artif......
  • 10-springcloud-eureka-6-Eureka 与 Zookeeper 的比较
    Eureka与Zookeeper的比较著名的CAP理论指出,一个分布式系统不可能同时满足C(一致性)、A(可用性)和P(分区容错性);由于分区容错性在是分布式系统中必须要保证的,因此我们只能在A和C之间进行权衡,在此Zookeeper保证的是CP,而Eureka则是AP。Zookeeper保证CP在Z......
  • 11-springcloud-eureka-7-Eureka 注册中心高可用集群
    在微服务架构的这种分布式系统中,我们要充分考虑各个微服务组件的高可用性问题,不能有单点故障,由于注册中心eureka本身也是一个服务,如果它只有一个节点,那么它有可能发生故障,这样我们就不能注册与查询服务了,所以我们需要一个高可用的服务注册中心,这就需要通过注册中心集群来解决。......
  • Spring学习第一天
    学习Spring第一天Spring是什么 .spring是一个开源框架,为了解决企业应用的复杂性而创建的,在现在不止以用于企业在一些个人网站上使用了, .是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架    - 从大小与开销两方面而言Spring都是轻量级的   -通过控制反转(IoC)......