Created
January 6, 2018 23:52
-
-
Save aramkoukia/375696f1c8ff0be34a4194d3a25254e0 to your computer and use it in GitHub Desktop.
Cache Attribute Handler
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
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