Example #1
0
        /// <summary>
        /// The given callback will be fired after the cache entry is evicted from the cache.
        /// </summary>
        /// <param name="options">The <see cref="MemoryCacheEntryOptions"/>.</param>
        /// <param name="callback">The callback to register for calling after an entry is evicted.</param>
        /// <returns>The <see cref="MemoryCacheEntryOptions"/> so that additional calls can be chained.</returns>
        public static MemoryCacheEntryOptions RegisterPostEvictionCallback(
            this MemoryCacheEntryOptions options,
            PostEvictionDelegate callback)
        {
            ThrowHelper.ThrowIfNull(callback);

            return(options.RegisterPostEvictionCallback(callback, state: null));
        }
Example #2
0
        /// <summary>
        /// The given callback will be fired after the cache entry is evicted from the cache.
        /// </summary>
        /// <param name="options"></param>
        /// <param name="callback"></param>
        /// <param name="state"></param>
        public static MemoryCacheEntryOptions RegisterPostEvictionCallback(
            this MemoryCacheEntryOptions options,
            PostEvictionDelegate callback)
        {
            if (callback == null)
            {
                throw new ArgumentNullException(nameof(callback));
            }

            return(options.RegisterPostEvictionCallback(callback, state: null));
        }
Example #3
0
        public void Set <T>(string key, T cachedValue, MemoryCacheEntryOptions options = null) where T : class
        {
            var entryOptions = new ms.MemoryCacheEntryOptions();

            if (options != null)
            {
                entryOptions.SlidingExpiration = options.SlidingExpiration;
                entryOptions.RegisterPostEvictionCallback((entryKey, value, reason, substate) => options.Callback?.Invoke(entryKey.ToString(), value, reason, substate));
            }

            _memoryCache.Set(key, cachedValue, entryOptions);
        }