andrew4582

CacheExtentions

Aug 18th, 2010
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.37 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Caching;
  6.  
  7. namespace Core.Web {
  8.     /// <summary>
  9.     /// Extra syntax sugar for System.Web.Caching.Cache
  10.     /// </summary>
  11.     public static class CacheExtentions {
  12.         /// <summary>
  13.         /// Gets a cache value by key, if value is null then return will be the defaultValue
  14.         /// </summary>
  15.         /// <typeparam name="T">Type of Item in the value</typeparam>
  16.         /// <param name="cache"></param>
  17.         /// <param name="key"></param>
  18.         /// <param name="defaultValue">value if value is null</param>
  19.         /// <returns>Cached item or default value</returns>
  20.         public static T Get<T>(this Cache cache,string key,T defaultValue = default(T)) {
  21.  
  22.             object raw = cache[key];
  23.             if(raw == null)
  24.                 return defaultValue;
  25.  
  26.             return (T)Convert.ChangeType(raw,typeof(T));
  27.         }
  28.         /// <summary>
  29.         /// TryGet pattern for cache
  30.         /// </summary>
  31.         /// <typeparam name="T"></typeparam>
  32.         /// <param name="cache"></param>
  33.         /// <param name="key"></param>
  34.         /// <param name="cacheValue"></param>
  35.         /// <returns></returns>
  36.         public static bool TryGet<T>(this Cache cache,string key,out T cacheValue) {
  37.  
  38.             object raw = cache[key];
  39.             if(raw == null) {
  40.                 cacheValue = default(T);
  41.                 return false;
  42.             }
  43.  
  44.             cacheValue = (T)Convert.ChangeType(raw,typeof(T));
  45.  
  46.             return cacheValue != null;
  47.         }
  48.         /// <summary>
  49.         /// Returns the cached item or call the returnValueFunc() delegate to insert the results into the cache
  50.         /// </summary>
  51.         /// <typeparam name="T"></typeparam>
  52.         /// <param name="cache"></param>
  53.         /// <param name="key"></param>
  54.         /// <param name="returnValueFunc">Delegate to be called to insert an item into the cache</param>
  55.         /// <returns>Cached Item or Item return from the  returnValueFunc delegate</returns>
  56.         public static T InsertGet<T>(this Cache cache,string key,Func<T> returnValueFunc) {
  57.  
  58.             object raw = cache[key];
  59.  
  60.             if(raw == null) {
  61.                 if(returnValueFunc == null)
  62.                     return default(T);
  63.  
  64.                 T item = returnValueFunc();
  65.                 cache.Insert(key,item);
  66.                 return item;
  67.             }
  68.             else {
  69.                 return (T)Convert.ChangeType(raw,typeof(T));
  70.             }
  71.         }
  72.         /// <summary>
  73.         /// Returns the cached item or call the cacheFunc() delegate to insert the results into the cache
  74.         /// </summary>
  75.         /// <typeparam name="T"></typeparam>
  76.         /// <param name="cache"></param>
  77.         /// <param name="key"></param>
  78.         /// <param name="cacheFunc">Delegate cacheFunc: (cache,key)=>{ cache.Insert(key,"DATA"); return "DATA" }</param>
  79.         /// <returns>Cached Item or Item return from the  cacheFunc delegate</returns>
  80.         public static T InsertGet<T>(this Cache cache,string key,Func<Cache,string,T> cacheFunc) {
  81.  
  82.             object raw = cache[key];
  83.  
  84.             if(raw == null) {
  85.                 if(cacheFunc == null)
  86.                     return default(T);
  87.  
  88.                 T item = cacheFunc(cache,key);
  89.  
  90.                 return item;
  91.             }
  92.             else {
  93.                 return (T)Convert.ChangeType(raw,typeof(T));
  94.             }
  95.         }
  96.         /// <summary>
  97.         /// Returns the cached item or call the cacheFunc() delegate to insert the results into the cache
  98.         /// </summary>
  99.         /// <typeparam name="T"></typeparam>
  100.         /// <param name="cache"></param>
  101.         /// <param name="key"></param>
  102.         /// <param name="returnValueFunc"></param>
  103.         /// <param name="dependencies"></param>
  104.         /// <param name="absoluteExpiration"></param>
  105.         /// <param name="slidingExpiration"></param>
  106.         /// <param name="priority"></param>
  107.         /// <param name="onRemoveCallback"></param>
  108.         /// <returns>Cached Item or Item return from the  returnValueFunc delegate</returns>
  109.         public static T InsertGet<T>(this Cache cache
  110.             ,string key
  111.             ,Func<T> returnValueFunc
  112.             ,CacheDependency dependencies = null
  113.             ,DateTime? absoluteExpiration = null
  114.             ,TimeSpan? slidingExpiration = null
  115.             ,CacheItemPriority priority = CacheItemPriority.Normal
  116.             ,CacheItemRemovedCallback onRemoveCallback = null,bool enabled = true) {
  117.  
  118.             object raw = null;
  119.             if(enabled)
  120.                 raw = cache[key];
  121.  
  122.             if(raw == null) {
  123.                 if(returnValueFunc == null)
  124.                     return default(T);
  125.  
  126.                 T item = returnValueFunc();
  127.  
  128.                 if(enabled)
  129.                     cache.Insert(key,item
  130.                         ,dependencies
  131.                         ,absoluteExpiration.HasValue ? absoluteExpiration.Value : Cache.NoAbsoluteExpiration
  132.                         ,slidingExpiration.HasValue ? slidingExpiration.Value : Cache.NoSlidingExpiration
  133.                         ,priority
  134.                         ,onRemoveCallback);
  135.  
  136.                 return item;
  137.             }
  138.             else {
  139.                 return (T)Convert.ChangeType(raw,typeof(T));
  140.             }
  141.         }
  142.     }
  143. }
Add Comment
Please, Sign In to add comment