Skip to content

Instantly share code, notes, and snippets.

@asimmon
Created November 20, 2015 15:49
Show Gist options
  • Save asimmon/3337358f5eecd0a77432 to your computer and use it in GitHub Desktop.
Save asimmon/3337358f5eecd0a77432 to your computer and use it in GitHub Desktop.
Caching method results with AOP - Cache Provider
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