-
-12 27
-
Beside the JSP tag library and the CacheFilter you can use OSCache through its straightforward API. You can use the GeneralCacheAdministrator to create, flush and administrate the cache. The GeneralCacheAdministrator has a cache instance and delegates different Cache's methods. Furthermore the GeneralCacheAdministrator is in charge of load the cache.properties and create a cache instance with the properties definded. You have to store an instance of the GeneralCacheAdministrator in a static value or use a singleton pattern to access the same GeneralCacheAdministrator.
在用OSCACHE过程中,我们除了能用JSP标签库和缓存过滤器来使用它,还可以通过OSCACHE中的API GeneralCacheAdministrator 来创建缓存管理,GeneralCacheAdministrator可以创建缓存实例提供管理缓存的方法接口.更多的是,它能改变缓存属性配置文件并通过属性定义创建缓存实例.可以通过存储一个它的静态实例或使用单例模式得到同一个缓存管理者.
通过CacheManager类来看怎样缓存对象
主要的类:
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) //取消未确定的更新先看两代码:
代码1 :
package com.demo1;
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();
}
}代码2:
package com.demo2;
import java.util.Date;
import com.opensymphony.oscache.base.NeedsRefreshException;
import com.opensymphony.oscache.general.GeneralCacheAdministrator;
public class BaseCache extends GeneralCacheAdministrator {
//过期时间(单位为秒);
private int refreshPeriod;
//关键字前缀字符;
private String keyPrefix;
private static final long serialVersionUID = -4397192926052141162L;
public BaseCache(String keyPrefix,int refreshPeriod){
super();
this.keyPrefix = keyPrefix;
this.refreshPeriod = refreshPeriod;
}
//添加被缓存的对象;
public void put(String key,Object value){
this.putInCache(this.keyPrefix+"_"+key,value);
}
//删除被缓存的对象;
public void remove(String key){
this.flushEntry(this.keyPrefix+"_"+key);
}
//删除所有被缓存的对象;
public void removeAll(Date date){ this.flushAll(date); }
public void removeAll(){ this.flushAll(); }
//获取被缓存的对象;
public Object get(String key) throws Exception{
try{
return this.getFromCache(this.keyPrefix+"_"+key,this.refreshPeriod);
} catch (NeedsRefreshException e) {
this.cancelUpdate(this.keyPrefix+"_"+key); throw e;
}
}
}
官方网站举例:
部分代码省略,admin 为GeneralCacheAdministrator实例具体见使用文档
GeneralCacheAdministrator() Create the cache administrator.
GeneralCacheAdministrator(Properties p) Create the cache administrator with the specified properties--错误的操作代码
String myKey = "myKey";
String myValue;
int myRefreshPeriod = 1000;
try {
// Get from the cache
myValue = (String) admin.getFromCache(myKey, myRefreshPeriod);
} catch (NeedsRefreshException nre) {
try {
// Get the value (probably from the database)
myValue = "This is the content retrieved.";
// Store in the cache
admin.putInCache(myKey, myValue);
} catch (Exception ex) {
// We have the current content if we want fail-over.
myValue = (String) nre.getCacheContent();
// It is essential that cancelUpdate is called if the
// cached content is not rebuilt
admin.cancelUpdate(myKey);
}
}--正确的操作代码:
String myKey = "myKey";
String myValue;
int myRefreshPeriod = 1000;
try {
// Get from the cache
myValue = (String) admin.getFromCache(myKey, myRefreshPeriod);
} catch (NeedsRefreshException nre) {
try {
// Get the value (probably from the database)
myValue = "This is the content retrieved.";
// Store in the cache
admin.putInCache(myKey, myValue);
updated = true;
} finally {
if (!updated) {
// It is essential that cancelUpdate is called if the
// cached content could not be rebuilt
admin.cancelUpdate(myKey);
}
}
}
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。
- 评论(0)
发表评论 TrackBack