SpringBoot2+Vue2 actual combat (fourteen) springboot integrated redis to achieve caching

1. Add cache

After adding redis cache, the database will not be refreshed all the time, reducing database pressure

pom.xml dependencies

<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));
    }

You can also customize the key, enclosed in ' '

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

2. Update the cache:

fileController

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

3. Delete the cache

After the database is deleted, the first cache is also deleted, and the database will not be requested later

//删除
    @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();
    }

4. Integrate 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);
    }

After operating the database update cache operation: (used when adding, deleting, and modifying)

The first method: the easiest way

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

delete cache

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();
    }

The second method:

set cache

FileController



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

 ① Take out the data from redis, set it up after the operation, no need to query the database, and the performance is relatively high

//从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));

② Find the data from the database, and then set the latest cache

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

Guess you like

Origin blog.csdn.net/m0_64393446/article/details/131636890