ehcache-2.7.5.jar(主程序) ehcache-spring-annotations-1.2.0.jar(注解) guava-r09.jar(依赖) slf4j-api-1.6.6.jar(依赖)
####spring配置中须要添加以下内容 头部html
xmlns:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.2.xsd <!-- 缓存配置 --> <!-- 启用缓存注解功能(请将其配置在Spring主配置文件中) --> <cache:annotation-driven cache-manager="cacheManager" /> <!-- Spring本身的基于java.util.concurrent.ConcurrentHashMap实现的缓存管理器(该功能是从Spring3.1开始提供的) --> <!-- <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager"> <property name="caches"> <set> <bean name="myCache" class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"/> </set> </property> </bean> --> <!-- 若只想使用Spring自身提供的缓存器,则注释掉下面的两个关于Ehcache配置的bean,并启用上面的SimpleCacheManager便可 --> <!-- Spring提供的基于的Ehcache实现的缓存管理器 --> <bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> <property name="configLocation" value="classpath:ehcache.xml" /> </bean> <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"> <property name="cacheManager" ref="cacheManagerFactory" /> </bean>
####ehcache.xmljava
<ehcache> <diskStore path="java.io.tmpdir"/> <defaultCache maxElementsInMemory="1000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="false"/> <cache name="myCache" maxElementsOnDisk="20000" maxElementsInMemory="2000" eternal="false" overflowToDisk="true" diskPersistent="true"/> <cache name="cacheTest" maxElementsOnDisk="20000" maxElementsInMemory="2000" eternal="false" overflowToDisk="true" diskPersistent="true"/> </ehcache>
cache通常用在和数据库交互的地方servicespring
示例 package com.service; import java.util.Date; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import javax.annotation.Resource; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; 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.aft.site.yey.dao.NoticeDao; import com.aft.site.yey.entity.Notice; import com.app.jdbc.core.ToolUtil; /** * @author: yangyang 2013年10月21日 * @since JDK 1.6 */ @Service("XXXNoticeService") public class NoticeService { private static Log logger = LogFactory.getLog(NoticeService.class); @Resource(name = "XXXNoticeDao") private NoticeDao dao; /** * status = 0 指未删除 * */ @Cacheable(value = "cacheTest",key="'noticelist'") public List<Notice> topN(int begin, int end) { LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>(); orderby.put("publish_time", "desc"); Map<String, String> where = new HashMap<String, String>(); where.put(dao.STATUS + " = ? ", dao.NORMAL_CODE.toString()); //TODO:delete System.out.println("list:"); logger.info("[list ]"); return dao.find(where, orderby, begin, end); } @CacheEvict(value = "cacheTest",key="'noticelist'") public void delete(String id) { //TODO:delete System.out.println("delete:"); logger.info("delete "); dao.delete(id, false); } @CacheEvict(value = "cacheTest", allEntries = true) public void save(Notice notice) { notice.setRowid(ToolUtil.getUUID()); notice.setStatus(dao.NORMAL_CODE); notice.setPublish_time(new Date()); // TODO:yeyid的得到方式 notice.setYey_id("123"); dao.insert(notice); //TODO:delete System.out.println("save:"); logger.info("save "); } public Notice get(String id) { return dao.findById(id); } //@CachePut(value = "cacheTest",key="#notice.getRowid()") public void update(Notice notice) { Map<String, String> set = new HashMap<String, String>(); LinkedHashMap<String, String> where = new LinkedHashMap<String, String>(); set.put("title", notice.getTitle()); set.put("author", notice.getAuthor()); set.put("content", notice.getContent()); where.put(dao.getIdColumnName() + "=?", notice.getRowid()); dao.update(set, where); System.out.println("update:"); logger.info("update "); } }
cache主要注解使用:@Cacheable,@CacheEvict,@CachePut数据库
缓存是这样的,取值时在方法(A)调用前查一下缓存中是否有目标值,缓存存在的话直接从缓存中拿出再也不去执行方法(A),这也是最基本的*@Cacheable*的概念;apache
缓存中有值须要更新怎么办?使用@CacheEvict来更新,这个注解的意思是删除掉缓存里面的某个值,从而达到更新缓存的效果。关于缓存更新,例如,取topN个对象,第一次取的时候好比是前1~10个,缓存中存这1~10的一个集合对象,第二次取的时候直接从缓存中拿,这没问题,如今是这样的,假设数据库中删除了1~10个元素中的任意一个值,这样数据库中的topN与缓存中的topN就不一样步了,下次你在前台取topN的时候,由于缓存里面有这个对象,根据以前的介绍(取值时在方法(A)调用前查一下缓存中是否有目标值,缓存存在的话直接从缓存中拿出再也不去执行方法(A)),方法A被略过,查的值不是真正的topN了,所以须要在add或者delete以后删除掉原来的缓存,保持数据一致。其余情景请自行考虑。api
根据缓存的特性,如何作到既要保证方法被调用,又但愿结果被缓存呢?直接使用*@CachePut*,他与@Cacheable的区别就在与方法是会被执行的。缓存
注解里面属性解释,@Cacheable 与@CachePut同样, @CacheEvict还有和删除有关的两个属性:mvc
value:缓存的名称,在spring配置文件中定义,必须指定至少一个app
key:缓存的key,(缓存是键值对儿)能够为空,若是指定要按照 SpEL 表达式编写,若是不指定,则缺省按照方法的全部参数进行组合spa
condition:缓存的条件,能够为空,使用SpEL 编写,返回 true 或者 false,只有为 true 才进行缓存
allEntries:是否清空全部缓存内容,缺省为 false,若是指定为 true,则方法调用后将当即清空全部缓存
beforeInvocation:是否在方法执行前就清空,缺省为 false,若是指定为 true,则在**方法尚未执行的时候就清空缓存*,缺省状况下为false,这样若是方法执行抛出异常,则不会清空缓存
集群的同步问题,未完待续。
###很是详细的spring mvc和cache的使用博客 http://www.ibm.com/developerworks/cn/opensource/os-cn-spring-cache/
###官网文档 http://docs.spring.io/spring/docs/3.1.0.M1/spring-framework-reference/html/cache.html
###ehcache介绍 http://my.oschina.net/coolfire368/blog/123377
###spring mvc整合 ehcache http://blog.csdn.net/jadyer/article/details/12257865