Object - FromCache
A TKey extension method that get data from cache.
public static void Main() { var cache1 = new CacheTest(); var cache2 = new CacheTest(); //C# Extension Method: Object - FromCache int cache1_1 = cache1.FromCache(i => cache1.Increment()); int cache1_2 = cache1.FromCache(i => cache1.Increment()); int cache2_1 = cache1.FromCache("CustomKey", i => cache2.Increment()); int cache2_2 = cache1.FromCache("CustomKey", i => cache2.Increment()); int cache3_1 = cache1.FromCache("CustomKeyWithValue", 1); int cache3_2 = cache1.FromCache("CustomKeyWithValue", 2); Console.WriteLine(cache1_1); Console.WriteLine(cache1_2); Console.WriteLine(cache2_1); Console.WriteLine(cache2_2); Console.WriteLine(cache3_1); Console.WriteLine(cache3_2); } public class CacheTest { public int Cache = 0; public int Increment() { Cache++; return Cache; } }
View Source
using System; using System.Linq.Expressions; #if !NETSTANDARD using System.Runtime.Caching; #endif public static partial class Extensions { #if !NETSTANDARD /// <summary>A TKey extension method that from cache.</summary> /// <typeparam name="T">Generic type parameter.</typeparam> /// <typeparam name="TValue">Type of the value.</typeparam> /// <param name="this">The @this to act on.</param> /// <param name="cache">The cache.</param> /// <param name="key">The key.</param> /// <param name="value">The value.</param> /// <returns>A TValue.</returns> public static TValue FromCache<T, TValue>(this T @this, MemoryCache cache, string key, TValue value) { object item = cache.AddOrGetExisting(key, value, new CacheItemPolicy()) ?? value; return (TValue) item; } /// <summary>A TKey extension method that from cache.</summary> /// <typeparam name="T">Generic type parameter.</typeparam> /// <typeparam name="TValue">Type of the value.</typeparam> /// <param name="this">The @this to act on.</param> /// <param name="key">The key.</param> /// <param name="value">The value.</param> /// <returns>A TValue.</returns> public static TValue FromCache<T, TValue>(this T @this, string key, TValue value) { return @this.FromCache(MemoryCache.Default, key, value); } /// <summary>A TKey extension method that from cache.</summary> /// <typeparam name="T">Generic type parameter.</typeparam> /// <typeparam name="TValue">Type of the value.</typeparam> /// <param name="this">The @this to act on.</param> /// <param name="cache">The cache.</param> /// <param name="key">The key.</param> /// <param name="valueFactory">The value factory.</param> /// <returns>A TValue.</returns> public static TValue FromCache<T, TValue>(this T @this, MemoryCache cache, string key, Expression<Func<T, TValue>> valueFactory) { var lazy = new Lazy<TValue>(() => valueFactory.Compile()(@this)); Lazy<TValue> item = (Lazy<TValue>) cache.AddOrGetExisting(key, lazy, new CacheItemPolicy()) ?? lazy; return item.Value; } /// <summary>A TKey extension method that from cache.</summary> /// <typeparam name="T">Generic type parameter.</typeparam> /// <typeparam name="TValue">Type of the value.</typeparam> /// <param name="this">The @this to act on.</param> /// <param name="key">The key.</param> /// <param name="valueFactory">The value factory.</param> /// <returns>A TValue.</returns> public static TValue FromCache<T, TValue>(this T @this, string key, Expression<Func<T, TValue>> valueFactory) { return @this.FromCache(MemoryCache.Default, key, valueFactory); } /// <summary>A TKey extension method that from cache.</summary> /// <typeparam name="TKey">Type of the key.</typeparam> /// <typeparam name="TValue">Type of the value.</typeparam> /// <param name="this">The @this to act on.</param> /// <param name="valueFactory">The value factory.</param> /// <returns>A TValue.</returns> public static TValue FromCache<TKey, TValue>(this TKey @this, Expression<Func<TKey, TValue>> valueFactory) { string key = string.Concat("Z.Caching.FromCache;", typeof (TKey).FullName, valueFactory.ToString()); return @this.FromCache(MemoryCache.Default, key, valueFactory); } /// <summary>A TKey extension method that from cache.</summary> /// <typeparam name="TKey">Type of the key.</typeparam> /// <typeparam name="TValue">Type of the value.</typeparam> /// <param name="this">The @this to act on.</param> /// <param name="cache">The cache.</param> /// <param name="valueFactory">The value factory.</param> /// <returns>A TValue.</returns> public static TValue FromCache<TKey, TValue>(this TKey @this, MemoryCache cache, Expression<Func<TKey, TValue>> valueFactory) { string key = string.Concat("Z.Caching.FromCache;", typeof (TKey).FullName, valueFactory.ToString()); return @this.FromCache(cache, key, valueFactory); } #endif }