Пример #1
0
        object _onRemovedTargets;                           /* targets of OnRemove notification */

        /*
         * ctor.
         */

        internal CacheEntry(
            String key,
            Object value,
            CacheDependency dependency,
            CacheItemRemovedCallback onRemovedHandler,
            DateTime utcAbsoluteExpiration,
            TimeSpan slidingExpiration,
            CacheItemPriority priority,
            bool isPublic) :

            base(key, isPublic)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            if (slidingExpiration < TimeSpan.Zero || OneYear < slidingExpiration)
            {
                throw new ArgumentOutOfRangeException("slidingExpiration");
            }

            if (utcAbsoluteExpiration != Cache.NoAbsoluteExpiration && slidingExpiration != Cache.NoSlidingExpiration)
            {
                throw new ArgumentException(SR.GetString(SR.Invalid_expiration_combination));
            }

            if (priority < CacheItemPriorityMin || CacheItemPriorityMax < priority)
            {
                throw new ArgumentOutOfRangeException("priority");
            }

            _value            = value;
            _dependency       = dependency;
            _onRemovedTargets = onRemovedHandler;

            _utcCreated        = DateTime.UtcNow;
            _slidingExpiration = slidingExpiration;
            if (_slidingExpiration > TimeSpan.Zero)
            {
                _utcExpires = _utcCreated + _slidingExpiration;
            }
            else
            {
                _utcExpires = utcAbsoluteExpiration;
            }

            _expiresEntryRef = ExpiresEntryRef.INVALID;
            _expiresBucket   = 0xff;

            _usageEntryRef = UsageEntryRef.INVALID;
            if (priority == CacheItemPriority.NotRemovable)
            {
                _usageBucket = 0xff;
            }
            else
            {
                _usageBucket = (byte)(priority - 1);
            }
        }
 internal void AddCacheEntry(CacheEntry cacheEntry)
 {
     lock (this)
     {
         if (((byte)(cacheEntry.State & (CacheEntry.EntryState.AddedToCache | CacheEntry.EntryState.AddingToCache))) != 0)
         {
             ExpiresEntryRef expiresEntryRef = cacheEntry.ExpiresEntryRef;
             if ((cacheEntry.ExpiresBucket == 0xff) && expiresEntryRef.IsInvalid)
             {
                 if (this._freeEntryList._head == -1)
                 {
                     this.Expand();
                 }
                 ExpiresEntryRef freeExpiresEntry = this.GetFreeExpiresEntry();
                 cacheEntry.ExpiresBucket   = this._bucket;
                 cacheEntry.ExpiresEntryRef = freeExpiresEntry;
                 ExpiresEntry[] entryArray = this._pages[freeExpiresEntry.PageIndex]._entries;
                 int            index      = freeExpiresEntry.Index;
                 entryArray[index]._cacheEntry = cacheEntry;
                 entryArray[index]._utcExpires = cacheEntry.UtcExpires;
                 this.AddCount(cacheEntry.UtcExpires);
                 this._cEntriesInUse++;
                 if (((byte)(cacheEntry.State & (CacheEntry.EntryState.AddedToCache | CacheEntry.EntryState.AddingToCache))) == 0)
                 {
                     this.RemoveCacheEntryNoLock(cacheEntry);
                 }
             }
         }
     }
 }
        private void Reduce()
        {
            int num4;

            if ((this._cEntriesInUse >= this._minEntriesInUse) || this._blockReduce)
            {
                return;
            }
            int num   = 0x3f;
            int num2  = this._freeEntryList._tail;
            int index = this._freeEntryList._head;

Label_0032:
            num4 = this._pages[index]._pageNext;
            if (this._pages[index]._entries[0]._cFree > num)
            {
                this.MoveToListTail(index, ref this._freeEntryList);
            }
            else
            {
                this.MoveToListHead(index, ref this._freeEntryList);
            }
            if (index != num2)
            {
                index = num4;
                goto Label_0032;
            }
            while (this._freeEntryList._tail != -1)
            {
                ExpiresEntry[] entryArray = this._pages[this._freeEntryList._tail]._entries;
                int            num5       = ((this._cPagesInUse * 0x7f) - entryArray[0]._cFree) - this._cEntriesInUse;
                if (num5 < (0x7f - entryArray[0]._cFree))
                {
                    return;
                }
                for (int i = 1; i < entryArray.Length; i++)
                {
                    if (entryArray[i]._cacheEntry != null)
                    {
                        ExpiresEntryRef freeExpiresEntry = this.GetFreeExpiresEntry();
                        entryArray[i]._cacheEntry.ExpiresEntryRef = freeExpiresEntry;
                        this._pages[freeExpiresEntry.PageIndex]._entries[freeExpiresEntry.Index] = entryArray[i];
                        entryArray[0]._cFree++;
                    }
                }
                this.RemovePage(this._freeEntryList._tail);
            }
            return;
        }
 internal void UtcUpdateCacheEntry(CacheEntry cacheEntry, DateTime utcExpires)
 {
     lock (this)
     {
         ExpiresEntryRef expiresEntryRef = cacheEntry.ExpiresEntryRef;
         if ((cacheEntry.ExpiresBucket == this._bucket) && !expiresEntryRef.IsInvalid)
         {
             ExpiresEntry[] entryArray = this._pages[expiresEntryRef.PageIndex]._entries;
             int            index      = expiresEntryRef.Index;
             this.RemoveCount(entryArray[index]._utcExpires);
             this.AddCount(utcExpires);
             entryArray[index]._utcExpires = utcExpires;
             cacheEntry.UtcExpires         = utcExpires;
         }
     }
 }
 private void AddExpiresEntryToFreeList(ExpiresEntryRef entryRef)
 {
     ExpiresEntry[] entryArray = this._pages[entryRef.PageIndex]._entries;
     int index = entryRef.Index;
     entryArray[index]._cFree = 0;
     entryArray[index]._next = entryArray[0]._next;
     entryArray[0]._next = entryRef;
     this._cEntriesInUse--;
     int pageIndex = entryRef.PageIndex;
     entryArray[0]._cFree++;
     if (entryArray[0]._cFree == 1)
     {
         this.AddToListHead(pageIndex, ref this._freeEntryList);
     }
     else if (entryArray[0]._cFree == 0x7f)
     {
         this.RemovePage(pageIndex);
     }
 }
        private void RemoveCacheEntryNoLock(CacheEntry cacheEntry)
        {
            ExpiresEntryRef expiresEntryRef = cacheEntry.ExpiresEntryRef;

            if ((cacheEntry.ExpiresBucket == this._bucket) && !expiresEntryRef.IsInvalid)
            {
                ExpiresEntry[] entryArray = this._pages[expiresEntryRef.PageIndex]._entries;
                int            index      = expiresEntryRef.Index;
                this.RemoveCount(entryArray[index]._utcExpires);
                cacheEntry.ExpiresBucket      = 0xff;
                cacheEntry.ExpiresEntryRef    = ExpiresEntryRef.INVALID;
                entryArray[index]._cacheEntry = null;
                this.AddExpiresEntryToFreeList(expiresEntryRef);
                if (this._cEntriesInUse == 0)
                {
                    this.ResetCounts(DateTime.UtcNow);
                }
                this.Reduce();
            }
        }
        private void AddExpiresEntryToFreeList(ExpiresEntryRef entryRef)
        {
            ExpiresEntry[] entryArray = this._pages[entryRef.PageIndex]._entries;
            int            index      = entryRef.Index;

            entryArray[index]._cFree = 0;
            entryArray[index]._next  = entryArray[0]._next;
            entryArray[0]._next      = entryRef;
            this._cEntriesInUse--;
            int pageIndex = entryRef.PageIndex;

            entryArray[0]._cFree++;
            if (entryArray[0]._cFree == 1)
            {
                this.AddToListHead(pageIndex, ref this._freeEntryList);
            }
            else if (entryArray[0]._cFree == 0x7f)
            {
                this.RemovePage(pageIndex);
            }
        }
