OSCACHE

所需要的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"?>
<!DOCTYPE sqlMap     
    PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.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"/>
        <flushOnExecute statement="aaa.insertUser"/>
         <flushOnExecute statement="aaa.updateUser"/>
        <flushOnExecute statement="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 as userId , 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 into users(name,age) values(#userName#,#userAge#);
    </insert>
   
    <!-- 删除用户 -->
    <delete id="deleteUser" parameterClass="user">
        delete from users where id = #userId#;
    </delete>
   
    <!-- 修改用户 -->
    <update id="updateUser" parameterClass="user">
        update users set 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中应用的话,还需简单的引入。<%@ taglib uri="oscache" prefix="cache" %>
具体的应用:<cache:cache key="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:include page ="/view.do"/>
</oscache:cache>

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值