springboot 中使用ehcache缓存



@CacheConfig
@Cacheable
@CachePut
@CacheEvict
Last updated



Last updated
<!--spring cache-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<!--ehcache-->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.10.6</version>
</dependency>spring:
cache:
ehcache:
config: classpath:ehcache.xml<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="3600"
timeToLiveSeconds="0"
overflowToDisk="false"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120" />
<cache
name="myuser"
maxEntriesLocalHeap="2000"
eternal="false"
timeToIdleSeconds="3600"
timeToLiveSeconds="0"
overflowToDisk="false"
statistics="true">
</cache>
</ehcache>
<!--配置含义:
name:缓存名称。
maxElementsInMemory:缓存最大个数。
eternal:对象是否永久有效,一但设置了,timeout将不起作用。
timeToIdleSeconds:设置对象在失效前的允许闲置时间(单位:秒)。仅当eternal=false对象不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大。
timeToLiveSeconds:设置对象在失效前允许存活时间(单位:秒)。最大时间介于创建时间和失效时间之间。仅当eternal=false对象不是永久有效时使用,默认是0.,也就是对象存活时间无穷大。
overflowToDisk:当内存中对象数量达到maxElementsInMemory时,Ehcache将会对象写到磁盘中。
diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区。
maxElementsOnDisk:硬盘最大缓存个数。
diskPersistent:是否缓存虚拟机重启期数据。
diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒。
memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)。
clearOnFlush:内存数量最大时是否清除。
diskStore 则表示临时缓存的硬盘目录。
-->import com.boot.boot_cache.entity.User;
import com.boot.boot_cache.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
/**
* @author : kaifa
* create at: 2019-10-22 10:10
* @description: user 业务层
*/
@Service
@CacheConfig(cacheNames = "myuser")//对应ehcache.xml配置文件配置的缓存名称
public class UserService {
@Autowired
private UserMapper userMapper;
/**
* 查询所有用户
* @return
*/
@Cacheable
public List<User> selectListAll(){
return userMapper.selectListAll();
}
/**
* 修改用户
* @param user
* @return
*/
@CachePut(key = "#user.id")
@Transactional
public User updateUser(User user){
userMapper.updateUser(user) ;
return userMapper.findUserById(user.getId());//这里修改后需要查询一遍进行缓存
}
/**
* 查询所有用户
* @return
*/
@Cacheable(key = "#id")
public User findUserById(Integer id){
return userMapper.findUserById(id);
}
/* @CacheEvict(key = "#user.id")
@Transactional
public void updateUser(User user){
userMapper.updateUser(user) ;
}*/
}import com.boot.boot_cache.entity.User;
import java.util.List;
/**
* @author : kaifa
* create at: 2019-10-22 10:10
* @description: user mapper接口
*/
public interface UserMapper {
/**查询所有用户*/
List<User> selectListAll();
/**
* 修改用户信息
* @param user
* @return
*/
int updateUser(User user);
/**
* 根据id查询
* @param id
* @return
*/
User findUserById(Integer id);
}<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.boot.boot_cache.mapper.UserMapper">
<update id="updateUser" parameterType="com.boot.boot_cache.entity.User">
update T_USER
<set>
<if test="userName != null and userName != ''">
USERNAME = #{userName},
</if>
<if test="password != null and password != ''">
password = #{password},
</if>
</set>
<where>
id = #{id}
</where>
</update>
<select id="selectListAll" resultType="com.boot.boot_cache.entity.User">
select id,username,password,create_time createTime from t_user
</select>
<select id="findUserById" resultType="com.boot.boot_cache.entity.User">
select id,username userName,password,create_time createTime from t_user where id = #{id}
</select>
</mapper>import com.boot.boot_cache.entity.User;
import com.boot.boot_cache.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @author : kaifa
* create at: 2019-10-22 10:16
* @description: user controller
*/
@RestController
@RequestMapping("user")
public class UserController {
@Autowired
private UserService userService;
/**
* 查询所有用户
* @return
*/
@RequestMapping("userList.do")
List<User> selectListAll(){
return userService.selectListAll();
}
/**
* 修改用户
* @return
*/
@RequestMapping("updateUser.do")
String updateUser(User user){
userService.updateUser(user);
return "success";
}
/**
* 根据id查询
* @return
*/
@RequestMapping("findUserById.do")
User findUserById(Integer id){
return userService.findUserById(id);
}
}