Created
February 6, 2012 13:04
-
-
Save davidwhitney/1751946 to your computer and use it in GitHub Desktop.
Caching thing for Ian
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
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