Пример #8
0
        object                      _onRemovedTargets;      /* targets of OnRemove notification */

        /*
         * ctor.
         */

        internal CacheEntry(
                   String                   key, 
                   Object                   value, 
                   CacheDependency          dependency,
                   CacheItemRemovedCallback onRemovedHandler,
                   DateTime                 utcAbsoluteExpiration,              
                   TimeSpan                 slidingExpiration,      
                   CacheItemPriority        priority,
                   bool                     isPublic) : 

                base(key, isPublic) {

            if (value == null) {
                throw new ArgumentNullException("value");
            }

            if (slidingExpiration < TimeSpan.Zero || OneYear < slidingExpiration) {
                throw new ArgumentOutOfRangeException("slidingExpiration");
            }

            if (utcAbsoluteExpiration != Cache.NoAbsoluteExpiration && slidingExpiration != Cache.NoSlidingExpiration) {
                throw new ArgumentException(SR.GetString(SR.Invalid_expiration_combination));
            }

            if (priority < CacheItemPriorityMin || CacheItemPriorityMax < priority) {
                throw new ArgumentOutOfRangeException("priority");
            }

            _value = value;
            _dependency = dependency;
            _onRemovedTargets = onRemovedHandler;

            _utcCreated = DateTime.UtcNow;
            _slidingExpiration = slidingExpiration;
            if (_slidingExpiration > TimeSpan.Zero) {
                _utcExpires = _utcCreated + _slidingExpiration;
            }
            else {
                _utcExpires = utcAbsoluteExpiration;
            } 

            _expiresEntryRef = ExpiresEntryRef.INVALID;
            _expiresBucket = 0xff;

            _usageEntryRef = UsageEntryRef.INVALID;
            if (priority == CacheItemPriority.NotRemovable) {
                _usageBucket = 0xff;
            }
            else {
                _usageBucket = (byte) (priority - 1);
            }
        }
        internal int FlushExpiredItems(DateTime utcNow, bool useInsertBlock)
        {
            ExpiresEntry[]  entryArray;
            int             index;
            CacheEntry      entry;
            ExpiresEntryRef ref3;
            ExpiresEntryRef ref4;

            if ((this._cEntriesInUse == 0) || (this.GetExpiresCount(utcNow) == 0))
            {
                return(0);
            }
            ExpiresEntryRef iNVALID = ExpiresEntryRef.INVALID;
            int             num2    = 0;

            try
            {
                if (useInsertBlock)
                {
                    this._cacheExpires.CacheSingle.BlockInsertIfNeeded();
                }
                lock (this)
                {
                    if ((this._cEntriesInUse == 0) || (this.GetExpiresCount(utcNow) == 0))
                    {
                        return(0);
                    }
                    this.ResetCounts(utcNow);
                    int num3 = this._cPagesInUse;
                    for (int i = 0; i < this._pages.Length; i++)
                    {
                        entryArray = this._pages[i]._entries;
                        if (entryArray != null)
                        {
                            int num5 = 0x7f - entryArray[0]._cFree;
                            for (int j = 1; j < entryArray.Length; j++)
                            {
                                entry = entryArray[j]._cacheEntry;
                                if (entry != null)
                                {
                                    if (entryArray[j]._utcExpires > utcNow)
                                    {
                                        this.AddCount(entryArray[j]._utcExpires);
                                    }
                                    else
                                    {
                                        entry.ExpiresBucket   = 0xff;
                                        entry.ExpiresEntryRef = ExpiresEntryRef.INVALID;
                                        entryArray[j]._cFree  = 1;
                                        entryArray[j]._next   = iNVALID;
                                        iNVALID = new ExpiresEntryRef(i, j);
                                        num2++;
                                        this._cEntriesInFlush++;
                                    }
                                    num5--;
                                    if (num5 == 0)
                                    {
                                        break;
                                    }
                                }
                            }
                            num3--;
                            if (num3 == 0)
                            {
                                break;
                            }
                        }
                    }
                    if (num2 == 0)
                    {
                        return(0);
                    }
                    this._blockReduce = true;
                }
            }
            finally
            {
                if (useInsertBlock)
                {
                    this._cacheExpires.CacheSingle.UnblockInsert();
                }
            }
            CacheSingle cacheSingle = this._cacheExpires.CacheSingle;

            for (ref3 = iNVALID; !ref3.IsInvalid; ref3 = ref4)
            {
                entryArray = this._pages[ref3.PageIndex]._entries;
                index      = ref3.Index;
                ref4       = entryArray[index]._next;
                entry      = entryArray[index]._cacheEntry;
                entryArray[index]._cacheEntry = null;
                cacheSingle.Remove(entry, CacheItemRemovedReason.Expired);
            }
            try
            {
                if (useInsertBlock)
                {
                    this._cacheExpires.CacheSingle.BlockInsertIfNeeded();
                }
                lock (this)
                {
                    for (ref3 = iNVALID; !ref3.IsInvalid; ref3 = ref4)
                    {
                        entryArray = this._pages[ref3.PageIndex]._entries;
                        index      = ref3.Index;
                        ref4       = entryArray[index]._next;
                        this._cEntriesInFlush--;
                        this.AddExpiresEntryToFreeList(ref3);
                    }
                    this._blockReduce = false;
                    this.Reduce();
                }
            }
            finally
            {
                if (useInsertBlock)
                {
                    this._cacheExpires.CacheSingle.UnblockInsert();
                }
            }
            return(num2);
        }
 static ExpiresEntryRef()
 {
     INVALID = new ExpiresEntryRef(0, 0);
 }
 static ExpiresEntryRef()
 {
     INVALID = new ExpiresEntryRef(0, 0);
 }
 internal int FlushExpiredItems(DateTime utcNow, bool useInsertBlock)
 {
     ExpiresEntry[] entryArray;
     int index;
     CacheEntry entry;
     ExpiresEntryRef ref3;
     ExpiresEntryRef ref4;
     if ((this._cEntriesInUse == 0) || (this.GetExpiresCount(utcNow) == 0))
     {
         return 0;
     }
     ExpiresEntryRef iNVALID = ExpiresEntryRef.INVALID;
     int num2 = 0;
     try
     {
         if (useInsertBlock)
         {
             this._cacheExpires.CacheSingle.BlockInsertIfNeeded();
         }
         lock (this)
         {
             if ((this._cEntriesInUse == 0) || (this.GetExpiresCount(utcNow) == 0))
             {
                 return 0;
             }
             this.ResetCounts(utcNow);
             int num3 = this._cPagesInUse;
             for (int i = 0; i < this._pages.Length; i++)
             {
                 entryArray = this._pages[i]._entries;
                 if (entryArray != null)
                 {
                     int num5 = 0x7f - entryArray[0]._cFree;
                     for (int j = 1; j < entryArray.Length; j++)
                     {
                         entry = entryArray[j]._cacheEntry;
                         if (entry != null)
                         {
                             if (entryArray[j]._utcExpires > utcNow)
                             {
                                 this.AddCount(entryArray[j]._utcExpires);
                             }
                             else
                             {
                                 entry.ExpiresBucket = 0xff;
                                 entry.ExpiresEntryRef = ExpiresEntryRef.INVALID;
                                 entryArray[j]._cFree = 1;
                                 entryArray[j]._next = iNVALID;
                                 iNVALID = new ExpiresEntryRef(i, j);
                                 num2++;
                                 this._cEntriesInFlush++;
                             }
                             num5--;
                             if (num5 == 0)
                             {
                                 break;
                             }
                         }
                     }
                     num3--;
                     if (num3 == 0)
                     {
                         break;
                     }
                 }
             }
             if (num2 == 0)
             {
                 return 0;
             }
             this._blockReduce = true;
         }
     }
     finally
     {
         if (useInsertBlock)
         {
             this._cacheExpires.CacheSingle.UnblockInsert();
         }
     }
     CacheSingle cacheSingle = this._cacheExpires.CacheSingle;
     for (ref3 = iNVALID; !ref3.IsInvalid; ref3 = ref4)
     {
         entryArray = this._pages[ref3.PageIndex]._entries;
         index = ref3.Index;
         ref4 = entryArray[index]._next;
         entry = entryArray[index]._cacheEntry;
         entryArray[index]._cacheEntry = null;
         cacheSingle.Remove(entry, CacheItemRemovedReason.Expired);
     }
     try
     {
         if (useInsertBlock)
         {
             this._cacheExpires.CacheSingle.BlockInsertIfNeeded();
         }
         lock (this)
         {
             for (ref3 = iNVALID; !ref3.IsInvalid; ref3 = ref4)
             {
                 entryArray = this._pages[ref3.PageIndex]._entries;
                 index = ref3.Index;
                 ref4 = entryArray[index]._next;
                 this._cEntriesInFlush--;
                 this.AddExpiresEntryToFreeList(ref3);
             }
             this._blockReduce = false;
             this.Reduce();
         }
     }
     finally
     {
         if (useInsertBlock)
         {
             this._cacheExpires.CacheSingle.UnblockInsert();
         }
     }
     return num2;
 }