从0开始搭建一个生产级SpringBoot2.0.X项目(七)SpringBoot 使用Spring Cache

 前言

最近有个想法想整理一个内容比较完整springboot项目初始化Demo。

SpringBoot使用Spring Cache 缓存数据,每次调用需要缓存功能的方法时,spring会检查指定参数的指定的目标方法是否已经被调用过,如果有就直接从缓存中获取。

一、启动类增加注解@EnableCaching

package com.murg.bootdemo;

import org.mybatis.spring.annotation.MapperScan;
        import org.springframework.boot.SpringApplication;
        import org.springframework.boot.autoconfigure.SpringBootApplication;
        import org.springframework.cache.annotation.EnableCaching;

@MapperScan("com.murg.bootdemo.**.mapper")
@SpringBootApplication
@EnableCaching
public class BootdemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(BootdemoApplication.class, args);
    }

}

二、修改tt26service 新增缓存方法和清除缓存方法

package com.murg.bootdemo.business.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.murg.bootdemo.business.entity.Tt26;
import com.murg.bootdemo.business.mapper.Tt26Mapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 */
@Service
@Slf4j
public class Tt26Service extends ServiceImpl<Tt26Mapper, Tt26> {
    //@Cacheable注解被用于getAlltt26方法上。
    // 其中value属性指定了缓存的名称为“tt26List”,
    // key属性指定了缓存的键为传入的code参数。
    // 当调用getTt26方法时,Spring会先检查名为“tt26List”的缓存中是否
    // 已经存在键为code的结果,如果存在,则直接返回缓存中的结果,否则执行方法体中的代码,并将/结果存储到缓存中。
    @Cacheable(value = "tt26List",key="#code")
    public List<Tt26> getTt26(String code) {
        log.info("第一次获取缓存没有才走这里");
        List<Tt26> tt26s = this.baseMapper.selectList(Wrappers.<Tt26>lambdaQuery().eq(Tt26::getCode,code));
        //存在返回的Student对象
        if(CollectionUtils.isNotEmpty(tt26s)){
            return tt26s;
        }
        //不存在返回null
        return null;
    }

    @CacheEvict(value = {"tt26List"},allEntries = true)
    public String removeCachett26() {
        log.info("清除缓存TT26");
        return "ok";
    }
}

2.1     @Cacheable注解被用于getTt26方法上。其中value属性指定了缓存的名称为“tt26List”,key属性指定了缓存的键为传入的code参数。当调用getTt26方法时,Spring会先检查名为“tt26List”的缓存中是否 已经存在键为code的结果,如果存在,则直接返回缓存中的结果,否则执行方法体中的代码,并将/结果存储到缓存中。

@CacheEvict(value = {"tt26List"},allEntries = true)

清除所有名称为“tt26List”的缓存

三、新增测试缓存接口TestCacheController 

package com.murg.bootdemo.business.controller;

import com.murg.bootdemo.business.entity.Tt26;
import com.murg.bootdemo.business.service.Tt26Service;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
@RequiredArgsConstructor
@RestController
public class TestCacheController {
    private final Tt26Service tt26Service;
    @RequestMapping(value = "/getTt26FromCache", method = RequestMethod.GET)
    public List<Tt26> getTt26FromCache(@RequestParam String code){

        return tt26Service.getTt26(code);
    }
    @RequestMapping(value = "/removeCacheTt26", method = RequestMethod.GET)
    public String removeCachett26(){
        //清楚缓存
        return tt26Service.removeCachett26();
    }

}

四、测试

第一次调用获取数据接口

更换参数为130101获取不同的值 

日志输出"第一次获取缓存没有才走这里"

第二次再次调用接口 入参为139901 未打印日志 说明没有进入getTt26方法 数据从缓存中获取

调用清除缓存方法之后再次调用获取数据接口

控制台输出,测试清除缓存有效。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值