您的位置:首页 > 编程语言 > Java开发

Spring整合Ehcache缓存

2017-11-03 00:00 267 查看
摘要: Ehcache缓存是一个非常轻量级的Java进程内的缓存,在web应用中使用非常广泛,很受Java开发者的青睐。本文就简单展示Ehcache在项目中如何使用

新建一个maven项目。
项目结构图如下:<br>


<br>
核心jar包:

<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.8.3</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>3.2.8.RELEASE</version>
</dependency>
<dependency>

手动引入spring-context-support包避免因为版本的问题出错(被坑过)<br>

下面对关键的代码进行解析:通过Java类来配置Ehcache,@EnableCaching注解来启用缓存,并且通知Spring容器创建EhCacheManager,并且指定本地的ehcachel.xml文件

@Configuration
@EnableCaching
public class CachingConfig {
[@Bean](https://my.oschina.net/bean)
public EhCacheCacheManager ehCacheCacheManager(CacheManager cm) {
return new EhCacheCacheManager(cm);
}

[@Bean](https://my.oschina.net/bean)
public EhCacheManagerFactoryBean ehCacheManagerFactoryBean() {
EhCacheManagerFactoryBean ehCacheManagerFactoryBean = new EhCacheManagerFactoryBean();

ehCacheManagerFactoryBean.setConfigLocation(new ClassPathResource("ehcache.xml"));
return ehCacheManagerFactoryBean;
}
}

ehcache.xml文件:

<ehcache>
<defaultCache
maxElementsInMemory="10000"
maxElementsOnDisk="100000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
diskSpoolBufferSizeMB="30"
maxElementsOnDisk="10000000"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
<cacheEventListenerFactory
class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"/>
<cache name="userListCache" maxBytesLocalHeap="50m" timeToLiveSeconds="100">
</cache>
</ehcache>

ehcache.xml文件中几个关键属性:
name:缓存的名称;<br>
maxElementsInMemory:内存中缓存element最大数量<br>
maxElementsOnDisk:磁盘上缓存的最大数量<br>
eternal:设定缓存是否永不过期<br>
overflowToDisk:缓存超过内存限制是否缓存到磁盘上<br>
timeToIdleSeconds:对象的空闲时间,多久没有被访问到就会失效<br>
timeToLiveSeconds:对象的存活时间<br>
memoryStoreEvictionPolicy:缓存超过指定内存的大小向磁盘缓存的策略,主要有三种FIFO,LFU,LRU
(FIFO:先进先出,不作过多解释,LFU:最少使用(缓存元素有个hit属性,hit最小的就是最少使用的),LRU:最近最少被使用)<br>

下面来进行业务逻辑的开发,对用户信息进行查询和修改:<br>
UserMapper.xml

<select id="getUserList" resultType="com.spark.cache.model.User">
select id as id,username as userName, password as password,age as age from user;
</select>

<update id="updateUser" parameterType="User">
update `user` set id=#{id}
<if test="userName!=null">
,username = #{userName}
</if>
<if test="password!=null">
,password = #{password}
</if>
<if test="age!=null">
,age = #{age}
</if>
where id = #{id}
</update>

service层方法的实现:

@Cacheable(value = "userListCache")
@Override
public List<User> getUserList() {
return userDao.getUserList();
}

@CacheEvict(value = "userListCache")
@Override
public int updateUser(User user) {
return userDao.updateUser(user);
}

介绍一下缓存中常用的注解:<br>
@Cacheable : 表明在Spring调用方法之前,先在缓存中查找方法的返回值,如果能够找到,返回缓存中的值,否则,这个方法就会被调用,返回值放到缓存中。<br>
@CachePut: 表明应该将该方法的返回值放到缓存中,方法调用之前不会检查缓存,该方法始终会被调用。<br>
@CacheEvict: 表明Spring应该在缓存中清空一个或者多个条目(缓存的value值)<br>
@Caching:是一个分组的注解,能够同时应用多个其他的缓存注解。

controller层处理:

@RequestMapping("/list")
@ResponseBody
public List<User> list() {
return userService.getUserList();
}

@RequestMapping(value = "/update", method = RequestMethod.POST)
@ResponseBody
public User update(User user) {
int result = userService.updateUser(user);
if (result > 1) {
return user;
}
return null;
}

下面我们来验证一下:<br>



看一下打印的日志:



再次请求:



日志没有重复打印,说明我们配置的缓存起作用了。

我们来测试一下修改:



再进行请求用户列表接口,然后看一下日志:



执行了一次更新操作,还有一次查询操作,说明我们的缓存配置没有问题。

如有不足之处,还请指出。

项目源码:https://github.com/YunDongTeng/SpringEhCache/tree/master/springcache
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息