Created
November 20, 2015 15:49
-
-
Save asimmon/3337358f5eecd0a77432 to your computer and use it in GitHub Desktop.
Caching method results with AOP - Cache Provider
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 interface ICacheProvider | |
{ | |
object Get(string key); | |
void Put(string key, object value, int duration); | |
bool Contains(string key); | |
} | |
public class MemoryCacheProvider : ICacheProvider | |
{ | |
public object Get(string key) | |
{ | |
return MemoryCache.Default[key]; | |
} | |
public void Put(string key, object value, int duration) | |
{ | |
if (duration <= 0) | |
throw new ArgumentException("Duration cannot be less or equal to zero", "duration"); | |
var policy = new CacheItemPolicy | |
{ | |
AbsoluteExpiration = DateTime.Now.AddMilliseconds(duration) | |
}; | |
MemoryCache.Default.Set(key, value, policy); | |
} | |
public bool Contains(string key) | |
{ | |
return MemoryCache.Default[key] != null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment