Advertisement
Guest User

Cache Manager

a guest
Mar 3rd, 2011
674
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.89 KB | None | 0 0
  1. //---------------------------------------------------------------------
  2. // <copyright file="CacheManager.cs" company="NA">
  3. //     No copyright
  4. // </copyright>
  5. // <summary>
  6. // Manages caching
  7. // </summary>
  8. //---------------------------------------------------------------------
  9. namespace MyApplicationNamespace.Caching
  10. {
  11.     #region Directives
  12.  
  13.     using System;
  14.     using System.Text;
  15.     using System.Web;
  16.     using System.Web.Caching;
  17.     using System.Threading;
  18.     using System.Diagnostics;
  19.     using System.Reflection;
  20.     using System.Collections;
  21.     using System.Collections.ObjectModel;
  22.     using System.Collections.Generic;
  23.  
  24.     #endregion
  25.  
  26.     /// <summary>
  27.     /// The CacheManager class provides access to data that is stored using
  28.     /// the <see cref="HttpRuntime"/> caching mechanism .
  29.     /// </summary>
  30.     public class CacheManager
  31.     {
  32.         private static HttpRuntime httpRuntime;
  33.  
  34.         #region Constructs
  35.  
  36.         /// <summary>
  37.         /// Construct
  38.         /// </summary>
  39.         static CacheManager()
  40.         {
  41.             EnsureHttpRuntime();
  42.         }
  43.  
  44.         #endregion
  45.  
  46.         #region Internal Methods
  47.  
  48.         /// <summary>
  49.         /// Access to the <see cref="HttpRuntime"/> Cache object
  50.         /// </summary>
  51.         internal static Cache ObjectCache
  52.         {
  53.             get { return HttpRuntime.Cache; }
  54.         }
  55.  
  56.         #endregion
  57.  
  58.         #region Public static Methods
  59.  
  60.         /// <summary>
  61.         /// Finds an object within the cache
  62.         /// </summary>
  63.         /// <param name="key">The key used in the cache for the obejct to find</param>
  64.         /// <returns>object</returns>
  65.         public static object Find(string key)
  66.         {
  67.             return ObjectCache[key];
  68.         }
  69.  
  70.         /// <summary>
  71.         /// Helper method to add an object to the cache for a period of time
  72.         /// </summary>
  73.         /// <param name="cachedObject">The object to add to the cache</param>
  74.         /// <param name="key">The key name to store the object with</param>
  75.         /// <param name="absoluteExpiration">absoluteExpiration</param>
  76.         /// <param name="slidingExpiration">slidingExpiration</param>
  77.         public static void AddObjectToCache(object cachedObject, string key, DateTime absoluteExpiration, TimeSpan slidingExpiration)
  78.         {
  79.             ObjectCache.Add(key, cachedObject, null, absoluteExpiration, slidingExpiration, CacheItemPriority.Default, null);
  80.         }
  81.  
  82.         /// <summary>
  83.         /// Helper method to add an object to the cache for a period of time
  84.         /// </summary>
  85.         /// <param name="cachedObject">The object to add to the cache</param>
  86.         /// <param name="key">The key name to store the object with</param>
  87.         /// <param name="absoluteExpiration">absoluteExpiration</param>
  88.         public static void AddObjectToCache(object cachedObject, string key, DateTime absoluteExpiration)
  89.         {
  90.             AddObjectToCache(cachedObject, key, absoluteExpiration, Cache.NoSlidingExpiration);
  91.         }
  92.  
  93.         /// <summary>
  94.         /// Helper method to add an object to the cache for a period of time
  95.         /// </summary>
  96.         /// <param name="cachedObject">The object to add to the cache</param>
  97.         /// <param name="key">The key name to store the object with</param>
  98.         /// <param name="slidingExpiration">slidingExpiration</param>
  99.         public static void AddObjectToCache(object cachedObject, string key, TimeSpan slidingExpiration)
  100.         {
  101.             AddObjectToCache(cachedObject, key, Cache.NoAbsoluteExpiration, slidingExpiration);
  102.         }
  103.  
  104.         /// <summary>
  105.         /// Helper method to add an object to the cache for a period of time
  106.         /// </summary>
  107.         /// <param name="cachedObject">The object to add to the cache</param>
  108.         /// <param name="key">The key name to store the object with</param>
  109.         public static void AddObjectToCache(object cachedObject, string key)
  110.         {
  111.             AddObjectToCache(cachedObject, key, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration);
  112.         }
  113.  
  114.         /// <summary>
  115.         /// Removes all items from the cache
  116.         /// </summary>
  117.         public static void ClearCache()
  118.         {
  119.             IDictionaryEnumerator cacheEnum = ObjectCache.GetEnumerator();
  120.             while (cacheEnum.MoveNext())
  121.             {
  122.                 string key = cacheEnum.Key.ToString();
  123.                 RemoveFromCache(key);
  124.             }
  125.         }
  126.  
  127.         /// <summary>
  128.         /// Removes an item from the cache
  129.         /// </summary>
  130.         /// <param name="key">key</param>
  131.         private static void RemoveFromCache(string key)
  132.         {
  133.             ObjectCache.Remove(key);
  134.         }
  135.  
  136.         /// <summary>
  137.         /// Call this method after editing cached data. It will remove the item from the
  138.         /// cache and force the caching machanism to read from the database next time the
  139.         /// data method is called
  140.         /// </summary>
  141.         /// <param name="cacheKey">cacheKey</param>
  142.         public static void InvalidateCache(string cacheKey)
  143.         {
  144.             int length = cacheKey.Length;
  145.  
  146.             List<string> keys = new List<string>();
  147.             IDictionaryEnumerator enumerator = ObjectCache.GetEnumerator();
  148.  
  149.             while (enumerator.MoveNext())
  150.             {
  151.                 string key = enumerator.Key.ToString();
  152.                 if ((key.Length >= length && key.Substring(0, length) == cacheKey) || (key.IndexOf(cacheKey) > -1))
  153.                 {
  154.                     keys.Add(key);
  155.                 }
  156.             }
  157.  
  158.             for (int i = 0; i < keys.Count; i++)
  159.             {
  160.                 CacheManager.RemoveFromCache(keys[i]);
  161.             }
  162.         }
  163.  
  164.         /// <summary>
  165.         /// Generic method that controls whether an item is retrieved from either the database or the object cache.
  166.         /// </summary>
  167.         /// <typeparam name="TValue">Type of returned item</typeparam>
  168.         /// <param name="cacheKey">The key that identifies the item in the cache</param>
  169.         /// <param name="absoluteExpiration">Absolute time when item should expire</param>
  170.         /// <param name="slidingExpiration">Time since last access till item expires</param>
  171.         /// <param name="method">The method used to perform the data operation</param>
  172.         /// <returns>TValue</returns>
  173.         public static TValue RetrieveFromCacheOrInvoke<TValue>(string cacheKey, DateTime absoluteExpiration, TimeSpan slidingExpiration, Delegate method)
  174.         {
  175.             TValue results = default(TValue);
  176.             if (ObjectCache[cacheKey] == null)
  177.             {
  178.                 results = (TValue)method.DynamicInvoke();
  179.                 AddObjectToCache(results, cacheKey, absoluteExpiration, slidingExpiration);
  180.             }
  181.             else
  182.             {
  183.                 results = (TValue)ObjectCache[cacheKey];
  184.             }
  185.  
  186.             return results;
  187.         }
  188.  
  189.         /// <summary>
  190.         /// Generic method that controls whether an item is retrieved from either the database or the object cache.
  191.         /// </summary>
  192.         /// <typeparam name="TValue">Type of returned item</typeparam>
  193.         /// <param name="cacheKey">The key that identifies the item in the cache</param>
  194.         /// <param name="absoluteExpiration">Absolute time when item should expire</param>
  195.         /// <param name="method">The method used to perform the data operation</param>
  196.         /// <returns>TValue</returns>
  197.         public static TValue RetrieveFromCacheOrInvoke<TValue>(string cacheKey, DateTime absoluteExpiration, Delegate method)
  198.         {
  199.             return RetrieveFromCacheOrInvoke<TValue>(cacheKey, absoluteExpiration, Cache.NoSlidingExpiration, method);
  200.         }
  201.  
  202.         /// <summary>
  203.         /// Generic method that controls whether an item is retrieved from either the database or the object cache.
  204.         /// </summary>
  205.         /// <typeparam name="TValue">Type of returned item</typeparam>
  206.         /// <param name="cacheKey">The key that identifies the item in the cache</param>
  207.         /// <param name="slidingExpiration">Time since last access till item expires</param>
  208.         /// <param name="method">The method used to perform the data operation</param>
  209.         /// <returns>TValue</returns>
  210.         public static TValue RetrieveFromCacheOrInvoke<TValue>(string cacheKey, TimeSpan slidingExpiration, Delegate method)
  211.         {
  212.             return RetrieveFromCacheOrInvoke<TValue>(cacheKey, Cache.NoAbsoluteExpiration, slidingExpiration, method);
  213.         }
  214.  
  215.         /// <summary>
  216.         /// Generic method that controls whether an item is retrieved from either the database or the object cache.
  217.         /// </summary>
  218.         /// <typeparam name="TValue">Type of returned item</typeparam>
  219.         /// <param name="cacheKey">The key that identifies the item in the cache</param>
  220.         /// <param name="method">The method used to perform the data operation</param>
  221.         /// <returns>TValue</returns>
  222.         public static TValue RetrieveFromCacheOrInvoke<TValue>(string cacheKey, Delegate method)
  223.         {
  224.             return RetrieveFromCacheOrInvoke<TValue>(cacheKey, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, method);
  225.         }
  226.  
  227.         #endregion
  228.  
  229.         #region Private Methods
  230.  
  231.         /// <summary>
  232.         /// Ensures the HttpRuntime is initialised
  233.         /// </summary>
  234.         private static void EnsureHttpRuntime()
  235.         {
  236.             if (null == httpRuntime)
  237.             {
  238.                 try
  239.                 {
  240.                     // Create an Http Content to give us access to the cache.
  241.                     Monitor.Enter(typeof(CacheManager));
  242.                     httpRuntime = new HttpRuntime();
  243.                 }
  244.                 finally
  245.                 {
  246.                     Monitor.Exit(typeof(CacheManager));
  247.                 }
  248.             }
  249.         }
  250.  
  251.         #endregion
  252.     }
  253.  
  254. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement