Ejemplo n.º 1
0
        private CachedPage AddPageToCache(long pageIndex)
        {
            lock (locker)
            {
                //Make sure cache page limit is not exceeded
                long maxCacheCount = CachedPageCapacity - 1;//-1 so we can insert the new page
                long countToEvict  = cachedPages.Count - maxCacheCount;
                if (countToEvict > 0)
                {
                    List <long> indicesToEvict = cachedPages.OrderBy(x => x.Value.RecentUseCounter).Select(y => y.Key).Take((int)countToEvict).ToList();
                    foreach (var indexToEvict in indicesToEvict)
                    {
                        EvictPageFromCache(indexToEvict);
                    }
                }

                CachedPage page = new CachedPage(this, pageIndex);
                cachedPages.Add(pageIndex, page);
                return(page);
            }
        }
Ejemplo n.º 2
0
        private bool TryGetCachedPage(long pageIndex, out CachedPage cachedPage)
        {
            lock (locker)
            {
                if (CachedPageCapacity == 0)
                {
                    cachedPage = null;
                    return(false);
                }

                if (!cachedPages.TryGetValue(pageIndex, out cachedPage))
                {
                    //The page is not yet in cache

                    if (PageStorage.IsPageAllocated(pageIndex))
                    {
                        try
                        {
                            cachedPage = AddPageToCache(pageIndex);
                            return(true);
                        }
                        catch (OutOfMemoryException)
                        {
                            cachedPage = null;
                            return(false);
                        }
                    }
                    else
                    {
                        //The page is not allocated, cannot cache it
                        cachedPage = null;
                        return(false);
                    }
                }
                else
                {
                    return(true);
                }
            }
        }