Created
October 25, 2011 18:36
-
-
Save Dashue/1313772 to your computer and use it in GitHub Desktop.
Helper for inserting and getting entries from the HttpContext Cache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
namespace PinnacleSports.DynamicLines.Helpers | |
{ | |
public class HttpContextCacheHelper | |
{ | |
private readonly ICache _cache; | |
public HttpContextCacheHelper() | |
{ | |
_cache = new HttpContextCache(); | |
} | |
public HttpContextCacheHelper(ICache cache) | |
{ | |
_cache = cache; | |
} | |
public void Insert(string key, object value, DateTime absoluteExpiration, TimeSpan slidingExpiration) | |
{ | |
_cache.Insert(key, value, absoluteExpiration, slidingExpiration); | |
} | |
public List<TOut> GetByKey<TOut>(IEnumerable<string> keys, out List<string> keysNotInCache) | |
where TOut : class | |
{ | |
var cachedEntries = new List<TOut>(); | |
keysNotInCache = new List<string>(); | |
foreach (var key in keys) | |
{ | |
var cachedEntry = GetByKey<TOut>(key); | |
if (cachedEntry == null) | |
{ | |
keysNotInCache.Add(key); | |
} | |
else | |
{ | |
cachedEntries.Add(cachedEntry); | |
} | |
} | |
return cachedEntries; | |
} | |
public TOut GetByKey<TOut>(string key) | |
where TOut : class | |
{ | |
var obj = _cache.Get(key); | |
return obj != null ? (TOut)obj : null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment