OSCache 缓存运用

对于一些网站类型的项目.我们可以采取的方案就是减少和数据库方面的交换..这样我们可以使用缓存..由于项目使用Hibernate已经使用了Ehcache的2级缓存..这里可以在web层上使用oscache来进行缓存 

需要的jar包有:oscache.jar和lib\commons-logging.jar 
开发过程中需要将oscache.properties放在src下面

oscache又分为局部缓存和全局缓存.先介绍几个局部缓存比较常用的标签 

1.缓存的key将以请求的uri+查询字符串组成.如果你访问 
oscache/index.jsp?name=xxxoscache/index.jsp?name=ppp 
将得到两份缓存。缓存默认存放在application范围.缓存时间默认3600秒.即一个小时 


2.这时候缓存将以name为key。不再以请求的uri+查询字符串组成,所有如果你访问 
oscache/index.jsp?name=xxx和oscache/index.jsp?name=ppp 
将得到一份缓存

<cache:cache key="name">
name=${param.name}
</cache:cache>

3.缓存范围设置为session。这时候缓存保持的用户的session中.如果用户关闭浏览器.再重新打开一个新浏览器. 
原来的缓存内容将不存在

<cache:cache key="name" scope="session">
name=${param.name}
</cache:cache>

4.下面设置缓存时间为10秒,超过10秒后,缓存内容将消失掉. 

<cache:cache key="name" time="10">
name=${param.name}
</cache:cache>

5.refresh为true将导致缓存内容过期被清除.简单地说.该属性为true用于清除缓存 

<cache:cache key="name" time="60" refresh="${param.refresh}">
name=${param.name}
</cache:cache>

局部缓存清理缓存方式: 
人为管理缓存<flush>标签 
<cache:flush scope="application" /> 
清除application范围内的缓存 
<cache:flush scope="session" /> 
清除session范围内的缓存 
<cache:flush scope="application" group="currencvData"/> 
清除application范围内组名为currencvData的所有缓存 

而局部缓存的名字重复的话后面添加到缓存将取代前面的缓存.所以命名过程中需要有规范. 

下面是全局缓存的介绍.这里只需要在web.xml进行配置就可以使用到全局缓存 

<filter>
   <filter-name>CacheFilter</filter-name>
   <filter-class>com.opensymphony.oscache.web.filter.CacheFilter</filter-class>
   <init-param>
       <param-name>time</param-name>
       <param-value>7200</param-value>
   </init-param>
   <init-param>
       <param-name>scope</param-name>
       <param-value>application</param-value>
   </init-param>
</filter>
<filter-mapping>
   <filter-name>CacheFilter</filter-name>
   <url-pattern>/xxx/*</url-pattern>
</filter-mapping>

全局缓存是对整个页面的url地址为key缓存相应的页面. 
所以在使用全局缓存之前我们需要考虑清楚哪些页面的url我需要缓存的.在定义action的过程中需要考虑请求url的名字的命名..<url-pattern>/xxx/*</url-pattern> 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值