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

📄 memcache.java

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

package MemCachePak;


public class MemCache implements MemCacheInterface
{
   private ObjChart NowChart;
   private int CacheSize = 0;
   private static MemCache theCacheInstance = null;
   private static boolean lock = true;
   private WebObj NowObj;
   private WebObj FirstObj;
   private WebObj LastObj;
   public ObjChart thefirstchart;

   /**
   @roseuid 417CDCDB023A
   */
   public boolean IsEmpty()
   {
     if(FirstObj==null)
     {
       return true;
     }
     else return false;
   }

   /**
   @roseuid 417CDCDD00C3
   */
   public boolean DeleteObj(WebObj CachedObj)
   {
     while(!lock)
     {
     }
     if(lock&&CacheSize>=Configuration.MAX_SIZE)
     {
       Close();
       NowObj = SearchObj(CachedObj.GetSourceFileName());
       if(NowObj!=null)//当cache中有此对象的原对象链表时
       {
         if((NowObj.GetCachedFileName()).equals(CachedObj.GetCachedFileName()))//当删除的对象是链表head时
         {
           if(SearchlastObj(CachedObj.GetSourceFileName())==null)//当删除的对象是FirstOBj时
           {
             if(NowObj.IsLastEdition())//当链表中只有一个对象时
             {
               FirstObj = NowObj.NextObj;
               SubCacheSize(CachedObj.GetFileSize());
               Open();
               return true;
             }
             else
             {
               NowObj.NextObjEdition.NextObj = NowObj.NextObj;
               FirstObj = NowObj.NextObjEdition;
               FirstObj.IsLinkHead = true;
               SubCacheSize(CachedObj.GetFileSize());
               Open();
               return true;
             }
           }
           else//当删除的对象是head但不是first时
           {
             WebObj last = SearchlastObj(CachedObj.GetSourceFileName());
             if(NowObj.IsLastEdition())//当链表中只有一个对象时
             {
               last.NextObj = NowObj.NextObj;
               if(last.NextObj == null)
               {
                 LastObj = last;
               }
               SubCacheSize(CachedObj.GetFileSize());
               Open();
               return true;
             }
             else
             {
               NowObj.NextObjEdition.NextObj = NowObj.NextObj;
               last.NextObj = NowObj.NextObjEdition;
               last.NextObj.IsLinkHead = true;
               if(NowObj.NextObj==null)
               {
                 LastObj = last.NextObj;
               }
               SubCacheSize(CachedObj.GetFileSize());
               Open();
               return true;
             }
           }
         }
         else//当要删除的不是head时
         {
           while(NowObj.NextObjEdition!=null)
           {
             if((NowObj.NextObjEdition.GetCachedFileName()).equals(CachedObj.GetCachedFileName()))
             {
               NowObj.NextObjEdition = NowObj.NextObjEdition.NextObjEdition;
               SubCacheSize(CachedObj.GetFileSize());
               Open();
               return true;
             }
             NowObj = NowObj.NextObjEdition;
           }
         }

       }
       else
       {
         Open();
         return false;//当在纵向链表中没有找到时返回false
        }
       Open();
       return false;//当在横向链表中没有找到时返回false
     }
     return false;
   }

   /**
   @roseuid 417DA6AE0118
   */
   public MemCache()
   {
     NowObj = null;
     FirstObj = null;
     LastObj = null;
     thefirstchart = null;
     NowChart = null;
   }

