使用oscache提高程序性能
来源:互联网 发布:linux 下的虚拟机 编辑:程序博客网 时间:2024/05/21 19:41
一. 简介
oscache是一个广泛采用的高性能的j2ee缓存框架,能应用于任何java应用程序的普通的缓存解决方案。
二. 特点
1. 可以缓存任何对象, 你可以不受限制的缓存部分jsp页面或http请求,任何java对象都能缓存
2. 可以永久缓存,因为缓存能随意的写入硬盘
3. 支持集群
4. 可以控制缓存对象的过期
三. 使用
1.对局部页面缓存
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ taglib uri="http://www.opensymphony.com/oscache" prefix="cache"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><title>My JSP 'index.jsp' starting page</title></head><body><h1>测试1</h1> 没有缓存的日期:<%=new Date()%><br><!-- 页面局部缓存,使用标签 --><cache:cache time="5" key="cacheDate" scope="application"> 缓存局部页面,每5秒刷新缓存一次,日期: <%=new Date()%></cache:cache><br></body></html>2.对整个页面缓存
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ taglib uri="http://www.opensymphony.com/oscache" prefix="cache"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><title>My JSP 'index.jsp' starting page</title></head><body><!-- 页面全局缓存,在web.xml中配置 --><h1>测试2</h1>缓存整个页面,每10秒刷新缓存一次,日期:<%=new Date()%><br></body></html>3.缓存对象
public class CacheServlet extends HttpServlet {private static final long serialVersionUID = 1L;protected void doGet(HttpServletRequest request, HttpServletResponse response) {int refreshPeriod = 10; // cache in 6 secondsGeneralCacheAdministrator cache = new GeneralCacheAdministrator();try {// get data from cacheDate myDate = (Date) cache.getFromCache("myDate", refreshPeriod); System.out.println("current date is : " + myDate); } catch (NeedsRefreshException e) {try {Date myDate = new Date();// put data into cachecache.putInCache("myDate", myDate);} catch (Exception ex) {cache.cancelUpdate("myDate");}}}}oscache.properties
#默认在内存中作缓存,如设置为false,那cache只能缓存到数据库或硬盘中cache.memory=false#持久化缓存类,如此类打开,则必须设置cache.path信息 cache.persistence.class=com.opensymphony.oscache.plugins.diskpersistence.DiskPersistenceListener#建立磁盘缓存cache.path=D\:\\cache#缓存元素个数 cache.capacity=1000#cache.cluster 为集群设置信息。如#cache.cluster.multicast.ip为广播IP地址#cache.cluster.properties为集群属性web.xml
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"id="WebApp_ID" version="2.5"><filter><filter-name>CacheFilter</filter-name><!-- CacheFilter只捕获Http头为200的页面请求,即只对无错误请求作缓存 --><filter-class>com.opensymphony.oscache.web.filter.CacheFilter</filter-class><init-param><!-- 缓存内容的时间段,单位是秒,默认是3600秒 --><param-name>time</param-name><param-value>10</param-value></init-param><init-param><param-name>scope</param-name><param-value>application</param-value></init-param></filter><!-- cache all the cache.jsp file --><filter-mapping><filter-name>CacheFilter</filter-name><url-pattern>/cache2.jsp</url-pattern></filter-mapping><servlet><servlet-name>cacheServlet</servlet-name><servlet-class>com.zdp.cache.CacheServlet</servlet-class></servlet><servlet-mapping><servlet-name>cacheServlet</servlet-name><url-pattern>/cacheServlet</url-pattern></servlet-mapping><welcome-file-list><welcome-file>cache1.jsp</welcome-file></welcome-file-list></web-app>四. 核心API - GeneralCacheAdministrator:
1.void putInCache(String key,Object content);//put on Object in a cache
2.Object getFromCache(String key);// get on Object from the Cache
3.void removeEntry(String key);// remove an Object in a cache
4.void flushEntry(String key);// flushes a single cache entry
5.void flushAll();// flush the entire cache immediately
6.void cancelUpdate(String key);// cacels a pending cache update
参加资料:http://www.cnblogs.com/huzi007/p/3873396.html
1 0
- 使用oscache提高程序性能
- 使用OSCache优化程序性能
- 使用OSCache来提高javaEE性能
- 使用缓存技术来提高性能之——OSCache缓存技术入门(开篇)OSCache是什么?
- 使用缓存技术来提高性能之——OSCache缓存技术入门(一)
- 使用缓存技术来提高性能之——OSCache缓存技术入门(二)
- 使用异步 I/O 提高程序性能
- 使用SIMD技术提高C++程序性能
- 使用SIMD技术提高C++程序性能
- 使用SIMD技术提高C++程序性能
- 使用PAUSE提高程序的循环性能
- 提高程序性能
- 提高程序性能
- 程序性能提高
- 提高程序性能
- OSCache使用
- OScache使用
- OSCACHE使用
- poj 动态规划DP - 1163 The Triangle
- Opening Ceremony
- poj 2019 Cornfields
- Hdoj 5210 Delete 【水】
- mongodb
- 使用oscache提高程序性能
- Vimperator
- arch - pacman 使用方法
- ZOJ Problem Set - 3878||Convert QWERTY to Dvorak
- 不能在win挂载目录下进行内核编译
- java动态代理(JDK和cglib)
- OSG+VS2010+win7环境搭建
- ThinkPHP网站用Phpunit 测试
- MyEclipse无法启动--Runtime Environment (JRE) or Java Development Kit (JDK) must be available in order to