Spring Cache Demo
2016-04-02 23:35
531 查看
1,配置cacheManager,applicationContext.xml
2,测试实现Controller
3,测试实现Service
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-4.0.xsd"> <context:annotation-config /> <context:component-scan base-package="com.zouhao.spring.cache" /> <cache:annotation-driven/> <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager"> <property name="caches"> <set> <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"> <property name="name" value="default"/> </bean> <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"> <property name="name" value="baseCache"/> </bean> </set> </property> </bean> </beans>
2,测试实现Controller
package com.zouhao.spring.cache.controller; import javax.xml.ws.Response; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import com.alibaba.fastjson.JSON; import com.zouhao.spring.cache.entity.CacheObject; import com.zouhao.spring.cache.service.SpringCacheService; @Controller @RequestMapping("/cache") public class SpringCacheController { @Autowired private SpringCacheService springCacheService; @ResponseBody @RequestMapping(value = "/getCache", method= RequestMethod.GET) private String getCacheById(@RequestParam("id") Integer id){ return JSON.toJSONString(springCacheService.getCacheObject(id)); } @ResponseBody @RequestMapping(value = "/removeCache", method= RequestMethod.GET) private String clearCacheById(@RequestParam("id") Integer id){ springCacheService.clearCacheObjectById(id); return "clear success"; } @ResponseBody @RequestMapping(value = "/updateCache", method= RequestMethod.GET) private String updateCacheById(@RequestParam("id") Integer id){ springCacheService.updateCacheObject(id); return "update success"; } }
3,测试实现Service
package com.zouhao.spring.cache.service; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.CachePut; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; import com.zouhao.spring.cache.entity.CacheObject; @Service public class SpringCacheService { //将查询结果通过id作为key缓存 @Cacheable(key="#id",value="baseCache") public CacheObject getCacheObject(Integer id) { System.out.println(String.format("query CacheObject from DB by id:%d",id)); CacheObject obj = newCacheObject(id); return obj; } // 更新 accountCache 缓存 @CachePut(value="baseCache",key="#id") public CacheObject updateCacheObject(Integer id) { System.out.println(String.format("update the CacheObject by id:%d", id)); return updateCacheObjectFromDB(id); } //清空缓存 @CacheEvict(value="baseCache",key="#id") public void clearCacheObjectById(Integer id) { } private CacheObject updateCacheObjectFromDB(Integer id) { System.out.println(String.format("get CacheObject from cache by id:%d ", id)); CacheObject obj = getCacheObject(id); System.out.println(String.format("set CacheObject name field from cache")); obj.setName("xxxxxxxxxxxxxxxxxxxx"); return obj; } public CacheObject newCacheObject(Integer id){ CacheObject obj = new CacheObject(); obj.setId(id); obj.setName("test"); obj.setPass("pass"); return obj; } }
相关文章推荐
- 由HBase的数据删除方式联想到JVM垃圾回收机制及磁盘碎片整理机制
- 机器学习:weka源码在eclipse的配置和异常VerifyError的解决
- Java Web之Filter
- 学习javaEE每一天2016.4.2
- java 之自动装拆箱及final用法
- java从字符串中提取数字
- Eclipse常用快捷键
- 8 面向对象(制作帮助文档,通过JDK提供的API学习了Math类,代码块,继承(继承子父成员访问特点,super关键字,方法重写))
- 20145212 《Java程序设计》第5周学习总结
- 腾讯笔试之M*N矩阵 左对角到右对角最大值 java实现
- java线程FAQ
- 写入文件和读取文件信息—Java Card开发第三篇
- 20145330第五周《Java学习笔记》
- Java中,一个类实现某个接口,必须重写接口中的所有方法吗
- Hibernate入门
- 对jdk版本对eclipse影响的思考
- JAVA守护线程 daemon thread
- java线程等待、设置优先级、同步、挂起、恢复(Join、setPriority、synchronized、wait、notify……)
- Spring中的事务管理
- mac配置java环境和maven环境