本文共 4160 字,大约阅读时间需要 13 分钟。
POM依赖
org.springframework.boot spring-boot-starter-web
启动类
package com.xiaobu;import lombok.extern.slf4j.Slf4j;import org.springframework.boot.CommandLineRunner;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cache.annotation.CacheConfig;import org.springframework.cache.annotation.EnableCaching;import org.springframework.scheduling.annotation.EnableAsync;import org.springframework.scheduling.annotation.EnableScheduling;import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;import tk.mybatis.spring.annotation.MapperScan;/** * @author xiaobu * @EnableCaching 开启缓存 @CacheConfig(cacheNames = {"myCache"}) cacheNames 必须和ehcache.xml里面的name必须一致 */@CacheConfig(cacheNames = { "myCache"})@EnableCaching@EnableAsync@EnableScheduling@SpringBootApplication@Slf4j@MapperScan(basePackages = "com.xiaobu.mapper")public class SsmApplication implements WebMvcConfigurer, CommandLineRunner { public static void main(String[] args) { SpringApplication.run(SsmApplication.class, args); } @Override public void run(String... args) throws Exception { log.info("服务启动成功。。。。"); }}
配置文件
spring.cache.type=ehcachespring.cache.ehcache.config=classpath:/ehcache.xml
ehcache.xml
使用Spring Cache自动根据方法生成缓存
key: 缓存的 key,可以为空,如果指定要按照 SpEL 表达式编写,如果不指定,则缺省按照方法的所有参数进行组合。例如:@Cacheable(value=”testcache”,key=”#id”)
value: 缓存的名称,必须指定至少一个。例如:@Cacheable(value=”mycache”) 或者@Cacheable(value={”cache1”,”cache2”}
condition: 缓存的条件,可以为空,使用 SpEL 编写,返回 true 或者 false,只有为 true 才进行缓存(如:condition ="#id<2",只缓存id<2的;condition=”#userName.length()>2”只缓存名字长度大于2的)
@Cacheable注解会先查询是否已经有缓存,有会使用缓存,没有则会执行方法并缓存。
@CachePut注解的作用 主要针对方法配置,能够根据方法的请求参数对其结果进行缓存,和 @Cacheable 不同的是,它每次都会触发真实方法的调用 。简单来说就是用户更新缓存数据。但需要注意的是该注解的value 和 key 必须与要更新的缓存相同,也就是与@Cacheable 相同。
@CachEvict 的作用 主要针对方法配置,能够根据一定的条件对缓存进行清空 。
控制层
package com.xiaobu.controller;import com.xiaobu.entity.City;import com.xiaobu.service.CityService;import org.springframework.cache.annotation.Cacheable;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;/** * @author xiaobu * @version JDK1.8.0_171 * @date on 2019/9/4 16:25 * @description */@RestController@RequestMapping("/ehcache")public class EhcacheController { @Resource private CityService cityService; @Cacheable(value = "cacheCity",key ="#id", condition ="#id<2") @GetMapping("getCityByCache/{id}") public City getCityByCache(@PathVariable Integer id) { System.out.println("进入getCityByCache方法...."); return cityService.getCityByCache(id); }}
服务层:
package com.xiaobu.service;import com.github.pagehelper.PageHelper;import com.xiaobu.entity.City;import com.xiaobu.mapper.CityMapper;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import java.util.List;/** * @author xiaobu * @since 2019-09-04 11:09 */@Servicepublic class CityService { @Autowired private CityMapper cityMapper; public City getCityByCache(Integer id){ System.out.println("getCityByCache 执行查询。。。。"); return cityMapper.selectByPrimaryKey(id); } public City getCityByCachePut(Integer id){ System.out.println("getCityByCachePut 执行查询。。。。"); City city= cityMapper.selectByPrimaryKey(id); city.setState("广东"); cityMapper.updateByPrimaryKey(city); return city; } public City getCityByNoCache(Integer id){ System.out.println("getCityByNoCache 执行查询。。。。"); return cityMapper.selectByPrimaryKey(id); }}
访问 http://localhost:8899/ehcache/getCityByCache/1 第一次会去数据库查,第二次则直接在缓存里面查找。
参考:
转载地址:http://xgrai.baihongyu.com/