所须要的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
# 内存中缓存的最大容量
html
cache.capacity=1000node
ibatis.xml:
web
<?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
算法
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd"> <sqlMap namespace="Users"> <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.xmlspring
<!-- 在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中。
sql
<!-- 针对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页面
apache
<oscache:cache> <jsp:include page ="/view.do"/> </oscache:cache>
OSCache的配置比较活的,你能够根据你的状况进行相应的配置。它主要是针对页面级的对象。
简单的说,缓存就是Map<key,value>,建立缓存就是添加一个map,使用就是经过key取value.
缓存