Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Caching;
- namespace Core.Web {
- /// <summary>
- /// Extra syntax sugar for System.Web.Caching.Cache
- /// </summary>
- public static class CacheExtentions {
- /// <summary>
- /// Gets a cache value by key, if value is null then return will be the defaultValue
- /// </summary>
- /// <typeparam name="T">Type of Item in the value</typeparam>
- /// <param name="cache"></param>
- /// <param name="key"></param>
- /// <param name="defaultValue">value if value is null</param>
- /// <returns>Cached item or default value</returns>
- public static T Get<T>(this Cache cache,string key,T defaultValue = default(T)) {
- object raw = cache[key];
- if(raw == null)
- return defaultValue;
- return (T)Convert.ChangeType(raw,typeof(T));
- }
- /// <summary>
- /// TryGet pattern for cache
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="cache"></param>
- /// <param name="key"></param>
- /// <param name="cacheValue"></param>
- /// <returns></returns>
- public static bool TryGet<T>(this Cache cache,string key,out T cacheValue) {
- object raw = cache[key];
- if(raw == null) {
- cacheValue = default(T);
- return false;
- }
- cacheValue = (T)Convert.ChangeType(raw,typeof(T));
- return cacheValue != null;
- }
- /// <summary>
- /// Adds the cached item via the returnValueFunc() delegate to insert/replace the results into the cache
- /// </summary>
- public static void AddOrUpdate<T>(this Cache cache
- ,string key
- ,Func<T> returnValueFunc
- ,CacheDependency dependencies = null
- ,DateTime? absoluteExpiration = null
- ,TimeSpan? slidingExpiration = null
- ,CacheItemPriority priority = CacheItemPriority.Normal
- ,CacheItemRemovedCallback onRemoveCallback = null,bool enabled = true) {
- object raw = null;
- if(enabled)
- raw = cache[key];
- if(raw == null) {
- if(returnValueFunc == null)
- return;
- T item = returnValueFunc();
- if(enabled)
- cache.Insert(key,item
- ,dependencies
- ,absoluteExpiration.HasValue ? absoluteExpiration.Value : Cache.NoAbsoluteExpiration
- ,slidingExpiration.HasValue ? slidingExpiration.Value : Cache.NoSlidingExpiration
- ,priority
- ,onRemoveCallback);
- return;
- }
- }
- /// <summary>
- /// Returns the cached item or call the returnValueFunc() delegate to insert the results into the cache
- /// </summary>
- /// <returns>Cached Item or Item return from the returnValueFunc delegate</returns>
- public static T GetOrAdd<T>(this Cache cache
- ,string key
- ,Func<T> returnValueFunc
- ,CacheDependency dependencies = null
- ,DateTime? absoluteExpiration = null
- ,TimeSpan? slidingExpiration = null
- ,CacheItemPriority priority = CacheItemPriority.Normal
- ,CacheItemRemovedCallback onRemoveCallback = null,bool enabled = true) {
- object raw = null;
- if(enabled)
- raw = cache[key];
- if(raw == null) {
- if(returnValueFunc == null)
- return default(T);
- T item = returnValueFunc();
- if(enabled)
- cache.Insert(key,item
- ,dependencies
- ,absoluteExpiration.HasValue ? absoluteExpiration.Value : Cache.NoAbsoluteExpiration
- ,slidingExpiration.HasValue ? slidingExpiration.Value : Cache.NoSlidingExpiration
- ,priority
- ,onRemoveCallback);
- return item;
- }
- else {
- return (T)Convert.ChangeType(raw,typeof(T));
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment