Skip to content

Instantly share code, notes, and snippets.

@Dashue
Created October 25, 2011 18:36
Show Gist options
  • Save Dashue/1313772 to your computer and use it in GitHub Desktop.
Save Dashue/1313772 to your computer and use it in GitHub Desktop.
Helper for inserting and getting entries from the HttpContext Cache
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