OSCache是当前运用最广的缓存方案。其主被用的最广泛功能是缓存页面,这里主要是用其缓存文件对象。
运用OScache的步骤:
1.取得oscache.jar 文件放到 /WEB-INF/lib 或相应类库目录 目录中。
2.oscache.jar依赖commons-collections.jar包。如果你的jdk版本为1.3,
建议在lib中加入Apache Common Lib 的commons-collections.jar包。
如jdk是1.4以上则不必要。
3.src根目录或发布环境的/WEB-INF/classes 目录下放入oscache.properties。
cache.memory
值为true 或 false ,默认为在内存中作缓存,
如设置为false,那cache只能缓存到数据库或硬盘中,那cache还有什么意义:)
cache.capacity
缓存元素个数
cache.persistence.class
持久化缓存类,如此类打开,则必须设置cache.path信息
cache.cluster 相关
为集群设置信息。
如cache.cluster.multicast.ip为广播IP地址
cache.cluster.properties为集群属性
cache.path
硬盘持久化时存放文件的目录。如果目录不存在OSCache会自动创建。
Windows系统:c://myapp//cache。其它:/opt/myapp/cache
cache.persistence.overflow.only*
是否只有当指定的内存缓存已经满时才进行持久化。推荐使用true,flase是为向后兼容。
cache.unlimited.disk
硬盘缓存是否有限制。缺省为cache.capacity指定的值
运用:
com.opensymphony.oscache.general.GeneralCacheAdministrator
GeneralCacheAdministrator主要对实现持久化对象的保存以及取出的相关的操作。
Object getFromCache(String key) //根据key获取缓存对象
Object getFromCache(String key , int refreshInterval)//refreshInterval时间内,根据key获取缓存对象
void putInCache(String key ,Object obj) //保存被缓存对象
void flushAll() //删除所有被缓存的对象
void flushAll(Date date) //在指定的时间去删除所有被缓存的对象
void cancelUpdate(String key) //取消未确定的更新
运用OScache的步骤:
1.取得oscache.jar 文件放到 /WEB-INF/lib 或相应类库目录 目录中。
2.oscache.jar依赖commons-collections.jar包。如果你的jdk版本为1.3,
建议在lib中加入Apache Common Lib 的commons-collections.jar包。
如jdk是1.4以上则不必要。
3.src根目录或发布环境的/WEB-INF/classes 目录下放入oscache.properties。
cache.memory
值为true 或 false ,默认为在内存中作缓存,
如设置为false,那cache只能缓存到数据库或硬盘中,那cache还有什么意义:)
cache.capacity
缓存元素个数
cache.persistence.class
持久化缓存类,如此类打开,则必须设置cache.path信息
cache.cluster 相关
为集群设置信息。
如cache.cluster.multicast.ip为广播IP地址
cache.cluster.properties为集群属性
cache.path
硬盘持久化时存放文件的目录。如果目录不存在OSCache会自动创建。
Windows系统:c://myapp//cache。其它:/opt/myapp/cache
cache.persistence.overflow.only*
是否只有当指定的内存缓存已经满时才进行持久化。推荐使用true,flase是为向后兼容。
cache.unlimited.disk
硬盘缓存是否有限制。缺省为cache.capacity指定的值
运用:
com.opensymphony.oscache.general.GeneralCacheAdministrator
GeneralCacheAdministrator主要对实现持久化对象的保存以及取出的相关的操作。
Object getFromCache(String key) //根据key获取缓存对象
Object getFromCache(String key , int refreshInterval)//refreshInterval时间内,根据key获取缓存对象
void putInCache(String key ,Object obj) //保存被缓存对象
void flushAll() //删除所有被缓存的对象
void flushAll(Date date) //在指定的时间去删除所有被缓存的对象
void cancelUpdate(String key) //取消未确定的更新
java 代码
package
com.iflytek;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.opensymphony.oscache.base.NeedsRefreshException;
import com.opensymphony.oscache.general.GeneralCacheAdministrator;
public class DisplayChart extends HttpServlet ... {
/** *//**
* Default constructor.
*/
public DisplayChart() ...{
super();
}
/** *//**
* Init method.
*
* @throws ServletException never.
*/
public void init() throws ServletException ...{
return;
}
public static GeneralCacheAdministrator cacheAdmin = new GeneralCacheAdministrator();
public void service(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException ...{
String path = getServletContext().getRealPath("/");
File file = null;
SimpleDateFormat sdf= new SimpleDateFormat("hh-mm-ss");
try ...{
file = (File)cacheAdmin.getFromCache(sdf.format(new Date()));
System.out.println("来自缓存!"+ sdf.format(new Date()));
} catch (NeedsRefreshException e) ...{
file = new File(path+"xmls/Pipe11.xml");
cacheAdmin.putInCache(sdf.format(new Date()), file);
System.out.println("--缓存没有!"+sdf.format(new Date()));
}
sendResponse(file,response);
return;
}
/** *//**
* 把文件用响应流写出
* @param file
* @param response
* @throws IOException
*/
public void sendResponse(File file,HttpServletResponse response) throws IOException...{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());
byte[] input = new byte[1024];
boolean eof = false;
while (!eof) ...{
int length = bis.read(input);
if (length == -1) ...{
eof = true;
}
else ...{
bos.write(input, 0, length);
}
}
bos.flush();
bis.close();
bos.close();
}
}
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.opensymphony.oscache.base.NeedsRefreshException;
import com.opensymphony.oscache.general.GeneralCacheAdministrator;
public class DisplayChart extends HttpServlet ... {
/** *//**
* Default constructor.
*/
public DisplayChart() ...{
super();
}
/** *//**
* Init method.
*
* @throws ServletException never.
*/
public void init() throws ServletException ...{
return;
}
public static GeneralCacheAdministrator cacheAdmin = new GeneralCacheAdministrator();
public void service(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException ...{
String path = getServletContext().getRealPath("/");
File file = null;
SimpleDateFormat sdf= new SimpleDateFormat("hh-mm-ss");
try ...{
file = (File)cacheAdmin.getFromCache(sdf.format(new Date()));
System.out.println("来自缓存!"+ sdf.format(new Date()));
} catch (NeedsRefreshException e) ...{
file = new File(path+"xmls/Pipe11.xml");
cacheAdmin.putInCache(sdf.format(new Date()), file);
System.out.println("--缓存没有!"+sdf.format(new Date()));
}
sendResponse(file,response);
return;
}
/** *//**
* 把文件用响应流写出
* @param file
* @param response
* @throws IOException
*/
public void sendResponse(File file,HttpServletResponse response) throws IOException...{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());
byte[] input = new byte[1024];
boolean eof = false;
while (!eof) ...{
int length = bis.read(input);
if (length == -1) ...{
eof = true;
}
else ...{
bos.write(input, 0, length);
}
}
bos.flush();
bis.close();
bos.close();
}
}