oscache

来源:互联网 发布:二分法查找c语言 编辑:程序博客网 时间:2024/04/29 16:24

所需要的jar包:

oscache-2.4.1.jar

假如要用oscache注解的话还需jar:

cglib-nodep-2.1_3.jar
commons-logging.jar
log4j-1.2.15.jar
spring-modules-cache.jar
spring.jar

oscache.properties配置如下:

# 是否使用内存作为缓存空间
cache.memory=true
cache.key=_oscache_cache
# 如果使用磁盘缓存(cache.memory=true),则需要指定磁盘存储接口实现
cache.persistence.class=com.opensymphony.oscache.plugins.diskpersistence.DiskPersistenceListener
# ie Windows:
cache.path=D:\\dcache
# 缓存调度算法
cache.algorithm=com.opensymphony.oscache.base.algorithm.LRUCache

#缓存管理事件监听器,通过这个监听器可以获知当前Cache的运行情况  cache.event.listeners=com.opensymphony.oscache.plugins.clustersupport.JMSBroadcastingListener 


# 内存中缓存的最大容量
cache.capacity=1000

ibatis.xml

<?xml version="1.0"encoding="UTF-8"?>
<!DOCTYPEsqlMap     
    PUBLIC"-//ibatis.apache.org//DTD SQL Map2.0//EN"     
   "http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap namespace="aaa">


type:

当type="OSCACHE",程序将自动在classpath下寻找oscache.properties

   <cacheModel id="product-cache"type="OSCACHE">
      <flushInterval hours="24"/>
      <flushOnExecutestatement="aaa.insertUser"/>
       <flushOnExecutestatement="aaa.updateUser"/>
      <flushOnExecutestatement="aaa.deleteUser"/>
      <property name="size" value="1000"/>
   </cacheModel>
   
   <typeAlias alias="user"type="com.test.po.TestUser"/>
   <resultMap class="com.test.po.TestUser"id="resultUser">
      <result property="userId"column="id"/>
      <result property="userName"column="name"/>
      <result property="userAge"column="age"/>
   </resultMap>
   
   <!-- 根据ID查询用户 -->
   <select id="selectByUserId" parameterClass="user"resultClass="user">
       select id asuserId , name as userName , age as userAge from users where id =#userId#;
   </select>
   
   <!-- 查询用户集合 -->
   <select id="selectByList" resultMap="resultUser"cacheModel="product-cache">
       select *from users;
   </select>
   
   <!-- 添加用户 -->
   <insert id="insertUser"parameterClass="user">
       insert intousers(name,age) values(#userName#,#userAge#);
   </insert>
   
   <!-- 删除用户 -->
   <delete id="deleteUser"parameterClass="user">
       delete fromusers where id = #userId#;
   </delete>
   
   <!-- 修改用户 -->
   <update id="updateUser"parameterClass="user">
       update usersset name = #userName# where id = #userId#;
   </update>
</sqlMap>

OSCACHE还有个它主要的功能就是对页面的缓存
在web.xml进行一些简单的配置,oscache提供了强大的标签。主要是针对jsp的,如果项目中需把oscache.tld放到WEB-INF/classes下,在再web.xml中配置一下。

web.xml


 
<!-- 在jsp中使用oscahche标签-->
 <jsp-config>
  <taglib>
  <taglib-uri>oscache</taglib-uri>
  <taglib-location>
   /WEB-INF/classes/oscache.tld
  </taglib-location>
  </taglib>
 </jsp-config>

在jsp中应用的话,还需简单的引入。<%@ tagliburi="oscache" prefix="cache" %>
具体的应用:<cache:cachekey="testcache"></cache:cache>。

以上是针对具体的页面的配置步骤。如果想针对某一页面的类型进行配置,可以利用过滤器来配置。比如我们想对以*.jsp的页面进行缓存的话,我们只需这样简单的配置在web.xml中。

 <!-- 针对jsp页面进行缓存-->
<filter>  
  <filter-name>CacheFilter</filter-name>
  <filter-class>com.opensymphony.oscache.web.filter.CacheFilter</filter-class>

    <init-param>    
    <!-- oscache缓存过期时间--> 
          <param-name>time</param-name>
          <param-value>60</param-value> 
    </init-param>  
    <init-param>                          
         <param-name>scope</param-name>
         <param-value>session</param-value>
     </init-param>

  </filter>
<filter-mapping>
      <filter-name>CacheFilter</filter-name>
      <url-pattern>*.jsp</url-pattern>
</filter-mapping>   

如果想对某一访问的路径action进行缓存的话,我们可以这样做,在一个jsp文件中简单的加上
jsp页面
<oscache:cache>
   <jsp:includepage ="/view.do"/>
</oscache:cache>



OSCache的配置比较活的,你可以根据你的情况进行相应的配置。它主要是针对页面级的对象。
简单的说,缓存就是Map<key,value>,创建缓存就是添加一个map,使用就是通过key取value.

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 便利店转不出去怎么办 便利店开业营业额低怎么办 现在etc超时了怎么办? 中国银行e贷逾期怎么办 招商银行e分期逾期怎么办 保单贷款还不了怎么办 离婚时按揭房怎么办 按揭房子不要了怎么办? 征信有问题怎么办房贷 夫妻一方不做房贷共还人怎么办 给你花逾期一年怎么办 文件夹密码忘记了怎么办 网址被qq拦截怎么办 手机qq邮箱中毒怎么办 邮箱附件带病毒怎么办? 打开了病毒附件怎么办? qq邮箱被拦截怎么办 邮件地址已被注册怎么办 信用卡欠款怎么办房贷 贷款还不起了怎么办 装修贷款无抵押怎么办 黑户买房做贷款怎么办 易分期无法还款怎么办 工行晚还款一天怎么办 手机银行网页打不开怎么办 顺丰分拣错误怎么办 超市生意越来越差怎么办 收到逾期催收函怎么办 手机贷逾期了怎么办 临额到期还不上怎么办 信用卡不想用了怎么办 授信时间到了怎么办 餐饮吃到苍蝇怎么办 商户二级处罚了怎么办 立刷蓝牙连不上怎么办 传奇3骑不了怎么办 房子说话有回音怎么办 老师声音哑了怎么办 人流后还有残留怎么办 人流后有残留物怎么办 模拟人生3外星人怎么办