Пример #1
0
 ///////////////////////////////////////////////////////////////////////
 /*private void Insert(CachePage page, string key)
 {
     bool lastpos;
     if (algorithm == CacheReplacement.LFU)
     {
         IEnumerator cacheEnumerator = dataCache.GetEnumerator();
         CachePage iterPage;
         lastpos = true;
         while (cacheEnumerator.MoveNext())
         {
             iterPage = (CachePage)cacheEnumerator.Current;
             if (iterPage.hitsNumber < page.hitsNumber)
             {
                 int index = dataCache.IndexOf(iterPage);
                 dataCache.Insert(index, page);
                 dataIndex[key] = index;
                 lastpos = false;
                 break;
             }
         }
         if (lastpos)
         {
             dataCache.Insert(this.Count, page);
             dataIndex[key] = this.Count;
         }
     }
     else
     {
         dataCache.Insert(0, page);
         dataIndex[key] = 0;
     }
     this.Count++;
 }*/
 ///////////////////////////////////////////////////////////////////////
 private void Insert(CachePage page, string key)
 {
     // Si la cache esta llena. Tenemos que reemplazar una pagina
     // Se deberia avisar del FALLO DE PAGINA
     if (this.Count >= this.Capacity)
     {
         CachePage pageToReplace = (CachePage) dataCache[0];
         foreach (CachePage currentPage in dataCache)
         {
             // Buscamos la menos usada
             if (algorithm == CacheReplacement.LFU)
             {
                 if (pageToReplace.hitsNumber > currentPage.hitsNumber)
                     pageToReplace = currentPage;
             }
             // Buscamos la mas vieja
             else if (algorithm == CacheReplacement.LRU)
             {
                 if (pageToReplace.lastHit > currentPage.lastHit)
                     pageToReplace = currentPage;
             }
         }
         // La reemplazamos
         int index = dataCache.IndexOf(pageToReplace);
         dataCache.Insert(index, page);
         dataIndex[key] = index;
         // Eliminamos el indice
         dataIndex.Remove(pageToReplace.key);
     }
     else
     {
         dataCache.Insert(this.Count, page);
         dataIndex[key] = this.Count++;
     }
 }
Пример #2
0
 //////////////////////////////////////////////////////////////////////
 private void Reallocate(CachePage page, string key, int index)
 {
     if (algorithm == CacheReplacement.LFU)
     {
         bool firstpos = true;
         CachePage tmp;
         while(index > 0)
         {
             tmp = (CachePage)dataCache[index];
             if (tmp.hitsNumber >= page.hitsNumber)
             {
                 dataCache.Insert(index+1, page);
                 dataIndex[key] = index +1;
                 firstpos = false;
                 break;
             }
             else
                 index --;
         }
         if (firstpos)
         {
             dataCache.Insert(0, page);
             dataIndex[key] = 0;
         }
     }
     else if (algorithm == CacheReplacement.LRU)
     {
         dataCache.Insert(0, page);
         dataIndex[key] = 0;
     }
 }
Пример #3
0
        //////////////////////////////////////////////////////////////////////
        ///<summary>
        ///Set data in cache. If data already exist in cache, only refresh hit date and hits number
        ///and reallocate. If data doesnt exist, and cache is not full insert new data else if 
        ///cache is full overwrite cache data, replacing  with apropiate algorithm
        ///</summary>
        /*public void SetCacheData(object data, string key)
        {
            CachePage page;

            if (dataIndex.ContainsKey(key))			// Update cache hits
            {
                int index = (int) dataIndex[key];
                page = (CachePage) dataCache[index];
                dataCache.Remove(index);
                dataIndex.Remove(key);
                page.lastHit = DateTime.Now;
                page.hitsNumber ++;
                page.index = -1;
                page.data = data;
                Reallocate(page, key, index);
            }
            else												// Add to cache
            {
                if (this.Count <= this.Capacity)
                {
                    page = new CachePage();
                    page.lastHit = DateTime.Now;
                    page.hitsNumber = 1;
                    page.key = key;
                    page.index = -1;
                    page.data = data;
                    Insert(page, key);
                    Console.WriteLine("a-{0}..{1}",this.Count,this.Capacity);
                }
                else 											// Cache is full, need to replace page
                {
                    if (algorithm == CacheReplacement.LFU)
                    {

                    }
                    else if (algorithm == CacheReplacement.LRU)
                    {

                    }
                }
            }
        }*/
        ////////////////////////////////////////////////////////////////////////
        public bool SetCacheData(object data, string key)
        {
            CachePage page;

            if (dataIndex.ContainsKey(key))			// Update cache hits
            {
                int index = (int) dataIndex[key];
                page = (CachePage) dataCache[index];
                page.lastHit = DateTime.Now;
                page.hitsNumber ++;
                page.data = data;
                return true;
            }
            else									// Add to cache
            {
                page = new CachePage();
                page.lastHit = DateTime.Now;
                page.hitsNumber = 1;
                page.key = key;
                page.index = -1;
                page.data = data;
                Insert(page, key);
                return false;
            }
        }