SpringBoot2+Vue2实战(十四)springboot集成redis实现缓存

一、添加缓存

添加redis缓存之后就不会一直刷新数据库,减少数据库压力

pom.xml依赖

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>
SpringbootApplication
@EnableCaching
EchartsController
 @AuthAccess
    @GetMapping("/file/front/all")
    @Cacheable(value = "files", key = "targetClass + methodName")
    public Result frontAll() {
        return Result.success(fileMapper.selectList(null));
    }

也可以自定义key,要用 ' ' 括起来

@AuthAccess
    @GetMapping("/file/front/all")
    @Cacheable(value = "files", key = "'frontAll'")
    public Result frontAll() {
        return Result.success(fileMapper.selectList(null));
    }

二、更新缓存:

fileController

//更新
    @PostMapping("/update")
    //更新缓存
    @CachePut(value = "files",key = "'frontAll'")
    public Result update(@RequestBody Files files) {
        //新增或修改
        fileMapper.updateById(files);
        return success(fileMapper.selectList(null));
    }

三、删除缓存

数据库执行删除之后,第一次缓存也删除,后面就不会请求数据库

//删除
    @DeleteMapping("/{id}")
    //清除一条缓存,key为要清空的数据
    @CacheEvict(value = "emp",key = "'frontAll'")
    public Result delete(@PathVariable("id") Integer id) {
        Files files = fileMapper.selectById(id);
        files.setIsDelete(true);
        fileMapper.updateById(files);
        return success();
    }

四、集成redis

pom.xml

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

application.yml

  redis:
    port: 6379
    host: 127.0.0.1

EchartController

@Resource
    private FileMapper fileMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;



@AuthAccess
    @GetMapping("/file/front/all")
    /*@Cacheable(value = "files", key = "targetClass + methodName")*/
    public Result frontAll() {

        //1.从缓存获取数据
        String jsonStr = stringRedisTemplate.opsForValue().get(FILES_KEY);
        List<Files> files;
        //2.取出来的json是空的
        if (StrUtil.isBlank(jsonStr)) {
            //3.从数据库取出数据
            files = fileMapper.selectList(null);
            //4.再去缓存到redis
            stringRedisTemplate.opsForValue().set(FILES_KEY,JSONUtil.toJsonStr(files));
        } else {
            //减轻数据库的压力
            //5.如果有,从redis缓存中获取数据
            files = JSONUtil.toBean(jsonStr, new TypeReference<List<Files>>() {
            }, true);
        }
        return Result.success(files);
    }

操作完数据库更新缓存操作:(增删改时使用)

第一种方法:最简单的方式

//最简单的方式
        flushRedis(Constants.FILES_KEY);

删除缓存

FileController

@Autowired
    private StringRedisTemplate stringRedisTemplate;


//删除缓存
    private void flushRedis(String key){
        stringRedisTemplate.delete(key);
    }




//更新
    @PostMapping("/update")
    //更新缓存
    /*@CachePut(value = "files",key = "'frontAll'")*/
    public Result update(@RequestBody Files files) {
        //新增或修改
        fileMapper.updateById(files);
        flushRedis(Constants.FILES_KEY);
        return success();
    }


 //删除
    @DeleteMapping("/{id}")
    //清除一条缓存,key为要清空的数据
   /* @CacheEvict(value = "emp",key = "'frontAll'")*/
    public Result delete(@PathVariable("id") Integer id) {
        Files files = fileMapper.selectById(id);
        files.setIsDelete(true);
        fileMapper.updateById(files);
        flushRedis(Constants.FILES_KEY);
        return success();
    }

第二种方法:

设置缓存

FileController



//设置缓存
    private void setCache(String key,String value){
        stringRedisTemplate.opsForValue().set(key,value);
    }

 ①从redis取出数据,操作完,再设置,不用查询数据库,性能比较高

//从redis取出数据,操作完,再设置,不用查询数据库
        String json = stringRedisTemplate.opsForValue().get(Constants.FILES_KEY);
        List<Files> files1 = JSONUtil.toBean(json, new TypeReference<List<Files>>() {
        },true);
        files1.add(saveFile);
        setCache(Constants.FILES_KEY,JSONUtil.toJsonStr(files1));

②从数据库查出数据,再设置最新缓存

 //从数据库查出数据
        List<Files> files = fileMapper.selectList(null);
        //设置最新的缓存
        setCache(Constants.FILES_KEY, JSONUtil.toJsonStr(files));

猜你喜欢

转载自blog.csdn.net/m0_64393446/article/details/131636890