oscache的简单配置与使用

oscache的简单配置与使用


准备工作:

0. 下载、解压缩OSCache 
地址:https://java.net/downloads/oscache/  本人使用2.4版本

解压后的目录:




1.搭建动态web项目,

oscache-2.4.jar拷贝到WEB_INF下的lib目录下

把etc目录里面的2个文件oscache.properties和oscache.tld拷贝到项目的src目录下


2.修改oscache.properties文件

#cache.memory = false 去除# 改false为true

cache.memory = true

开启内存缓存


使用:


案例一:

1先向jsp文件中引入标签  <%@taglib uri="http://www.opensymphony.com/oscache"  prefix="oscache" %>

2.使用oscache:cache 标签去包裹去要缓存的内容


<%@page import="java.util.Date"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib uri="http://www.opensymphony.com/oscache" prefix="oscache"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录</title>
</head>
<body>
现在时间:<%= new Date() %> <br />

<oscache:cache time="3">
缓存时间:<%= new Date() %>

</oscache:cache>


案例二:

       jsp页面不需要任何变动(jsp页面不用引入oscache标签),直接在web.xml中添加对jsp文件的过滤并进行缓存,具体配置如下:


    <filter>
        <filter-name>oscache</filter-name>
        <filter-class>com.opensymphony.oscache.web.filter.CacheFilter</filter-class>
        <init-param>
            <param-name>time</param-name>
            <param-value>5</param-value>  // 时间单位是妙
        </init-param>
        <init-param>
            <param-name>scope</param-name>
            <param-value>application</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>oscache</filter-name>
        <url-pattern>*.jsp</url-pattern>  //对所所有jsp页面有效
    </filter-mapping>


说明:


jsp中<%@taglib uri="http://www.opensymphony.com/oscache" prefix="oscache"%>

的等价于写法

<%@taglib uri="cache" prefix="oscache"%>

并在web.xml中添加如下配置

<jsp-config>
        <taglib>
            <taglib-uri>cache</taglib-uri>
            <taglib-location>classpath:oscache.tld</taglib-location>
        </taglib>
</jsp-config>


前者是直接网络上去找,后者是使用本机离线文件


其他:

osCache常用标签例如

<oscache:cache time="30" >

 //自上一次刷新后开始计时,过了30秒后刷新页面.页面内容才会更新.

<oscache:cache>


<cache:flush  refresh="${param.fre}" scope="application"/>

<a href="cache1.jsp">返回</a>

</body>

支持el表达式可从param和session域中取值






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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值