新建一个maven项目。 项目结构图以下:<br> <br> 核心jar包:git
<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>github
@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>app
下面来进行业务逻辑的开发,对用户信息进行查询和修改:<br> UserMapper.xmlmaven
<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层方法的实现:ide
@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层处理:spa
@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> 看一下打印的日志:
再次请求:
日志没有重复打印,说明咱们配置的缓存起做用了。.net
咱们来测试一下修改:
再进行请求用户列表接口,而后看一下日志: 执行了一次更新操做,还有一次查询操做,说明咱们的缓存配置没有问题。
若有不足之处,还请指出。
项目源码:https://github.com/YunDongTeng/SpringEhCache/tree/master/springcache