Esempio n. 1
0
 // Stays in the cache as long as possible
 public static Task Set(string key, object value)
 {
     return(Task.Run(async() => {
         await SleepAsync(10);
         var options = new Microsoft.Framework.Caching.Memory.MemoryCacheEntryOptions().SetPriority(Microsoft.Framework.Caching.Memory.CacheItemPriority.NeverRemove);
         Set(key, value, options);
     }));
 }
Esempio n. 2
0
 // Automatically remove if not accessed in the given time
 //SetSlidingExpiration(TimeSpan.FromMinutes(5)));
 public static Task SetAndValidFor(string key, object value, System.TimeSpan validFor)
 {
     return(Task.Run(async() => {
         await SleepAsync(10);
         var options = new Microsoft.Framework.Caching.Memory.MemoryCacheEntryOptions();
         options.SetSlidingExpiration(validFor);
         Set(key, value, options);
     }));
 }
Esempio n. 3
0
 public static Task SetAndExpiresMonths(string key, object value, int months = 1)
 {
     return(Task.Run(async() => {
         await SleepAsync(10);
         var options = new Microsoft.Framework.Caching.Memory.MemoryCacheEntryOptions();
         options.SetAbsoluteExpiration(DateTimeOffset.UtcNow.AddMonths(months));
         Set(key, value, options);
     }));
 }
Esempio n. 4
0
        public object Set([NotNull] string key, object value, MemoryCacheEntryOptions cacheEntryOptions)
        {
            CheckDisposed();
            CacheEntry priorEntry = null;
            var        utcNow     = _clock.UtcNow;

            DateTimeOffset?absoluteExpiration = null;

            if (cacheEntryOptions.AbsoluteExpirationRelativeToNow.HasValue)
            {
                absoluteExpiration = utcNow + cacheEntryOptions.AbsoluteExpirationRelativeToNow;
            }
            else if (cacheEntryOptions.AbsoluteExpiration.HasValue)
            {
                if (cacheEntryOptions.AbsoluteExpiration <= utcNow)
                {
                    throw new ArgumentOutOfRangeException(
                              nameof(MemoryCacheEntryOptions.AbsoluteExpiration),
                              cacheEntryOptions.AbsoluteExpiration.Value,
                              "The absolute expiration value must be in the future.");
                }

                absoluteExpiration = cacheEntryOptions.AbsoluteExpiration;
            }

            var entry = new CacheEntry(
                key,
                value,
                utcNow,
                absoluteExpiration,
                cacheEntryOptions,
                _entryExpirationNotification);

            var link = EntryLinkHelpers.ContextLink;

            if (link != null)
            {
                // Copy triggers and AbsoluteExpiration to the link.
                // We do this regardless of it gets cached because the triggers are associated with the value we'll return.
                if (entry.Options.Triggers != null)
                {
                    link.AddExpirationTriggers(entry.Options.Triggers);
                }
                if (absoluteExpiration.HasValue)
                {
                    link.SetAbsoluteExpiration(absoluteExpiration.Value);
                }
            }

            bool added = false;

            _entryLock.EnterWriteLock();
            try
            {
                if (_entries.TryGetValue(key, out priorEntry))
                {
                    _entries.Remove(key);
                    priorEntry.SetExpired(EvictionReason.Replaced);
                }

                if (!entry.CheckExpired(utcNow))
                {
                    _entries[key] = entry;
                    entry.AttachTriggers();
                    added = true;
                }
            }
            finally
            {
                _entryLock.ExitWriteLock();
            }
            if (priorEntry != null)
            {
                priorEntry.InvokeEvictionCallbacks();
            }
            if (!added)
            {
                entry.InvokeEvictionCallbacks();
            }

            StartScanForExpiredItems();

            return(value);
        }
Esempio n. 5
0
 private static void Set(string key, object value, Microsoft.Framework.Caching.Memory.MemoryCacheEntryOptions options) => _cache.Set(key, value, options);