博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringBoot | 整合CacheEHCACHE
阅读量:4181 次
发布时间:2019-05-26

本文共 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 的作用 主要针对方法配置,能够根据一定的条件对缓存进行清空 。

  • allEntries: 是否清空所有缓存内容,缺省为 false,如果指定为 true,则方法调用后将立即清空所有缓存。
  • beforeInvocation: 是否在方法执行前就清空,缺省为 false,如果指定为 true,则在方法还没有执行的时候就清空缓存,缺省情况下,如果方法执行抛出异常,则不会清空缓存。

控制层

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 第一次会去数据库查,第二次则直接在缓存里面查找。

1567657731(1).jpg

参考:

转载地址:http://xgrai.baihongyu.com/

你可能感兴趣的文章
NAND flash和NOR flash的区别
查看>>
writeb(), writew(), writel(),readb(), readw(), readl() 宏函数
查看>>
NOR Flash擦写和原理分析
查看>>
51单片机程序执行流程(STARTUP.A51)
查看>>
原码, 反码, 补码 详解
查看>>
Java自学第一阶段(二)- 小试牛刀
查看>>
Java自学第一阶段(三)- 万能的变量
查看>>
Java自学第一阶段(四)-万能的变量(2)
查看>>
HashMap存储原理以及与hashcode、equals方法的关系
查看>>
python3.6在windows下安装scrapy遇到的问题总结
查看>>
pycharm中打开scrapy项目,import scrapy报错问题
查看>>
scrapy爬取图片,自定义图片下载路径和图片名称
查看>>
python3下import MySQLdb出错问题
查看>>
Maven搭建SSM框架(eclipse)
查看>>
synchronized+Integer模拟火车票预售,出现的问题总结
查看>>
沉浸式过山车,感受巨蚁数字心灵的激情
查看>>
htmlunit爬取js异步加载后的页面
查看>>
修改Linux系统locale设置
查看>>
linux网络无法连接问题
查看>>
linux 查看ip
查看>>