1、配置依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>
2、启动类
添加 @EnableCaching 注解
3、配置文件 ehcache.xml
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd">
<diskStore path="java.io.tmpdir"/>
<!--
name: 缓存名称
overflowToDisk: 如果内存中数据超过内存限制,是否要缓存到磁盘上
maxElementsInMemory: 内存中缓存数量的上限
maxElementsOnDisk: 磁盘上缓存数量的上限
eternal: 代表对象是否永不过期
timeToIdleSeconds: 对象空闲时间,指对象在多长时间没有被访问就会失效
timeToLiveSeconds: 对象存活时间,指对象从创建到失效所需要的时间
diskPersistent: 是否在磁盘上持久化,指重启jvm后,数据是否有效,默认为false
diskExpiryThreadIntervalSeconds: 对象检测线程运行时间间隔,标识对象状态的线程多长时间运行一次
memoryStoreEvictionPolicy: 存中数据超过内存限制,向磁盘缓存时的策略,默认值LRU
-->
<!-- 默认的必须添加 -->
<defaultCache
overflowToDisk="true"
maxElementsInMemory="10000"
maxElementsOnDisk="10000000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
</defaultCache>
<cache name="user"
overflowToDisk="true"
maxElementsInMemory="10000"
maxElementsOnDisk="10000000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
</cache>
</ehcache>
4、业务层使用
package com.vim.modules.web.service;
import com.vim.modules.web.dao.UserDao;
import com.vim.modules.web.model.User;
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;
@Service
@CacheConfig(cacheNames = "user")
public class UserService {
@Autowired
private UserDao userDao;
/**
* 将方法的返回值放到缓存中,方法始终都会被调用
*/
@CachePut(key = "#user.id")
public User saveUser(User user){
userDao.save(user);
return userDao.get(user.getId());
}
/**
* 将方法的返回值放到缓存中,方法始终都会被调用
*/
@CachePut(key = "#user.id")
public User updateUser(User user){
userDao.update(user);
return userDao.get(user.getId());
}
/**
* 删除一个缓存
*/
@CacheEvict(key = "#id")
public void deleteUser(String id){
userDao.delete(id);
}
/**
* 删除多个缓存
*/
@Caching(evict = {
@CacheEvict(key = "'user_info_'.concat(#id)"),
@CacheEvict(key = "'user_permissions_'.concat(#id)")
})
public void deleteUser(String id){
userDao.delete(id);
}
/**
* 现在缓存中查找,找得到,返回值;否则,方法被调用,将方法的返回值放到缓存中
*/
@Cacheable(key = "#id")
public User getUser(String id){
return userDao.get(id);
}
}
5、工具类
package com.vim.common.utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.List;
/**
* @作者 Administrator
* @时间 2019-07-26 8:57
* @版本 1.0
* @说明 Ehcache 缓存操作类
*/
@Component
public class EhcacheUtils {
private static EhcacheUtils ehcacheUtils;
@PostConstruct
public void init() {
ehcacheUtils = this;
}
@Autowired
private EhCacheCacheManager cacheManager;
/**
* 添加缓存
*/
public static void put(String cacheName, String key, Object value){
ehcacheUtils.cacheManager.getCache(cacheName).put(key, value);
}
/**
* 查询缓存对象
*/
public static <T> T get(String cacheName, String key, Class<T> t){
return ehcacheUtils.cacheManager.getCache(cacheName).get(key, t);
}
/**
* 查询缓存列表
*/
public static <T> List<T> getList(String cacheName, String key){
return ehcacheUtils.cacheManager.getCache(cacheName).get(key, List.class);
}
/**
* 删除缓存
*/
public static void delete(String cacheName, String key){
ehcacheUtils.cacheManager.getCache(cacheName).put(key, null);
}
/**
* 清空缓存
* @param cacheName
*/
public static void clear(String cacheName){
ehcacheUtils.cacheManager.getCache(cacheName).clear();
}
}