spring boot spring cache ehcache3.x整合
2024-09-01 13:32:18
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();
} }
最新文章
- 【PHP面向对象(OOP)编程入门教程】11.类的继承
- 【转载】javaAgent 参数
- 将Apache加入到linux系统service
- 每一个成功的程序员的身后都有一个--------Parse
- C#获取枚举描述代码
- 【皇甫】☀那些事儿......STEP
- Java沙箱技术
- 教程:如何减小iOS应用程序的大小?
- newClass a = Func(3)中隐藏的操作
- bzoj1296: [SCOI2009]粉刷匠
- UVA 539 The Settlers of Catan dfs找最长链
- 你真的了解 console 吗
- Swift2.3适配Swift3.0时出现的各种问题
- Xcode8.2 继续使用插件
- android动态LinearLayout
- 网络运维必回的模拟器-GNS软件下载和安装
- Value &#39;0000-00-00 00:00:00&#39; can not be represented as java.sql.Timestamp
- 操作系统04_IO管理
- the detailed annotation of StringBuilder
- iis7.5加fck解析漏洞后台拿shell