Skip to content

Instantly share code, notes, and snippets.

@aramkoukia
Created January 6, 2018 23:52
Show Gist options
  • Save aramkoukia/375696f1c8ff0be34a4194d3a25254e0 to your computer and use it in GitHub Desktop.
Save aramkoukia/375696f1c8ff0be34a4194d3a25254e0 to your computer and use it in GitHub Desktop.
Cache Attribute Handler
using CacheManager.Core;
using System;
using Unity.Interception.PolicyInjection.Pipeline;
namespace CachingAspectOrientedImplemetation
{
public class CacheAttributeHandler : ICallHandler
{
private BaseCacheManager<object> _cache;
public String KeyPrefix { get; set; }
public int Order { get; set; }
public CacheAttributeHandler(string keyPrefix)
{
var config = new ConfigurationBuilder()
.WithSystemRuntimeCacheHandle()
.Build();
_cache = new BaseCacheManager<object>(config);
KeyPrefix = keyPrefix;
}
public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
{
var result = _cache.Get(KeyPrefix);
if (result != null)
return input.CreateMethodReturn(result);
IMethodReturn methodReturn = getNext()(input, getNext);
_cache.Add(KeyPrefix, methodReturn.ReturnValue);
return methodReturn;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment