oscache 使用

之前做的项目面临一个优化的问题..对于一些网站类型的项目.我们可以采取的方案就是减少和数据库方面的交换..这样我们可以使用缓存..由于项目使用Hibernate已经使用了Ehcache的2级缓存..这里可以在web层上使用oscache来进行缓存
  oscache下载地址 http://www.opensymphony.com/oscache/download.action
需要的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
将得到一份缓存
Java代码 复制代码  收藏代码
  1. <cache:cache key="name">   
  2. name=${param.name}   
  3. </cache:cache>  
<cache:cache key="name">
name=${param.name}
</cache:cache>



3.缓存范围设置为session。这时候缓存保持的用户的session中.如果用户关闭浏览器.再重新打开一个新浏览器.
原来的缓存内容将不存在
Java代码 复制代码  收藏代码
  1. <cache:cache key="name" scope="session">   
  2. name=${param.name}   
  3. </cache:cache>  
<cache:cache key="name" scope="session">
name=${param.name}
</cache:cache>



4.下面设置缓存时间为10秒,超过10秒后,缓存内容将消失掉.
Java代码 复制代码  收藏代码
  1. <cache:cache key="name" time="10">   
  2. name=${param.name}   
  3. </cache:cache>  
<cache:cache key="name" time="10">
name=${param.name}
</cache:cache>



5.refresh为true将导致缓存内容过期被清除.简单地说.该属性为true用于清除缓存
Java代码 复制代码  收藏代码
  1. <cache:cache key="name" time="60" refresh="${param.refresh}">   
  2. name=${param.name}   
  3. </cache:cache>  
<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进行配置就可以使用到全局缓存
Java代码 复制代码  收藏代码
  1. <filter>   
  2.             <filter-name>CacheFilter</filter-name>   
  3.             <filter-class>com.opensymphony.oscache.web.filter.CacheFilter</filter-class>   
  4.             <init-param>   
  5.                 <param-name>time</param-name>   
  6.                 <param-value>7200</param-value>   
  7.             </init-param>   
  8.             <init-param>   
  9.                 <param-name>scope</param-name>   
  10.                 <param-value>application</param-value>   
  11.             </init-param>   
  12.     </filter>   
  13.     <filter-mapping>   
  14.             <filter-name>CacheFilter</filter-name>   
  15.             <url-pattern>/xxx/*</url-pattern>   
  16.     </filter-mapping>  
<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>

总结:在许多网站中都使用了缓存技术.而oscache使用面也比较广..使用方式根据个人的项目而定..我只是将自己了解总结一下..还有在了解许多工具和开源框架中发现许多东西都要有明确的命名规范和规定..在我们项目开发中也需要更多的规范和约束才能使得项目更好的运作..感觉开源框架也是别人所写的项目..我们可以从中得到许多信息..
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值