Created
June 5, 2015 21:26
-
-
Save Tron5000/f15c2ac58746e97d4d39 to your computer and use it in GitHub Desktop.
C# CacheManager
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Configuration; | |
| using System.Runtime.Caching; | |
| using System.Runtime.Serialization; | |
| using System.Reflection; | |
| using System.Linq.Expressions; | |
| using System.Diagnostics; | |
| namespace Util.Caching | |
| { | |
| public class CacheManager | |
| { | |
| /// <summary> | |
| /// Boolean value from "UseDataCache" appSetting in the config - controls whether or not caching is enabled. | |
| /// If retrieving this value from the config fails this value defaults to false. | |
| /// </summary> | |
| protected bool UseDataCache | |
| { | |
| get | |
| { | |
| bool parsedUseDataCache = false; | |
| bool.TryParse(ConfigurationManager.AppSettings["UseDataCache"], out parsedUseDataCache); | |
| return parsedUseDataCache; | |
| } | |
| } | |
| /// <summary> | |
| /// Reference to the cache object being used. | |
| /// </summary> | |
| protected MemoryCache Cache | |
| { | |
| get | |
| { | |
| return MemoryCache.Default; | |
| } | |
| } | |
| /// <summary> | |
| /// For data retrieval operations that need to be cached for a short time. | |
| /// It simply sets an absolute expiration date expirationSeconds seconds in the future. | |
| /// </summary> | |
| /// <typeparam name="T"></typeparam> | |
| /// <param name="dataRetrieval"></param> | |
| /// <param name="expirationSeconds"></param> | |
| /// <returns></returns> | |
| public T GetCachedData<T>(Expression<Func<T>> dataRetrieval, int expirationSeconds) where T : class | |
| { | |
| T item = null; | |
| if (dataRetrieval != null) | |
| { | |
| // Use the method name of the dataRetrieval method as the cache key. | |
| string cacheKey = ((MethodCallExpression)dataRetrieval.Body).Method.Name; | |
| if (!string.IsNullOrEmpty(cacheKey)) | |
| { | |
| // CacheKey: OBJECTNAME.METHODNAME.ARG1VALUE.ARG2VALUE etc | |
| cacheKey = typeof(T).Name + "." + cacheKey; | |
| var arguments = ((MethodCallExpression)dataRetrieval.Body).Arguments; | |
| if (arguments != null) | |
| { | |
| foreach (var arg in arguments) | |
| { | |
| var argAsObject = Expression.Convert(arg, typeof(object)); | |
| var argValue = Expression.Lambda<Func<object>>(argAsObject, null).Compile()(); | |
| cacheKey += "." + argValue.ToString(); | |
| } | |
| } | |
| item = Cache[cacheKey] as T; | |
| if (item == null) | |
| { | |
| Debug.WriteLine("CacheManager " + cacheKey + " was not found in cache at " + DateTime.Now.ToString("M/d/yyyy HH:mm:ss tt") + ". Cache Count: " + Cache.Count()); | |
| item = dataRetrieval.Compile()(); | |
| if ((UseDataCache) && (item != null)) | |
| { | |
| // Only add item to the cache if using the cache as per the config and if the item to be cached isn't null. | |
| CacheItemPolicy policy = new CacheItemPolicy(); | |
| policy.AbsoluteExpiration = DateTimeOffset.Now.AddSeconds(expirationSeconds); | |
| Cache.Set(cacheKey, item, policy); | |
| Debug.WriteLine("CacheManager " + cacheKey + " was added to cache at " + DateTime.Now.ToString("M/d/yyyy HH:mm:ss tt") + ". Cache Count: " + Cache.Count()); | |
| } | |
| } | |
| } | |
| } | |
| return item; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment