参考:
http://www.klstudio.com/post/123.html
一. 创建需要缓存的对象
package com.zxf.object;
public class News {
String id;
String message;
public News() {
super();
}
public News(String id) {
super();
this.id = id;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
二. 创建基础缓存类
package com.zxf.oscache;
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;
}
}
}
三. 创建cache管理类
package com.zxf.oscache;
import com.zxf.object.News;
public class CacheManager {
private BaseCache newCache;
private static CacheManager instance;
private static Object lock = new Object();
public CacheManager() {
// 这个根据配置文件来,初始BaseCache而已,测试用10秒就过期
newCache = new BaseCache("new", 10);
}
public static CacheManager getInstance() {
if (instance == null) {
synchronized (lock) {
if (instance == null) {
instance = new CacheManager();
}
}
}
return instance;
}
public void putnew(News news) {
// TODO 自动生成方法存根
newCache.put(news.getId(), news);
}
public void removenew(String newID) {
// TODO 自动生成方法存根
newCache.remove(newID);
}
public News getnew(String id) {
// TODO 自动生成方法存根
try {
return (News) newCache.get(id);
} catch (Exception e) {
// TODO 自动生成 catch 块
System.out.println("getnew>>newID[" + id + "]>>" + e.getMessage());
News news = new News(id);
this.putnew(news);
return news;
}
}
public void removeAllnew() {
// TODO 自动生成方法存根
newCache.removeAll();
}
}
四. 最后创建两个servlet 运行例子
package com.zxf.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.zxf.object.News;
import com.zxf.oscache.CacheManager;
/**
* 刷新oscache
*/
public class FlushNews extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public FlushNews() {
super();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
CacheManager cache = CacheManager.getInstance();
News news = new News();
news.setId("123");
news.setMessage("Oscache test.");
cache.putnew(news);
response.getWriter().print("flush news success~~~");
response.flushBuffer();
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
package com.zxf.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.zxf.object.News;
import com.zxf.oscache.CacheManager;
/**
* 刷新oscache
*/
public class PublishNews extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public PublishNews() {
super();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
CacheManager cache = CacheManager.getInstance();
News getnew = cache.getnew("123");
StringBuilder sb = new StringBuilder();
sb.append("<table>");
sb.append("<tr>");
sb.append("<td>");
sb.append("new's id");
sb.append("</td>");
sb.append("<td>");
sb.append(getnew.getId());
sb.append("</td>");
sb.append("</tr>");
sb.append("<tr>");
sb.append("<td>");
sb.append("new's message");
sb.append("</td>");
sb.append("<td>");
sb.append(getnew.getMessage());
sb.append("</td>");
sb.append("</tr>");
sb.append("</table>");
response.getWriter().print(sb.toString());
response.flushBuffer();
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
附上servlet配置文件web.xml
<servlet>
<description></description>
<display-name>FlushNews</display-name>
<servlet-name>FlushNews</servlet-name>
<servlet-class>com.zxf.servlet.FlushNews</servlet-class>
</servlet>
<servlet>
<description></description>
<display-name>PublishNews</display-name>
<servlet-name>PublishNews</servlet-name>
<servlet-class>com.zxf.servlet.PublishNews</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FlushNews</servlet-name>
<url-pattern>/FlushNews</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>PublishNews</servlet-name>
<url-pattern>/PublishNews</url-pattern>
</servlet-mapping>