示例#1
0
        public static TItem Set <TItem>(this IMemoryCache cache, object key, TItem value, MemoryCacheEntryOptions options)
        {
            using ICacheEntry entry = cache.CreateEntry(key);
            if (options != null)
            {
                entry.SetOptions(options);
            }

            entry.Value = value;

            return(value);
        }
示例#2
0
        /// <summary>
        /// Applies the values of an existing <see cref="MemoryCacheEntryOptions{TKey}"/> to the entry.
        /// </summary>
        /// <param name="entry">The <see cref="ICacheEntry{TKey}"/>.</param>
        /// <param name="options">Set the values of these options on the <paramref name="entry"/>.</param>
        /// <returns>The <see cref="ICacheEntry{TKey}"/> for chaining.</returns>
        public static ICacheEntry <TKey> SetOptions <TKey>(this ICacheEntry <TKey> entry, MemoryCacheEntryOptions <TKey> options)
            where TKey : notnull
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            entry.AbsoluteExpiration = options.AbsoluteExpiration;
            entry.AbsoluteExpirationRelativeToNow = options.AbsoluteExpirationRelativeToNow;
            entry.SlidingExpiration = options.SlidingExpiration;
            entry.Priority          = options.Priority;
            entry.Size = options.Size;

            foreach (var expirationToken in options.ExpirationTokens)
            {
                entry.AddExpirationToken(expirationToken);
            }

            foreach (var postEvictionCallback in options.PostEvictionCallbacks)
            {
                entry.RegisterPostEvictionCallback(postEvictionCallback.EvictionCallback, postEvictionCallback.State);
            }

            return(entry);
        }
        public static TItem Set <TKey, TItem>(this IMemoryCache <TKey> cache, TKey key, TItem value, MemoryCacheEntryOptions <TKey> options)
            where TKey : notnull
        {
            using ICacheEntry <TKey> entry = cache.CreateEntry(key);
            if (options != null)
            {
                entry.SetOptions(options);
            }

            entry.Value = value;

            return(value);
        }