Beispiel #1
0
 private void RemoveEntry(PersistentCacheEntry entry)
 {
     _EntryLock.EnterWriteLock();
     try
     {
         // Only remove it if someone hasn't modified it since our lookup
         PersistentCacheEntry currentEntry;
         if (_InMemoryEntries.TryGetValue(entry.Key, out currentEntry) &&
             object.ReferenceEquals(currentEntry, entry))
         {
             RemoveEntryFromMemoryAndStore(entry);
             if (_Options.IsPersistent)
             {
                 entry.PropertyChanged -= Entry_PropertyChanged;
             }
         }
     }
     finally
     {
         _EntryLock.ExitWriteLock();
     }
     entry.InvokeEvictionCallbacks();
 }
Beispiel #2
0
        private void SetEntry(PersistentCacheEntry entry)
        {
            var utcNow = _Options.Clock.UtcNow;

            DateTimeOffset?absoluteExpiration = null;

            if (entry._AbsoluteExpirationRelativeToNow.HasValue)
            {
                absoluteExpiration = utcNow + entry._AbsoluteExpirationRelativeToNow;
            }
            else if (entry._AbsoluteExpiration.HasValue)
            {
                absoluteExpiration = entry._AbsoluteExpiration;
            }

            // Applying the option's absolute expiration only if it's not already smaller.
            // This can be the case if a dependent cache entry has a smaller value, and
            // it was set by cascading it to its parent.
            if (absoluteExpiration.HasValue)
            {
                if (!entry._AbsoluteExpiration.HasValue || absoluteExpiration.Value < entry._AbsoluteExpiration.Value)
                {
                    entry._AbsoluteExpiration = absoluteExpiration;
                }
            }

            // Initialize the last access timestamp at the time the entry is added
            entry.LastAccessed = utcNow;

            var added = false;
            PersistentCacheEntry priorEntry;

            _EntryLock.EnterWriteLock();
            try
            {
                if (_InMemoryEntries.TryGetValue(entry.Key, out priorEntry))
                {
                    RemoveEntryFromMemoryAndStore(priorEntry);
                    priorEntry.SetExpired(EvictionReason.Replaced);
                }

                if (!entry.CheckExpired(utcNow))
                {
                    AddEntryToMemoryAndStore(entry);
                    entry.AttachTokens();
                    if (_Options.IsPersistent)
                    {
                        entry.PropertyChanged += Entry_PropertyChanged;
                    }
                    added = true;
                }
            }
            finally
            {
                _EntryLock.ExitWriteLock();
            }

            if (priorEntry != null)
            {
                priorEntry.InvokeEvictionCallbacks();
            }

            if (!added)
            {
                entry.InvokeEvictionCallbacks();
            }
            StartScanForExpiredItems();
        }