andrew4582

CacheExtentions

Mar 13th, 2011
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.35 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.        
  13.         /// <summary>
  14.         /// Gets a cache value by key, if value is null then return will be the defaultValue
  15.         /// </summary>
  16.         /// <typeparam name="T">Type of Item in the value</typeparam>
  17.         /// <param name="cache"></param>
  18.         /// <param name="key"></param>
  19.         /// <param name="defaultValue">value if value is null</param>
  20.         /// <returns>Cached item or default value</returns>
  21.         public static T Get<T>(this Cache cache,string key,T defaultValue = default(T)) {
  22.  
  23.             object raw = cache[key];
  24.             if(raw == null)
  25.                 return defaultValue;
  26.  
  27.             return (T)Convert.ChangeType(raw,typeof(T));
  28.         }
  29.      
  30.         /// <summary>
  31.         /// TryGet pattern for cache
  32.         /// </summary>
  33.         /// <typeparam name="T"></typeparam>
  34.         /// <param name="cache"></param>
  35.         /// <param name="key"></param>
  36.         /// <param name="cacheValue"></param>
  37.         /// <returns></returns>
  38.         public static bool TryGet<T>(this Cache cache,string key,out T cacheValue) {
  39.  
  40.             object raw = cache[key];
  41.             if(raw == null) {
  42.                 cacheValue = default(T);
  43.                 return false;
  44.             }
  45.  
  46.             cacheValue = (T)Convert.ChangeType(raw,typeof(T));
  47.  
  48.             return cacheValue != null;
  49.         }
  50.  
  51.         /// <summary>
  52.         /// Adds the cached item via the returnValueFunc() delegate to insert/replace the results into the cache
  53.         /// </summary>
  54.         public static void AddOrUpdate<T>(this Cache cache
  55.             ,string key
  56.             ,Func<T> returnValueFunc
  57.             ,CacheDependency dependencies = null
  58.             ,DateTime? absoluteExpiration = null
  59.             ,TimeSpan? slidingExpiration = null
  60.             ,CacheItemPriority priority = CacheItemPriority.Normal
  61.             ,CacheItemRemovedCallback onRemoveCallback = null,bool enabled = true) {
  62.  
  63.             object raw = null;
  64.             if(enabled)
  65.                 raw = cache[key];
  66.  
  67.             if(raw == null) {
  68.                 if(returnValueFunc == null)
  69.                     return;
  70.  
  71.                 T item = returnValueFunc();
  72.  
  73.                 if(enabled)
  74.                     cache.Insert(key,item
  75.                         ,dependencies
  76.                         ,absoluteExpiration.HasValue ? absoluteExpiration.Value : Cache.NoAbsoluteExpiration
  77.                         ,slidingExpiration.HasValue ? slidingExpiration.Value : Cache.NoSlidingExpiration
  78.                         ,priority
  79.                         ,onRemoveCallback);
  80.  
  81.                 return;
  82.             }
  83.         }
  84.         /// <summary>
  85.         /// Returns the cached item or call the returnValueFunc() delegate to insert the results into the cache
  86.         /// </summary>
  87.         /// <returns>Cached Item or Item return from the  returnValueFunc delegate</returns>
  88.         public static T GetOrAdd<T>(this Cache cache
  89.             ,string key
  90.             ,Func<T> returnValueFunc
  91.             ,CacheDependency dependencies = null
  92.             ,DateTime? absoluteExpiration = null
  93.             ,TimeSpan? slidingExpiration = null
  94.             ,CacheItemPriority priority = CacheItemPriority.Normal
  95.             ,CacheItemRemovedCallback onRemoveCallback = null,bool enabled = true) {
  96.  
  97.             object raw = null;
  98.             if(enabled)
  99.                 raw = cache[key];
  100.  
  101.             if(raw == null) {
  102.                 if(returnValueFunc == null)
  103.                     return default(T);
  104.  
  105.                 T item = returnValueFunc();
  106.  
  107.                 if(enabled)
  108.                     cache.Insert(key,item
  109.                         ,dependencies
  110.                         ,absoluteExpiration.HasValue ? absoluteExpiration.Value : Cache.NoAbsoluteExpiration
  111.                         ,slidingExpiration.HasValue ? slidingExpiration.Value : Cache.NoSlidingExpiration
  112.                         ,priority
  113.                         ,onRemoveCallback);
  114.  
  115.                 return item;
  116.             }
  117.             else {
  118.                 return (T)Convert.ChangeType(raw,typeof(T));
  119.             }
  120.         }
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment