SpringMVC配置缓存,SpringMVC与ehcache整合
因为SpringMVC已经支持了ehcache,所以我们就测试如何整合ehcache
其包在这里
org.springframework.cache.ehcache
属于spring-context-support-4.1.6.RELEASE.jar包,所以这个包肯定不能少哈。
其实整合很简单,我今天是第一次整合,就成功了
请看以下简要步骤,注意本例是假设您已经配置好SpringMVC环境,我们只是增加配置ehcache缓存
1、配置pom.xml,增加以下代码
代码的作用就是下载ehcache的jar,目前最新版本是2.6.11
<dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache-core</artifactId> <version>2.6.11</version> </dependency>
2、编写ehcache的参数配置文件,ehcache.xml
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" updateCheck="false"> <diskStore path="d://temp" /> <defaultCache eternal="false" maxEntriesLocalHeap="1000" overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="3600" timeToLiveSeconds="3600"/> <cache name="baseCache" eternal="false" maxEntriesLocalHeap="200" overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="600" statistics="true" timeToLiveSeconds="600"/> </ehcache>
以上ehcache.xml的简单解释下
diskStore:配置缓存文件的路劲,他可以是这些参数
本例是自定义的d//temp
他还有这些值
java.io.tmpdir,表示临时文件夹,windows表示在C:\Documents and Settings\Administrator\Local Setting\Temp
system.project_name,表示项目的名字
下面的参数意思表示
eternal 元素是否永恒,如果是就永不过期(必须设置)
maxElementsInMemory 缓存容量的内存最大值(必须设置)
overflowToDisk 当缓存达到maxElementsInMemory值是,是否允许溢出到磁盘(必须设置)
diskPersistent 磁盘缓存在VM重新启动时是否保持(默认为false)
timeToIdleSeconds 导致元素过期的访问间隔(秒为单位). 0表示可以永远空闲,默认为0
timeToLiveSeconds 元素在缓存里存在的时间(秒为单位). 0 表示永远存在不过期
memoryStoreEvictionPolicy 当达到maxElementsInMemory时,如何强制进行驱逐默认使用"最近使用(LRU)"策略,其它还有先入先出FIFO,最少使用LFU,较少使用LRU
上面我都给出了我自己的默认值
3、配置ehcache的核心文件cache-config.xml
配置如下
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd "> <cache:annotation-driven cache-manager="cacheManager"/> <bean id="ehcacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> <property name="configLocation" value="classpath:modelxml/ehcache.xml"/> </bean> <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"> <property name="cacheManager" ref="ehcacheManager"/> <property name="transactionAware" value="true"/> </bean> </beans>
上面的的代码中configLocation 指定是ehcache的参数配置文件地址
4、在appcationContext.xml导入ehcache的核心配置文件:cache-config.xml
<import resource="classpath:modelxml/cache-config.xml"/>
5、在您的dao层,在需要缓存的方法名前加入以下注解即可
@Cacheable(value = "baseCache")
注意参数value=baseCache与参数配置文件ehcache.xml中的的cache标签中的name属性一致
以下代码片段为dao层的代码片段
@Cacheable(value = "baseCache") public List<goodsfl> getfl() { String hql = "from goodsfl"; @SuppressWarnings("unchecked") List<goodsfl> list = (List<goodsfl>) hibernateTemplate.find(hql); return list; }6、测试
测试很简单
首先你会发现在你配置的缓存目录下会有文件产生,如我这里产生的文件是
base%0043ache.data 和base%0043ache.index
其次,当某个页面运行第一次,你会在eclipse的窗口发现执行了sql语句
而第二次运行的时候,就没有去执行sql语句
爆款云服务器s6 2核4G 低至0.46/天,具体规则查看活动详情