http://blog.csdn.net/qq18998401056/article/details/53467671

**************************************************************************

在Spring Boot中通过@EnableCaching注解自动化配置合适的缓存管理器(CacheManager),Spring Boot根据下面的顺序去侦测缓存提供者:
Generic
JCache (JSR-107)
EhCache 2.x
Hazelcast
Infinispan
Redis
Guava
Simple
除了按顺序侦测外,我们也可以通过配置属性spring.cache.type来强制指定。默认是simple类型。
由于ehcache3.x实现了jsr-107的标准接口,而本文通过整合ehcache3.x来使用JCache的方式。
引入依赖如下:

<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>3.1.3</version>
</dependency>
<!-- JSR107 API -->
<dependency>
<groupId>javax.cache</groupId>
<artifactId>cache-api</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>

ehcache 3.x配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<config
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns:jsr107='http://www.ehcache.org/v3/jsr107'
xmlns='http://www.ehcache.org/v3'
xsi:schemaLocation="
http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.1.xsd
http://www.ehcache.org/v3/jsr107 http://www.ehcache.org/schema/ehcache-107-ext-3.1.xsd">
<!-- <service>
<jsr107:defaults>
<jsr107:cache name="city" template="heap-cache"/>
</jsr107:defaults>
</service> --> <cache-template name="heap-cache">
<resources>
<heap unit="entries">2000</heap>
<offheap unit="MB">100</offheap>
</resources>
</cache-template> <cache alias="city" uses-template="heap-cache">
<expiry>
<ttl unit="seconds">40</ttl>
</expiry>
</cache> </config>

spring boot application.properties配置如下:

#注意:ehcache3.x配置文件路径必须指定
spring.cache.jcache.config=classpath:ehcache.xml

在spring boot 使用@EnableCaching 开启缓存
最后,贴出spring cache注解例子伪代码:

package com.lrh.service;
import java.util.List; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lrh.dao.CityMapper;
import com.lrh.domain.City;
import com.lrh.iservice.CityService; @Service
@Transactional
@CacheConfig(cacheNames = "city")
//@CacheDefaults(cacheName = "city")
public class CityServiceImpl implements CityService{ @Autowired
private CityMapper cityMapper; @Override
@CachePut(key = "#id")
public City editCity(String id, String name) {
cityMapper.edit(id, name);
City city=new City();
city.setId(Long.valueOf(id));
city.setName(name);
return city;
} @Override
public PageInfo<City> selectCityByPage() {
PageHelper.startPage(1,5);
List<City> list = cityMapper.selectAll();
PageInfo<City> page = new PageInfo(list);
return page;
}
/**
* condition满足缓存条件的数据才会放入缓存,condition在调用方法之前和之后都会判断
* unless用于否决缓存更新的,不像condition,该表达只在方法执行之后判断,此时可以拿到返回值result进行判断了
*/
@Override
@Cacheable(key = "#id",unless="#result == null")
//@CacheResult
public City findById(String id) {
return cityMapper.selectCityById(id);
} @Override
@CacheEvict(key="#id")
public void delete(String id) {
//cityMapper.delete(id);
} /**
* allEntries移除所有
*/
@Override
@CacheEvict(allEntries = true)
public void deleteAll() {
cityMapper.deleteAll();
} }

