Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //---------------------------------------------------------------------
- // <copyright file="CacheManager.cs" company="NA">
- // No copyright
- // </copyright>
- // <summary>
- // Manages caching
- // </summary>
- //---------------------------------------------------------------------
- namespace MyApplicationNamespace.Caching
- {
- #region Directives
- using System;
- using System.Text;
- using System.Web;
- using System.Web.Caching;
- using System.Threading;
- using System.Diagnostics;
- using System.Reflection;
- using System.Collections;
- using System.Collections.ObjectModel;
- using System.Collections.Generic;
- #endregion
- /// <summary>
- /// The CacheManager class provides access to data that is stored using
- /// the <see cref="HttpRuntime"/> caching mechanism .
- /// </summary>
- public class CacheManager
- {
- private static HttpRuntime httpRuntime;
- #region Constructs
- /// <summary>
- /// Construct
- /// </summary>
- static CacheManager()
- {
- EnsureHttpRuntime();
- }
- #endregion
- #region Internal Methods
- /// <summary>
- /// Access to the <see cref="HttpRuntime"/> Cache object
- /// </summary>
- internal static Cache ObjectCache
- {
- get { return HttpRuntime.Cache; }
- }
- #endregion
- #region Public static Methods
- /// <summary>
- /// Finds an object within the cache
- /// </summary>
- /// <param name="key">The key used in the cache for the obejct to find</param>
- /// <returns>object</returns>
- public static object Find(string key)
- {
- return ObjectCache[key];
- }
- /// <summary>
- /// Helper method to add an object to the cache for a period of time
- /// </summary>
- /// <param name="cachedObject">The object to add to the cache</param>
- /// <param name="key">The key name to store the object with</param>
- /// <param name="absoluteExpiration">absoluteExpiration</param>
- /// <param name="slidingExpiration">slidingExpiration</param>
- public static void AddObjectToCache(object cachedObject, string key, DateTime absoluteExpiration, TimeSpan slidingExpiration)
- {
- ObjectCache.Add(key, cachedObject, null, absoluteExpiration, slidingExpiration, CacheItemPriority.Default, null);
- }
- /// <summary>
- /// Helper method to add an object to the cache for a period of time
- /// </summary>
- /// <param name="cachedObject">The object to add to the cache</param>
- /// <param name="key">The key name to store the object with</param>
- /// <param name="absoluteExpiration">absoluteExpiration</param>
- public static void AddObjectToCache(object cachedObject, string key, DateTime absoluteExpiration)
- {
- AddObjectToCache(cachedObject, key, absoluteExpiration, Cache.NoSlidingExpiration);
- }
- /// <summary>
- /// Helper method to add an object to the cache for a period of time
- /// </summary>
- /// <param name="cachedObject">The object to add to the cache</param>
- /// <param name="key">The key name to store the object with</param>
- /// <param name="slidingExpiration">slidingExpiration</param>
- public static void AddObjectToCache(object cachedObject, string key, TimeSpan slidingExpiration)
- {
- AddObjectToCache(cachedObject, key, Cache.NoAbsoluteExpiration, slidingExpiration);
- }
- /// <summary>
- /// Helper method to add an object to the cache for a period of time
- /// </summary>
- /// <param name="cachedObject">The object to add to the cache</param>
- /// <param name="key">The key name to store the object with</param>
- public static void AddObjectToCache(object cachedObject, string key)
- {
- AddObjectToCache(cachedObject, key, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration);
- }
- /// <summary>
- /// Removes all items from the cache
- /// </summary>
- public static void ClearCache()
- {
- IDictionaryEnumerator cacheEnum = ObjectCache.GetEnumerator();
- while (cacheEnum.MoveNext())
- {
- string key = cacheEnum.Key.ToString();
- RemoveFromCache(key);
- }
- }
- /// <summary>
- /// Removes an item from the cache
- /// </summary>
- /// <param name="key">key</param>
- private static void RemoveFromCache(string key)
- {
- ObjectCache.Remove(key);
- }
- /// <summary>
- /// Call this method after editing cached data. It will remove the item from the
- /// cache and force the caching machanism to read from the database next time the
- /// data method is called
- /// </summary>
- /// <param name="cacheKey">cacheKey</param>
- public static void InvalidateCache(string cacheKey)
- {
- int length = cacheKey.Length;
- List<string> keys = new List<string>();
- IDictionaryEnumerator enumerator = ObjectCache.GetEnumerator();
- while (enumerator.MoveNext())
- {
- string key = enumerator.Key.ToString();
- if ((key.Length >= length && key.Substring(0, length) == cacheKey) || (key.IndexOf(cacheKey) > -1))
- {
- keys.Add(key);
- }
- }
- for (int i = 0; i < keys.Count; i++)
- {
- CacheManager.RemoveFromCache(keys[i]);
- }
- }
- /// <summary>
- /// Generic method that controls whether an item is retrieved from either the database or the object cache.
- /// </summary>
- /// <typeparam name="TValue">Type of returned item</typeparam>
- /// <param name="cacheKey">The key that identifies the item in the cache</param>
- /// <param name="absoluteExpiration">Absolute time when item should expire</param>
- /// <param name="slidingExpiration">Time since last access till item expires</param>
- /// <param name="method">The method used to perform the data operation</param>
- /// <returns>TValue</returns>
- public static TValue RetrieveFromCacheOrInvoke<TValue>(string cacheKey, DateTime absoluteExpiration, TimeSpan slidingExpiration, Delegate method)
- {
- TValue results = default(TValue);
- if (ObjectCache[cacheKey] == null)
- {
- results = (TValue)method.DynamicInvoke();
- AddObjectToCache(results, cacheKey, absoluteExpiration, slidingExpiration);
- }
- else
- {
- results = (TValue)ObjectCache[cacheKey];
- }
- return results;
- }
- /// <summary>
- /// Generic method that controls whether an item is retrieved from either the database or the object cache.
- /// </summary>
- /// <typeparam name="TValue">Type of returned item</typeparam>
- /// <param name="cacheKey">The key that identifies the item in the cache</param>
- /// <param name="absoluteExpiration">Absolute time when item should expire</param>
- /// <param name="method">The method used to perform the data operation</param>
- /// <returns>TValue</returns>
- public static TValue RetrieveFromCacheOrInvoke<TValue>(string cacheKey, DateTime absoluteExpiration, Delegate method)
- {
- return RetrieveFromCacheOrInvoke<TValue>(cacheKey, absoluteExpiration, Cache.NoSlidingExpiration, method);
- }
- /// <summary>
- /// Generic method that controls whether an item is retrieved from either the database or the object cache.
- /// </summary>
- /// <typeparam name="TValue">Type of returned item</typeparam>
- /// <param name="cacheKey">The key that identifies the item in the cache</param>
- /// <param name="slidingExpiration">Time since last access till item expires</param>
- /// <param name="method">The method used to perform the data operation</param>
- /// <returns>TValue</returns>
- public static TValue RetrieveFromCacheOrInvoke<TValue>(string cacheKey, TimeSpan slidingExpiration, Delegate method)
- {
- return RetrieveFromCacheOrInvoke<TValue>(cacheKey, Cache.NoAbsoluteExpiration, slidingExpiration, method);
- }
- /// <summary>
- /// Generic method that controls whether an item is retrieved from either the database or the object cache.
- /// </summary>
- /// <typeparam name="TValue">Type of returned item</typeparam>
- /// <param name="cacheKey">The key that identifies the item in the cache</param>
- /// <param name="method">The method used to perform the data operation</param>
- /// <returns>TValue</returns>
- public static TValue RetrieveFromCacheOrInvoke<TValue>(string cacheKey, Delegate method)
- {
- return RetrieveFromCacheOrInvoke<TValue>(cacheKey, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, method);
- }
- #endregion
- #region Private Methods
- /// <summary>
- /// Ensures the HttpRuntime is initialised
- /// </summary>
- private static void EnsureHttpRuntime()
- {
- if (null == httpRuntime)
- {
- try
- {
- // Create an Http Content to give us access to the cache.
- Monitor.Enter(typeof(CacheManager));
- httpRuntime = new HttpRuntime();
- }
- finally
- {
- Monitor.Exit(typeof(CacheManager));
- }
- }
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement