上一篇中发表的spring-mvc-hibernate二级缓存,由于需要缓存,找了很多信息,有一个blog的问答给了我很大的启发:
hibernate二级缓存是处于持久层,也就是对数据库操作上的缓存,还数据比较底层,这样效率其实并没有提高多少。(大概这个意思)
那我们就直接从业务或者是控制层,甚至是拦截器位置就开始做缓存。(这个给我的启发很大)
于是就开始找springmvc的缓存,由于hibernate默认使用ehcache框架,看来资料这个框架也确实不错,然后就开始做把:
需要的jar包:hibernate-ehcache-4.3.11.Final.jar(这个用作二级缓存要不要自己试试) ehcache-core-2.4.3.jar
先看看web.xml配置(新手看)
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>spring</display-name>
<welcome-file-list>
<welcome-file>demo1.html</welcome-file>
</welcome-file-list>
<session-config>
<session-timeout>60</session-timeout>
</session-config>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:config/spring-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/spring-commons.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
增加一个spring-ehcache.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<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.0.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-4.2.xsd">
<description>ehcache缓存配置管理文件</description>
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:config/ehcache.xml"/>
</bean>
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="ehcache"/>
</bean>
</beans>
再来个ehcache.xml
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
updateCheck="false">
<!-- Sets the path to the directory where cache .data files are created.
If the path is a Java System Property it is replaced by its value in the running VM.
The following properties are translated:
user.home - User's home directory
user.dir - User's current working directory
java.io.tmpdir - Default temp file path
-->
<!--指定二级缓存存放在磁盘上的位置-->
<diskStore path="/pd/file/ehcache/" />
<!--<diskStore path="user.dir" />-->
<!--
maxElementsInMemory: 在内存中存放的最大对象数;
eternal: 是否永久保存缓存,设置成false;
timeToLiveSeconds: 以创建时间为基准开始计算的超时时长;
timeToIdleSeconds: 在创建时间和最近访问时间中取出离现在最近的时间作为基准计算的超时时长;
overflowToDisk: 如果对象数量超过内存中最大的数,是否将其保存到磁盘中,设置成true;
如果仅设置timeToLiveSeconds,则该对象的超时时间=创建时间+timeToLiveSeconds,假设为A;
如果没设置timeToLiveSeconds,则该对象的超时时间=max(创建时间,最近访问时间)+timeToIdleSeconds,假设为B;
如果两者都设置,则取出A、B中的较小值,即min(A,B),表示只要有一个超时成立即算超时;
-->
<!--
设定具体的命名缓存的数据过期策略
cache元素的属性:
name:缓存名称
maxElementsInMemory:内存中最大缓存对象数
maxElementsOnDisk:硬盘中最大缓存对象数,若是0表示无穷大
eternal:true表示对象永不过期,此时会忽略timeToIdleSeconds和timeToLiveSeconds属性,默认为false
overflowToDisk:true表示当内存缓存的对象数目达到了maxElementsInMemory界限后,会把溢出的对象写到硬盘缓存中。
注意:如果缓存的对象要写入到硬盘中的话,则该对象必须实现了Serializable接口才行。
diskSpoolBufferSizeMB:磁盘缓存区大小,默认为30MB。每个Cache都应该有自己的一个缓存区。
diskPersistent:是否缓存虚拟机重启期数据
diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认为120秒
timeToIdleSeconds: 设定允许对象处于空闲状态的最长时间,以秒为单位。当对象自从最近一次被访问后,如果处于空闲状态的时间超过了
timeToIdleSeconds属性值,这个对象就会过期,EHCache将把它从缓存中清空。只有当eternal属性为false,该属性才有效。
如果该属性值为0,则表示对象可以无限期地处于空闲状态
timeToLiveSeconds:设定对象允许存在于缓存中的最长时间,以秒为单位。当对象自从被存放到缓存中后,如果处于缓存中的时间超过了
timeToLiveSeconds属性值,这个对象就会过期,EHCache将把它从缓存中清除。只有当eternal属性为false,该属性才有效。
如果该属性值为0,则表示对象可以无限期地存在于缓存中。timeToLiveSeconds必须大于timeToIdleSeconds属性,才有意义
memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。
可选策略有:LRU(最近最少使用,默认策略)、FIFO(先进先出)、LFU(最少访问次数)。
-->
<defaultCache
maxElementsInMemory="10000"
eternal="false"
overflowToDisk="true"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120" />
<cache name="helloworld"
maxElementsInMemory="10000"
eternal="false"
overflowToDisk="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"/>
</ehcache>
最后就是我们的controller了
net.sf.ehcache.Cache cache = cacheManager.getCacheManager().getCache("helloworld"); //获取缓存对象
Element element = new Element("test",pil); //创建新缓存值对象
cache.put(element); //这里将缓存值对象存入缓存
Element el = cache.get("test"); //这里把存入的缓存取出
(类名)el.getValue(); //这里把缓存值取出,他是个object,我们强转以下就可以正常用了
知道了缓存的存取,那么就可以用在项目中了。