所需要的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.