引入OSCache的jar包
package com.sun.utils;
import java.util.Date;
import com.opensymphony.oscache.base.NeedsRefreshException;
import com.opensymphony.oscache.general.GeneralCacheAdministrator;
public class BaseCache extends GeneralCacheAdministrator{
private static final long serialVersionUID = -4397192926052141162L;
private String keyPrefix; //关键字前缀字符,区别属于哪个模块
private int refreshPeriod; //过期时间(单位为秒);
public BaseCache(String keyPrefix,int refreshPeriod){
super();
this.keyPrefix = keyPrefix;
this.refreshPeriod = refreshPeriod;
}
/**
*
* put(添加被缓存的对象)
* @param key
* @param value
* @return void
* @createDate 2013-1-31 下午04:16:46
* @since 1.0.0
*/
public void put(String key,Object value){
this.putInCache(this.keyPrefix+"_"+key,value);
}
/**
* remove(删除被缓存的对象)
* @param key
* @return void
* @createDate 2013-1-31 下午04:17:06
* @since 1.0.0
*/
public void remove(String key){
this.flushEntry(this.keyPrefix+"_"+key);
}
/**
* removeAll(删除指定日期所有被缓存的对象)
* @param date
* @return void
* @createDate 2013-1-31 下午04:17:29
* @since 1.0.0
*/
public void removeAll(Date date){
this.flushAll(date);
}
/**
* removeAll(删除所有被缓存的对象)
* @return void
* @createDate 2013-1-31 下午04:18:01
* @since 1.0.0
*/
public void removeAll(){
this.flushAll();
}
/**
* get(获取被缓存的对象)
* @param key
* @return
* @throws Exception
* @return Object
* @createDate 2013-1-31 下午04:18:45
* @since 1.0.0
*/
public Object get(String key) throws Exception{
try{
return this.getFromCache(this.keyPrefix+"_"+key,this.refreshPeriod);
} catch (NeedsRefreshException e) {
this.cancelUpdate(this.keyPrefix+"_"+key);
e.printStackTrace();
throw e;
}
}
}
=============================================
package com.sun.utils;
public class User {
}
=============================================
package com.sun.utils;
public interface UserCacheService {
/**
* 前缀,用来区别于各个模块的缓存
*/
public String KEY_PREFIX="_user";
/**
*缓存刷新的时间
*/
public int REFRESH_PERIOD=3600;
/**
* putInCache(添加用户信息到缓存中)
* @param userId
* @param user
* @return void
* @createDate 2013-1-31 下午04:40:31
* @since 1.0.0
*/
public void putInCache(String userId,User user);
/**
* removeFromCache(在缓存中删除该用户信息)
* @param userId
* @return void
* @createDate 2013-1-31 下午04:40:58
* @since 1.0.0
*/
public void removeFromCache(String userId);
/**
* removeAll(删除该模块的所有缓存)
* @return void
* @createDate 2013-1-31 下午04:47:09
* @since 1.0.0
*/
public void removeAll();
/**
*
* getFromCache(根据用户ID获取缓存中的信息)
* @param userId
* @return void
* @createDate 2013-1-31 下午04:41:30
* @since 1.0.0
*/
public User getFromCache(String userId);
}
===========================================
package com.sun.utils;
public class UserCacheServiceImpl implements UserCacheService {
private BaseCache cache=new BaseCache(KEY_PREFIX, REFRESH_PERIOD);
@Override
public void putInCache(String userId, User user) {
cache.put(userId, user);
}
@Override
public void removeFromCache(String userId) {
cache.remove(userId);
}
@Override
public void removeAll() {
cache.removeAll();
}
@Override
public User getFromCache(String userId) {
User user=null;
try {
user= (User)cache.get(userId);
} catch (Exception e) {
e.printStackTrace();
}
return user;
}
}
-------------------------------------------------------------------------------------
OSCached缓存页面
<filter>
<filter-name>CacheFilter</filter-name>
<filter-class>com.opensymphony.oscache.web.filter.CacheFilter</filter-class>
<init-param>
<param-name>time</param-name>
<param-value>600</param-value>
</init-param>
<init-param>
<param-name>scope</param-name>
<param-value>session</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CacheFilter</filter-name>
<!-- 对所有jsp页面内容进行缓存 -->
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
在页面级缓存的情况下,可以通过设置CacheFilter的初始属性来决定缓存的一些特性:time属性设置缓存的时间段,默认为3600秒,可以根据自己的需要只有的设置,而scope属性设置,默认为application,可选项包括application、session
缓存页面的部分