Skip to content

Instantly share code, notes, and snippets.

@davidwhitney
Created February 6, 2012 13:04
Show Gist options
  • Save davidwhitney/1751946 to your computer and use it in GitHub Desktop.
Save davidwhitney/1751946 to your computer and use it in GitHub Desktop.
Caching thing for Ian
public class ThingThatGetsMeData : IThingThatGetsMeData
{
public Data GetTheData()
{
// database loading logic
}
}
public class CachingThingThatGetsMeData : IThingThatGetsMeData
{
IThingThatGetsMeData _inner;
public CachingThingThatGetsMeData(IThingThatGetsMeData realOne)
{
_inner = realOne;
}
public Data GetTheData()
{
if(_cache.ContainsKey[dataKey])
{
return _cache.ContainsKey[dataKey];
}
var data = _inner.GetTheData();
_cache[dataKey] = data;
return data;
}
}
Then instead of injecting ThingThatGetsMeData just inject CachingThingThatGetsMeData and after the first hit, everything is cool.
Just use a MemoryCache internally, as either a static or set the caching thing as a container managed singleton.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment