一、客户端浏览器缓存
1、基于springmvc框架
在rest.xml中配置缓存拦截器
<mvc:interceptor>
<mvc:mapping path="/space/new" />
<bean class="org.springframework.web.servlet.mvc.WebContentInterceptor" >
<property name="cacheSeconds" value="300"/> 失效时间
<property name="useCacheControlHeader" value="true"></property>
<property name="useCacheControlNoStore" value="true"></property>
<property name="useExpiresHeader" value="true"></property>
</bean>
</mvc:interceptor>
附:http://www.iteye.com/topic/1121788;
2、基于HttpServletResponse
response.setContentType("text/html"); 16 //servlet页面默认是不缓存的 17 //本页面允许在浏览器端或缓存服务器中缓存,时限为20秒。 18 //20秒之内重新进入该页面的话不会进入该servlet的 19 java.util.Date date = new java.util.Date(); 20 response.setDateHeader("Last-Modified",date.getTime()); //Last-Modified:页面的最后生成时间 21 response.setDateHeader("Expires",date.getTime()+20000); //Expires:过时期限值 22 response.setHeader("Cache-Control", "public"); //Cache-Control来控制页面的缓存与否,public:浏览器和缓存服务器都可以缓存页面信息; 23 response.setHeader("Pragma", "Pragma"); //Pragma:设置页面是否缓存,为Pragma则缓存,no-cache则不缓存 24 25 //不允许浏览器端或缓存服务器缓存当前页面信息。 26 /*response.setHeader( "Pragma", "no-cache" ); 27 response.setDateHeader("Expires", 0); 28 response.addHeader( "Cache-Control", "no-cache" );//浏览器和缓存服务器都不应该缓存页面信息 29 response.addHeader( "Cache-Control", "no-store" );//请求和响应的信息都不应该被存储在对方的磁盘系统中; 30 response.addHeader( "Cache-Control", "must-revalidate" );*///于客户机的每次请求,代理服务器必须想服务器验证缓存是否过时;附:http://www.cnblogs.com/liuling/p/2013-7-25-01.html;
二、OSCache缓存
1、基于urlpatter级别缓存
web.xml配置
<!-- oscache缓存 -->
<filter><filter-name>cacheFilter</filter-name>
<filter-class>com.opensymphony.oscache.web.filter.CacheFilter</filter-class>
<init-param>
<param-name>scope</param-name>
<param-value>application</param-value>
</init-param>
<init-param>
<param-name>time</param-name>
<param-value>300</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>cacheFilter</filter-name>
<url-pattern>/rest/space/new</url-pattern>
<url-pattern>/rest/space/resource/newest</url-pattern>
</filter-mapping>
oscache.properties
cache.memory=true
cache.persistence.class=com.opensymphony.oscache.plugins.diskpersistence.DiskPersistenceListener
cache.path=D\:\\cache
jar 包
oscache-2.4.1.jar附:http://shijincheng0223.iteye.com/blog/1412128
三、spring ehcache缓存
<!-- spring对ehcache的缓存工厂支持 -->
<bean id="ehCacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache.xml" />
<property name="shared" value="true" />
</bean>
<!-- spring对ehcache的缓存管理 -->
<bean id="secondeEhCacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="ehCacheManagerFactory"></property>
</bean>
<!-- 使用缓存管理器 -->
<cache:annotation-driven cache-manager="secondeEhCacheManager" />
ehcache.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="false"
monitoring="autodetect" dynamicConfig="true">
<diskStore path="java.io.tmpdir"/>
<defaultCache
maxElementsInMemory="10000"
eternal="false"
overflowToDisk="true"
timeToIdleSeconds="120"
timeToLiveSeconds="240"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
diskSpoolBufferSizeMB="30"
maxElementsOnDisk="10000000"
memoryStoreEvictionPolicy="LRU"/>
<cache
name="index"
maxElementsInMemory="10000"
eternal="false"
overflowToDisk="true"
timeToIdleSeconds="120"
timeToLiveSeconds="240"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
diskSpoolBufferSizeMB="30"
maxElementsOnDisk="10000000"
memoryStoreEvictionPolicy="LRU"/>
</ehcache>
附:http://blog.csdn.net/a491057947/article/details/49870433;
四、hibernate ehcache二级缓存
1、基于注解配置
1)hibrnate.properties配置
hibernate.cache.region.factory_class org.hibernate.cache.SingletonEhCacheRegionFactory
hibernate.cache.provider_configuration_file_resource_path ehcache.xml
## disable the second-level cache
hibernate.cache.use_second_level_cache true
## enable the query cache
hibernate.cache.use_query_cache true
## store the second-level cache entries in a more human-friendly format
hibernate.cache.use_structured_entries true
2)实体注解配置
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
附:http://www.cnblogs.com/xiaoluo501395377/p/3377604.html;