📄 cache.java
字号:
package MyNa.utils;
/* the Cache class is a framework for defining caching, or
'memoizing', Singleton classes; each needs to define the
static getInstance, freeInstance, and close methods.
Suggested overrides are included below, using static
variables "instance" which holds the singleton, and
"clients" which holds a count of instance's users.
We would define Cache as an abstract class with those
three abstract methods, except that abstract methods
can't be static (or synchronized). Remember to override them.
*/
import java.util.*;
public class Cache {
public static synchronized Cache getInstance(){return null;}
public static synchronized int freeInstance(){return 0;}
public static synchronized boolean close()throws Exception{return false;}
Logger lg;
protected Cachetable cache=null;
// the cache for answers; this is the only actual instance variable
protected Cache(){init();} // constructor only called internally
protected void init(){
cache=new Cachetable();
lg=new Logger();
} // set up cache; can register with a CacheRegistry here.
/*
The static variables ( instance and clients) and static methods
getInstance,freeInstance,and close must be overridden;
they refer to the static variables. The overrides are normally
trivial (just use the commented-out code below) but needed: we want
one instance of each Cache subclass, not one instance of Cache.
The freeItem(Object ob) method can be overridden to do anything
ob needs before tossing it out of the cache, but need not be.
*/
/* the following static vars & methods can be copied into a subclass */
/* ****************SUGGESTED OVERRIDE BLOCK*********************** */ /*
private static Cache instance=null; // the one and only class instance
private static int clients=0; // how many are asking us?
public static synchronized Cache getInstance(){
if(null==instance)instance=new Cache(); // "Cache" -> subclass name
clients++;
return instance;
}
public static synchronized int freeInstance(){
if(null==instance)return 0;
clients--;
if(clients==0)close(); // all gone, and the cache with it.
return clients;
}
public static synchronized boolean close()throws Exception{
clients=0;instance=null; return true;
// might want to close up objects here, using instance.freeSpace
// and overriding freeItem to be called by it.
}
*/ /* ************ END SUGGESTED OVERRIDE BLOCK*********************** */
public boolean freeItem(Object ob){
return true;
// called by freeSpace; override if you need to do anything here.
}
public int freeSpace(int numToFree){
return cache.freeSpace(numToFree);
}
public Object get(Object k){
return cache.get(k);
}
public Object get(Object k1,Object k2){
return cache.get(k1,k2);
}
public Object get(Object k1,Object k2,Object k3){
return cache.get(k1,k2,k3);
}
public Object put(Object k,Object v){
cache.put(k,v);
return v;
}
public Object put(Object k1,Object k2,Object v){
cache.put(k1,k2,v);
return v;
}
public Object put(Object k1,Object k2,Object k3,Object v){
cache.put(k1,k2,k3,v);
return v;
}
class Cachetable extends Hashtable {
public Cachetable(){}
public synchronized Object put(Object k,Object v){
if(null==k)k="";
return super.put(k,v);
}
public synchronized void put(Object k,Object k2,Object v){
Cachetable subCache=(Cachetable)get(k);
if(null==subCache)put(k,subCache=new Cachetable());
subCache.put(k2,v);
}
public synchronized void put(Object k,Object k2,Object k3,Object v){
Cachetable subCache=(Cachetable)get(k);
if(null==subCache)put(k,subCache=new Cachetable());
subCache.put(k2,k3,v);
}
public synchronized Object get(Object k){
return super.get(null==k?"":k);
}
public synchronized Object get(Object k,Object k2){
Cachetable subCache=(Cachetable)get(k);
if(null==subCache)return null;
return subCache.get(k2);
}
public synchronized Object get(Object k,Object k2,Object k3){
Cachetable subCache=(Cachetable)get(k);
if(null==subCache)return null;
return subCache.get(k2,k3);
}
public synchronized int freeSpace(int numToFree){ // returns leftover.
Enumeration enum=keys();
while(enum.hasMoreElements() && numToFree>0){
Object k=enum.nextElement();
Object v=get(k);
if(v instanceof Cachetable){
Cachetable sub=(Cachetable)v;
numToFree=sub.freeSpace(numToFree);
if(numToFree>0)remove(k); // didn't free enough, must be empty.
}else {
freeItem(v);
remove(k);
numToFree--;
}
}
return numToFree; // this many still not freed.
}
} // end of Cachetable inner class
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -