MemoryCache - AddOrGetExisting
A MemoryCache extension method that adds an or get existing.
public static void Main() { var cache2 = new CacheTest(); var cache3 = new CacheTest(); var cache4 = new CacheTest(); System.Runtime.Caching.MemoryCache cache = System.Runtime.Caching.MemoryCache.Default; //C# Extension Method: MemoryCache - AddOrGetExisting int cache1_1 = cache.AddOrGetExisting("cache1", 1); int cache1_2 = cache.AddOrGetExisting("cache1", 2); int cache2_1 = cache.AddOrGetExisting("cache2", i => cache2.Increment()); int cache2_2 = cache.AddOrGetExisting("cache2", i => cache2.Increment()); int cache3_1 = cache.AddOrGetExisting("cache3", i => cache3.Increment(), new CacheItemPolicy()); int cache3_2 = cache.AddOrGetExisting("cache3", i => cache3.Increment(), new CacheItemPolicy()); int cache4_1 = cache.AddOrGetExisting("cache4", i => cache4.Increment(), new DateTimeOffset(new DateTime(2100, 01, 01))); int cache4_2 = cache.AddOrGetExisting("cache4", i => cache4.Increment(), new DateTimeOffset(new DateTime(2100, 01, 01))); Console.WriteLine(cache1_1); Console.WriteLine(cache1_2); Console.WriteLine(cache2_1); Console.WriteLine(cache2_2); Console.WriteLine(cache3_1); Console.WriteLine(cache3_2); Console.WriteLine(cache4_1); Console.WriteLine(cache4_2); } public class CacheTest { public int Cache = 0; public int Increment() { Cache++; return Cache; } }
View Source
using System; #if !NETSTANDARD using System.Runtime.Caching; #endif public static partial class Extensions { #if !NETSTANDARD /// <summary>A MemoryCache extension method that adds an or get existing.</summary> /// <typeparam name="TValue">Type of the value.</typeparam> /// <param name="cache">The cache to act on.</param> /// <param name="key">The key.</param> /// <param name="value">The value.</param> /// <returns>A TValue.</returns> public static TValue AddOrGetExisting<TValue>(this MemoryCache cache, string key, TValue value) { object item = cache.AddOrGetExisting(key, value, new CacheItemPolicy()) ?? value; return (TValue) item; } /// <summary>A MemoryCache extension method that adds an or get existing.</summary> /// <typeparam name="TValue">Type of the value.</typeparam> /// <param name="cache">The cache to act on.</param> /// <param name="key">The key.</param> /// <param name="valueFactory">The value factory.</param> /// <returns>A TValue.</returns> public static TValue AddOrGetExisting<TValue>(this MemoryCache cache, string key, Func<string, TValue> valueFactory) { var lazy = new Lazy<TValue>(() => valueFactory(key)); Lazy<TValue> item = (Lazy<TValue>) cache.AddOrGetExisting(key, lazy, new CacheItemPolicy()) ?? lazy; return item.Value; } /// <summary>A MemoryCache extension method that adds an or get existing.</summary> /// <typeparam name="TValue">Type of the value.</typeparam> /// <param name="cache">The cache to act on.</param> /// <param name="key">The key.</param> /// <param name="valueFactory">The value factory.</param> /// <param name="policy">The policy.</param> /// <param name="regionName">(Optional) name of the region.</param> /// <returns>A TValue.</returns> public static TValue AddOrGetExisting<TValue>(this MemoryCache cache, string key, Func<string, TValue> valueFactory, CacheItemPolicy policy, string regionName = null) { var lazy = new Lazy<TValue>(() => valueFactory(key)); Lazy<TValue> item = (Lazy<TValue>) cache.AddOrGetExisting(key, lazy, policy, regionName) ?? lazy; return item.Value; } /// <summary>A MemoryCache extension method that adds an or get existing.</summary> /// <typeparam name="TValue">Type of the value.</typeparam> /// <param name="cache">The cache to act on.</param> /// <param name="key">The key.</param> /// <param name="valueFactory">The value factory.</param> /// <param name="absoluteExpiration">The policy.</param> /// <param name="regionName">(Optional) name of the region.</param> /// <returns>A TValue.</returns> public static TValue AddOrGetExisting<TValue>(this MemoryCache cache, string key, Func<string, TValue> valueFactory, DateTimeOffset absoluteExpiration, string regionName = null) { var lazy = new Lazy<TValue>(() => valueFactory(key)); Lazy<TValue> item = (Lazy<TValue>) cache.AddOrGetExisting(key, lazy, absoluteExpiration, regionName) ?? lazy; return item.Value; } #endif }