在我们开发网站中,必可避免需要将很多经常使用的数据进行缓存处理,这里有一个非常好的开源框架介绍给大家OSCache,而且很好用,还支持集群,强烈推荐大家使用,使用步骤:
定义一个标识接口,即实现他的类用来做缓存处理,代码如下
- /**
- * @author Administrator
- * 缓存对象的标识接口
- */
- public interface IOSCachable{
- public String toString();
- }
/**
* @author Administrator
* 缓存对象的标识接口
*/
public interface IOSCachable{
public String toString();
}
缓存操作类
- GeneralCacheAdministrator admin;
- public OSCache(){
- admin = new GeneralCacheAdministrator();
- }
- public OSCache(int size) {
- admin = new GeneralCacheAdministrator();
- admin.setCacheCapacity(size);
- }
- public void put(String key, String value) {
- this.admin.putInCache(key,value);
- }
- public void put(String key,IOSCachable obj){
- this.admin.putInCache(key,obj);
- }
- public void put(String key,String value,String[] groups){
- this.admin.putInCache(key,value,groups);
- }
- public void put(String key,IOSCachable obj,String[] groups){
- this.admin.putInCache(key,obj,groups);
- }
- public IOSCachable get(String key,int myRefreshPeriod){
- try{
- return (IOSCachable)this.admin.getFromCache(key,myRefreshPeriod);
- }catch(NeedsRefreshException ex){
- this.admin.cancelUpdate(key);
- return null;
- }
- }
- public Object get(String key){
- try{
- return this.admin.getFromCache(key);
- }catch(NeedsRefreshException ex){
- this.admin.cancelUpdate(key);
- return null;
- }
- }
- public void flushAll(){
- this.admin.flushAll();
- }
GeneralCacheAdministrator admin;
public OSCache(){
admin = new GeneralCacheAdministrator();
}
public OSCache(int size) {
admin = new GeneralCacheAdministrator();
admin.setCacheCapacity(size);
}
public void put(String key, String value) {
this.admin.putInCache(key,value);
}
public void put(String key,IOSCachable obj){
this.admin.putInCache(key,obj);
}
public void put(String key,String value,String[] groups){
this.admin.putInCache(key,value,groups);
}
public void put(String key,IOSCachable obj,String[] groups){
this.admin.putInCache(key,obj,groups);
}
public IOSCachable get(String key,int myRefreshPeriod){
try{
return (IOSCachable)this.admin.getFromCache(key,myRefreshPeriod);
}catch(NeedsRefreshException ex){
this.admin.cancelUpdate(key);
return null;
}
}
public Object get(String key){
try{
return this.admin.getFromCache(key);
}catch(NeedsRefreshException ex){
this.admin.cancelUpdate(key);
return null;
}
}
public void flushAll(){
this.admin.flushAll();
}
测试代码如下:
- //缓存对象
- for(int i=0;i<5;i++){
- objCmu.put("obj"+i,new CacheBeanTest("myname","ii"+i));
- }
- //读取缓存
- System.out.println(objCmu.get("obj3"));
- System.out.println(objCmu.get("obj4"));
- System.out.println(objCmu.get("obj7"));
//缓存对象
for(int i=0;i<5;i++){
objCmu.put("obj"+i,new CacheBeanTest("myname","ii"+i));
}
//读取缓存
System.out.println(objCmu.get("obj3"));
System.out.println(objCmu.get("obj4"));
System.out.println(objCmu.get("obj7"));
OSCache几个常用的配置参数说明:
- #最近最常使用算法 cache.algorithm=com.opensymphony.oscache.base.algorithm.LRUCache
- #先近先出使用算法
- cache.algorithm=com.opensymphony.oscache.base.algorithm.FIFOCache
- #缓存大小
- cache.capacity=1000
#最近最常使用算法 cache.algorithm=com.opensymphony.oscache.base.algorithm.LRUCache
#先近先出使用算法
cache.algorithm=com.opensymphony.oscache.base.algorithm.FIFOCache
#缓存大小
cache.capacity=1000