   /**
   @roseuid 417DD54A0379
   */
   public boolean InsertNewObj(WebObj NewWebObj)
   {
     while(!lock)
     {
     }
     if(lock)
     {
       Close();
       NewWebObj.AddUsedCount();
       NewWebObj.SetLastUsedTime();
       if(IsEmpty())//缓存为空
       {
         NewWebObj.IsLinkHead = true;
         FirstObj = NewWebObj;
         LastObj = NewWebObj;
         ChartFirstVex NewVex = new ChartFirstVex();
         NewVex.SetData(NewWebObj.GetCachedFileName());
         //插入新的顶点
         InsertNewVex(NewVex,NewWebObj.GetSourceFileName());
         //在转换图中插入新的有向边
         //InsertNewVector(NewWebObj.GetCacheSavedCost(),NewWebObj.GetSourceFileName(),
         // NewWebObj.GetCachedFileName(),NewWebObj.GetSourceFileName());
       }
       else//缓存不为空
       {
         NowObj = SearchObj(NewWebObj.GetSourceFileName());
         if(NowObj!=null)//缓存中已经存在此对象的其它版本
         {
           while(NowObj.NextObjEdition!=null)
           {
             NowObj=NowObj.NextObjEdition;
           }
           NowObj.NextObjEdition = NewWebObj;
           ChartFirstVex NewVex = new ChartFirstVex();
           NewVex.SetData(NewWebObj.GetCachedFileName());
           //插入新的顶点
           InsertNewVex(NewVex,NewWebObj.GetSourceFileName());
           //在转换图中插入新的有向边
           // InsertNewVector(NewWebObj.GetCacheSavedCost(),NewWebObj.GetSourceFileName(),
           // NewWebObj.GetCachedFileName(),NewWebObj.GetSourceFileName());
         }
         else//缓存中没有此对象的其它版本
         {
           NewWebObj.IsLinkHead = true;
           LastObj.NextObj = NewWebObj;
           LastObj = LastObj.NextObj;
           ChartFirstVex NewVex = new ChartFirstVex();
           NewVex.SetData(NewWebObj.GetCachedFileName());
           //插入新的顶点
           InsertNewVex(NewVex,NewWebObj.GetSourceFileName());
           //在转换图中插入新的有向边
           // InsertNewVector(NewWebObj.GetCacheSavedCost(),NewWebObj.GetSourceFileName(),
           //   NewWebObj.GetCachedFileName(),NewWebObj.GetSourceFileName());
         }
       }
       AddCacheSize(NewWebObj.GetFileSize());
       Open();
       return true;
     }
     return false;
   }

   /**
   @roseuid 417DD62B0241
   */
   public WebObj SearchObj(String InputObjURL)
   {
     NowObj = FirstObj;
     int result = 0;
     while(NowObj!=null)
     {
       if((NowObj.GetSourceFileName()).equals(InputObjURL))
       {
         result = 1;
         return NowObj;
       }
       else NowObj = NowObj.NextObj;
     }
     return null;
   }

   /**
   @roseuid 417EF9EF00BB
   */
   public WebObj SearchlastObj(String InputObjURL)
   {
     NowObj = FirstObj;
     WebObj lastObj = null;
     int result = 0;
     while(NowObj!=null)
     {
       if((NowObj.GetSourceFileName()).equals(InputObjURL))
       {
         result = 1;
         return lastObj;
       }
       else
         {
           lastObj = NowObj;
           NowObj = NowObj.NextObj;
         }
     }
     return null;
   }

   /**
   @roseuid 417F4D4500EA
   */
   public void Print()
   {
     NowObj = FirstObj;
     String stringout=null;
     WebObj head = FirstObj;
     System.out.println("web对象缓存: 缓存大小="+CacheSize);
     while(head!=null)
     {
       NowObj = head;
       while(NowObj!=null)
       {
         stringout = NowObj.GetCachedFileName();
         System.out.print(stringout+" "+NowObj.GetGPF()+"   ");
         //System.out.print(stringout+" "+NowObj.GetLastUsedTime(1)+"   ");
         NowObj = NowObj.NextObjEdition;
       }
       System.out.println();
       head = head.NextObj;
     }
     System.out.println("****************************************************");
     /*System.out.println("web对象转换图:");
     NowChart = thefirstchart;

     while(NowChart!=null)
     {
       System.out.println();
       System.out.println("*图代表的对象:"+NowChart.GetName()+"  图中的顶点个数:"
                          +NowChart.GetCount());
       ChartFirstVex NowVex = NowChart.theFirstVex;
       while(NowVex!=null)
       {
         ChartEdge NowEdgeIn = NowVex.firstin;
         ChartEdge NowEdgeOut = NowVex.firstout;
         System.out.println("**顶点代表的对象:"+NowVex.GetData());
         System.out.print("以此结点为起点的向量有:");
         while(NowEdgeIn!=null)
         {
           System.out.print(" 向量:<"+NowEdgeIn.Gethead()+","+NowEdgeIn.Gettail()
                            +","+NowEdgeIn.GetData()+">"+" ");
           NowEdgeIn = NowEdgeIn.Rlink1;
         }
         System.out.println();
         System.out.print("以此结点为终点的向量有:");
         while(NowEdgeOut!=null)
         {
           System.out.print(" 向量:<"+NowEdgeOut.Gethead()+","
                            +NowEdgeOut.Gettail()+","
                            +NowEdgeOut.GetData()+">"+" ");
           NowEdgeOut = NowEdgeOut.Rlink2;
         }
         NowVex = NowVex.nextvex;
         System.out.println();
       }
       NowChart = NowChart.NextChart;
     }
     System.out.println("****************************************************");*/
   }

⌨️ 快捷键说明

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