📄 replacecomputinglru.java
字号:
//Source file: E:/工作和学习/工作/硕士论文工作/程序/数据库缓存管理/20040304/src/MemCachePak/ReplaceComputingLRU.java
package MemCachePak;
public class ReplaceComputingLRU implements ReplaceComInterface
{
public MemCacheInterface theCache;
public ReplaceComputingLRU()
{
}
/**
@roseuid 41C63B8A02CE
*/
public boolean InsertNewObj(String[] RefArray, int ObjSize, int TimeCost)
{
WebObj obj = new WebObj();
obj.SetSourceFileName(RefArray[0]);
obj.SetCachedFileName(RefArray[1]);
obj.SetObjRef(RefArray);
obj.SetCost(TimeCost);
obj.SetFileSize(ObjSize);
obj.SetEnterTime();
theCache.InsertNewObj(obj);
theCache.InsertNewVector(TimeCost,RefArray[0],RefArray[1],RefArray[0]);
return true;
}
/**
@roseuid 41C63CBA0128
*/
public boolean InsertNewObj(String[] RefArray, int ObjSize, int TimeCost, String BeginPoint, boolean SourceOrMedia)
{
WebObj obj = new WebObj();
obj.SetSourceFileName(RefArray[0]);
obj.SetCachedFileName(RefArray[1]);
obj.SetObjRef(RefArray);
obj.SetCost(TimeCost);
obj.SetFileSize(ObjSize);
obj.SetEnterTime();
theCache.InsertNewObj(obj);
if(SourceOrMedia)
{
theCache.InsertNewVector(TimeCost,RefArray[0],RefArray[1],RefArray[0]);
}
else
{
WebObj MediaObj = theCache.GetCachedObj(RefArray[0],BeginPoint);
if(MediaObj!=null)
{
MediaObj.AddMediaUsedCount();
MediaObj.SetLastMediaUsedTime();
}
theCache.InsertNewVector(TimeCost,BeginPoint,RefArray[1],RefArray[0]);
}
return true;
}
/**
@roseuid 41C63D2900CB
*/
public boolean ReplaceCache()
{
//LRU算法的具体策略在这里实现
//首先判断缓存大小是否超过了门限
//如果超过了门限,则执行替换算法,返回true
if(theCache.GetCacheSize()>=Configuration.MAX_SIZE)
{
System.out.println("缓存进行了一次替换LRU");
//theCache.Print();
WebObj headObj,NowObj,LRUObj;
LRUObj = null;
headObj = theCache.GetTheFirstObj();
long TimeNow=System.currentTimeMillis();
long LRU = TimeNow;
while(headObj!=null)
{
NowObj = headObj;
while(NowObj!=null)
{
long lastusedtime = 0;
lastusedtime=NowObj.GetLastUsedTime(1);
//判断
if(lastusedtime<LRU)
{
LRU = lastusedtime;
LRUObj = NowObj;
}
//转到下一个版本对象
NowObj = NowObj.NextObjEdition;
}
//转到下一个版本对象群
headObj = headObj.NextObj;
}
if(LRUObj!=null)
{
//theCache.Print();
theCache.DeleteObj(LRUObj);
//同步删除文件
}
//theCache.Print();
return true;
}
else
{
//如果没有超过门限,则不执行替换算法,返回false
return false;
}
}
/**
@roseuid 41C63EA501E4
*/
protected ReplaceComputingLRU(MemCacheInterface Cache)
{
theCache = Cache;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -