⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 replacecomputinglfu.java

📁 一宗队列算法
💻 JAVA
字号:
//Source file: E:/工作和学习/工作/硕士论文工作/程序/数据库缓存管理/20040304/src/MemCachePak/ReplaceComputingLFU.java

package MemCachePak;


public class ReplaceComputingLFU implements ReplaceComInterface
{
   public MemCacheInterface theCache;

   public ReplaceComputingLFU()
   {
   }

   /**
   @roseuid 41C63EED0138
   */
   public ReplaceComputingLFU(MemCacheInterface Cache)
   {
     theCache = Cache;
   }

   /**
   @roseuid 41C63FBA03B9
   */
   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 41C63FBB002E
   */
   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 41C63FBB00DA
   */
   public boolean ReplaceCache()
   {
     //LFU算法的具体策略在这里实现
     //首先判断缓存大小是否超过了门限
     //如果超过了门限,则执行替换算法,返回true
     if(theCache.GetCacheSize()>=Configuration.MAX_SIZE)
     {
       System.out.println("缓存进行了一次替换LFU");
       //theCache.Print();
       WebObj headObj,NowObj,LFUObj;
       LFUObj = null;
       headObj = theCache.GetTheFirstObj();
       long TimeNow=System.currentTimeMillis();
       float LFU = (float)Configuration.K;
       while(headObj!=null)
       {
         NowObj = headObj;

         while(NowObj!=null)
         {
           //该对象版本的直接使用概率,利用滑动平均方法近似
            float ri = 0;
            if(NowObj.GetUsedCount()>=Configuration.K)
            {
              //计算使用概率
              ri=  (float)Configuration.K/(TimeNow -
                                    NowObj.GetLastUsedTime(Configuration.K));
            }
            else
            {
              //计算使用概率
              ri=  (float)NowObj.GetUsedCount()/(TimeNow -
                               NowObj.GetLastUsedTime(NowObj.GetUsedCount())+1);
            }
            //判断
            if(ri<LFU)
            {
              LFU = ri;
              LFUObj = NowObj;
            }
           //转到下一个版本对象
           NowObj = NowObj.NextObjEdition;
         }
         //转到下一个版本对象群
         headObj = headObj.NextObj;
       }
       if(LFUObj!=null)
       {
         //theCache.Print();
         theCache.DeleteObj(LFUObj);

         //同步删除文件

       }
       //theCache.Print();
       return true;
     }
     else
     {
       //如果没有超过门限,则不执行替换算法,返回false
       return false;
     }
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -