SpringCache框架

前言

记得我们Redis缓存吗?可以帮数据库分担压力已经大大降低了高并发,我们这次在Redis基础上再次优化给大家提供了一个框架SpringCache

一、SpringCache是什么?

SpringCache是一个框架,实现了基于注解的缓存功能,由原来的各总注入对象反射变成只需要简单地加入一个注解,就能实现缓存功能

具体就是通过CacheManager接口来统一不同的缓存技术。CacheManager是Spring提供的各种缓存技术抽象接口。

二、使用步骤

1.引入相关依赖

依赖如下:

    我们不仅要引入SpringCache的依赖,Reids也要引入

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-cache</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

2.加入指定注解

首先先给大家介绍下SpringCache的注解:

@EnableCaching: 开启缓存功能,放在引导类上
@Cacheable:  先从缓存中获取数据,能获取到直接返回结果,获取不到调用方法
    : [
         condition: 条件,满足条件时才缓存数据,
         unless属性: 满足条件不缓存数据
      ]
@CacheEvict:  清理缓存
    : (allEntries: 值为true,表示清理该分类下的所有数据)
@CachePut: 方法上面,将方法的返回值存储到缓存中
 

这些都是些最常用的几个注解

3.代码中如何使用:

  代码Controller中的这段非重点,只是来查看一下缓存

@Autowired

private CacheManager cacheManager;

引导类上加入注解如下:

@Slf4j
@SpringBootApplication
@EnableCaching //开启注解式缓存
public class CacheDemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(CacheDemoApplication.class,args);
        log.info("项目启动成功...");
    }
}

控制层Controller使用如下:

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.ArrayList;
import java.util.List;

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

    @Autowired
    private CacheManager cacheManager;

    @Autowired
    private UserService userService;

    @CachePut(value = "userCache",key = "#user.id")
    @PostMapping
    //添加方法
    
    public User save(User user){
        userService.save(user);
        return user;
    }
//    @CacheEvict(value = "userCache",key = "#p0")  //#p0 代表第一个参数
//     @CacheEvict(value = "userCache",key = "#root.args[0]") //#root.args[0] 代表第一个参数
    @CacheEvict(value = "userCache",key = "#id")//#id 代表变量名为id的ca参数
    @DeleteMapping("/{id}")
    //删除方法
    
    public void delete(@PathVariable Long id){
        userService.removeById(id);
    }

    @CacheEvict(value = "userCache",key = "#result.id")//返回值的属性
    @PutMapping
    //修改方法
    
    public User update(User user){
        userService.updateById(user);
        return user;
    }
    /**
     * Cacheable:在方法执行前spring先查看缓存中是否有数据,如果有数据,则直接返回缓存数据;若没有数据,调用方法并将方法返回值放到缓存中
     * value:缓存的名称,每个缓存名称下面可以有多个key
     * key:缓存的key
     * condition:条件,满足条件时才缓存数据
     * unless:满足条件则不缓存
     */
    @Cacheable(value = "userCache",key = "#id",unless = "#result == null ")
    @GetMapping("/{id}")
    //根据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;
    }
}

总结

提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了SpringCache的使用,而SpringCache是在Redis的基础上进行纯注解开发,大大提高开发效率降低高并发

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值