Custom Attribute That Caches
public class CacheAttribute : OnMethodInvocationAspect
{
public override void OnInvocation(MethodInvocationEventArgs eventArgs)
{
object value;
string key = // Compute the cache key (details omitted).
if (!cache.TryGetValue(key, out value))
{
lock ( this )
{
if (!cache.TryGetValue(key, out value))
{
eventArgs.Proceed();
value = eventArgs.ReturnValue;
cache.Add(key, value);
return;
}
}
}
eventArgs.ReturnValue = value;
}
}