spring boot spring cache ehcache3.x整合的更多相关文章

  1. Spring Boot + MyBatis + Druid + Redis + Thymeleaf 整合小结

    Spring Boot + MyBatis + Druid + Redis + Thymeleaf 整合小结 这两天闲着没事想利用**Spring Boot**加上阿里的开源数据连接池**Druid* ...

  2. spring boot guava cache 缓存学习

    http://blog.csdn.net/hy245120020/article/details/78065676 ****************************************** ...

  3. 解决Spring Boot(2.1.3.RELEASE)整合spring-data-elasticsearch3.1.5.RELEASE报NoNodeAvailableException[None of the configured nodes are available

    Spring Boot(2.1.3.RELEASE)整合spring-data-elasticsearch3.1.5.RELEASE报NoNodeAvailableException[None of ...

  4. [权限管理系统(四)]-spring boot +spring security短信认证+redis整合

    [权限管理系统]spring boot +spring security短信认证+redis整合   现在主流的登录方式主要有 3 种:账号密码登录.短信验证码登录和第三方授权登录,前面一节Sprin ...

  5. Spring Boot 2.0 快速集成整合消息中间件 Kafka

    欢迎关注个人微信公众号: 小哈学Java, 每日推送 Java 领域干货文章,关注即免费无套路附送 100G 海量学习.面试资源哟!! 个人网站: https://www.exception.site ...

  6. Spring Boot + Spring Data + Elasticsearch实例

    Spring Boot + Spring Data + Elasticsearch实例 学习了:https://blog.csdn.net/huangshulang1234/article/detai ...

  7. 基于Spring Boot+Spring Security+JWT+Vue前后端分离的开源项目

    一.前言 最近整合Spring Boot+Spring Security+JWT+Vue 完成了一套前后端分离的基础项目,这里把它开源出来分享给有需要的小伙伴们 功能很简单,单点登录,前后端动态权限配 ...

  8. Spring Boot -- Spring Boot之@Async异步调用、Mybatis、事务管理等

    这一节将在上一节的基础上,继续深入学习Spring Boot相关知识,其中主要包括@Async异步调用,@Value自定义参数.Mybatis.事务管理等. 本节所使用的代码是在上一节项目代码中,继续 ...

  9. 快速搭建基于Spring Boot + Spring Security 环境

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 1.Spring Security 权限管理框架介绍 简介: Spring Security 提供了基于 ...

  10. spring Boot+spring Cloud实现微服务详细教程第二篇

    上一篇文章已经说明了一下,关于spring boot创建maven项目的简单步骤,相信很多熟悉Maven+Eclipse作为开发常用工具的朋友们都一目了然,这篇文章主要讲解一下,构建spring bo ...

随机推荐

  1. linux shell 脚本攻略学习15--如何只列出目录,如何快速切换目录

    工作中经常遇到关于目录方面的问题,例如,如何只列出当前目录下的所有目录,以及如何快速高效的切换目录,而不需要使用鼠标,下面将简单介绍关于这两方面的解决方案: 一.如何只列出目录? 看似简单的任务,其实 ...

  2. Mysql模糊查询 select count(*) from sys_invitation where from_id like '%1006%';

    select count(*) from sys_invitation where from_id like '%1006%'; 查询结果 select * from sys_invitation w ...

  3. 深度优化LNMP之PHP

    PHP缓存加速介绍   1.操作码介绍及缓存原理     当客户端请求一个php程序时,服务器的PHP引擎会解析该PHP程序,并将其编译为特定的操作码文件(Operate Code,opcode)该文 ...

  4. Redis学习之路(005)- redis内存数据库C客户端hiredis API 中文说明

    A)编译安装 make make install (/usr/local) make install PREFIX=$HOME/progs(可以自由指定安装路径) B)同步的API接口 redisCo ...

  5. HDU 3584 Cube (三维数状数组)

    Cube Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submi ...

  6. 利用hadoop来解决“单表关联”的问题

    已知 child parent a b a c d b d c b e b f c g c h x g x h m x m n o x o n 则 c 2+c+g 2+c+h 1+a+c 1+d+c ...

  7. List元素排序简例

    前言:这种处理方式,在程序中偶尔会用的到,栗子很简单,关键是加强一下记忆,以及以备后用 1:实现Comparable接口的方式 1-1:没有使用泛型,重写compareTo()方法时需要判断类型及转换 ...

  8. C 语言整型谜题

    如题,此篇文章是描述C语言中的整数谜题. 假定机器字长是32位的,用2的补码表示整数.对以下C表达式,请问它们在所有情况下都正确吗?如果不是,请给出反例. 初始化: 1 2 3 4 int x = f ...

  9. 使用Object#tap使代码更优雅

    今天看spree源码的时候经常看到Object#tap方法.以前只知道有这个方法,而且感觉这个方法调试的作用大于实际,今日看来以前的理解应该不够准确. 先看下官方文档上tap的例子 Yields se ...

  10. python制作exe可执行文件的方法---使用pyinstaller

    python制作exe可执行文件的方法---使用pyinstaller   python生成windows下exe格式的可执行程序有三种可选方案: py2exe是大家所熟知的,今天要介绍pyinsta ...