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. java hashcode的Delphi实现

    程序使用java做后台服务,数据处理时使用了java的hashcode,Delphi程序也需要生成这个hashcode,为了一致,所以要在Delphi下实现和Java一样的算法. 下面即Delphi版 ...

  2. (org.jbehave.core.failures.BeforeOrAfterFailed: webdriver selenium错误解决。

    (org.jbehave.core.failures.BeforeOrAfterFailed: Method initWebDriver (annotated with @BeforeStory in ...

  3. 如何使两台机器不通过密码连接起来(linux)

    要求服务器10.96.22.40不通过密码直接连接服务器10.96.21.53 1:准备必须的软件 A:服务器40和53同时安装所需软件 yum -y install openssh-server o ...

  4. Android如何实现TCP和UDP传输

    TCP和UDP在网络传输中非常重要,在Android开发中同样重要. 首先我们来看一下什么是TCP和UDP. 什么是TCP? TCP:Transmission Control Protocol 传输控 ...

  5. TextSharp详情

    TextSharp是一个生成Pdf文件的开源项目,最近在项目中有使用到这个项目,对使用中的经验作一个小结. ITextSharp中相关的概念: 一.Document 这个对象有三个构造函数: 隐藏行号 ...

  6. 【Android】Android连接SQLite3数据库的操作

    在前面使用SQLite3的时候,并没有留意到有SQLiteOpenHelper这个类,所以只好在Activity里面去创建和维护数据库跟数据表的创建. 但是,现在有了SQLiteOpenHelper这 ...

  7. VS2017 IDE开发工具选型、安装和使用

    原文地址:https://blog.csdn.net/boonya/article/details/78739500 距离上次使用VS工具已是2年前了,这次准备选择比较新的版本来开发桌面程序了.总的来 ...

  8. Xcode 常用插件

    1.Xcode 插件 从 Xcode 8 起 Apple 禁用 Xcode 插件. 1)Xcode 插件安装目录 ~/library/Application Support/Developer/Sha ...

  9. windows7 sqlserver2012 无法写入受保护的内存 解决办法

    1.我服务器  是windows server 2008R2  装的是MSSQLSERVER2012 2.客户端开发是MSSQLSERVER2012 Windows7 遇到问题: 解决办法: 卸载迅雷 ...

  10. samba 服务器搭建

    为了能在两台机器上共享代码,方便测试不同平台性能和搭建分布式的web server,今天耗费半天时间搭建一个samba服务器共享数据,要求开放写权限,但多次实验均告失败,最终在 鸟哥 的提醒下 检查发 ...