I have used Autofac
DynamicProxy
to implement cache for methods. But inceptor doesn't work when call a method inside another method in same class. for example I have Class ClassRepository
as:
public class ClassRepository : IClassRepository
{
[Cache]
public async Task<List<string>> Method1()
{
//Do some thing
}
[Cache]
public async Task<List<string>> Method2()
{
var methodCall = await Method1()
}
}
And my inceptor is like this:
public class CacheInterceptor : IInterceptor
{
private readonly ICache cache;
private static ConcurrentDictionary<string, bool> InProcessKeys = new ConcurrentDictionary<string, bool>();
public CacheInterceptor(ICache cache)
{
this.cache = cache;
}
public void Intercept(IInvocation invocation)
{
ProcessInterceptAsync(invocation).Wait();
}
private async Task ProcessInterceptAsync(IInvocation invocation)
{
var proceed = invocation.CaptureProceedInfo();
var cacheAttribute = invocation.MethodInvocationTarget.GetCustomAttributes<CacheAttribute>(false).FirstOrDefault();
if (cacheAttribute == null)
{
proceed.Invoke();
return;
}
var key = GetCacheKey(invocation);
ExistKeyCheck(key);
var methodReturnType = invocation.Method.ReturnType;
dynamic cacheResult = GetCache(key, cacheAttribute, methodReturnType);
if (cacheResult != null)
{
invocation.ReturnValue = cacheResult;
InProcessKeys.Remove(key, out _);
return;
}
InProcessKeys.TryAdd(key, true);
proceed.Invoke();
var delegateType = GetDelegateType(invocation);
switch (delegateType)
{
case MethodType.Synchronous:
break;
case MethodType.AsyncAction:
await InterceptAsync((Task)invocation.ReturnValue);
break;
case MethodType.AsyncFunction:
var method = invocation.MethodInvocationTarget;
var isAsync = method.GetCustomAttribute(typeof(AsyncStateMachineAttribute)) != null;
if (isAsync && typeof(Task).IsAssignableFrom(method.ReturnType))
{
var methodResult = await InterceptAsync((dynamic)invocation.ReturnValue);
invocation.ReturnValue = methodResult;
}
break;
default:
break;
}
if (invocation.ReturnValue == null)
{
InProcessKeys.Remove(key, out _);
return;
}
await cache.SetAsync(key, invocation.ReturnValue, cacheAttribute.Duration, cacheAttribute.CacheInstance, cacheAttribute.Extend);
InProcessKeys.Remove(key, out _);
}
private dynamic GetCache(string key, CacheAttribute cacheAttribute, Type methodReturnType)
{
var finalType = methodReturnType.GetGenericArguments()[0];
var getMethod = typeof(DistributedCache).GetMethods().Where(x => x.IsGenericMethod && x.Name =="Get").FirstOrDefault().MakeGenericMethod(finalType);
var cacheResult = (dynamic)getMethod.Invoke(cache, new object[] { key, cacheAttribute.CacheInstance });
if (cacheResult is null) return null;
if (methodReturnType.GetGenericTypeDefinition() == typeof(Task<>))
return Task.FromResult(cacheResult);
else
return cacheResult;
}
private static void ExistKeyCheck(string key)
{
if (InProcessKeys.Any(x => x.Key==key))
{
Task.Delay(50).Wait();
var counter = 0;
while (InProcessKeys.Any(x => x.Key==key) && counter < 10)
{
Task.Delay(50).Wait();
counter++;
}
}
}
private static async Task InterceptAsync(Task task) => await task.ConfigureAwait(false);
private static async Task<T> InterceptAsync<T>(Task<T> task) => await task.ConfigureAwait(false);
private MethodType GetDelegateType(IInvocation invocation)
{
var returnType = invocation.Method.ReturnType;
if (returnType == typeof(Task))
return MethodType.AsyncAction;
if (returnType.IsGenericType && returnType.GetGenericTypeDefinition() == typeof(Task<>))
return MethodType.AsyncFunction;
return MethodType.Synchronous;
}
private enum MethodType
{
Synchronous,
AsyncAction,
AsyncFunction
}
private static string GetCacheKey(IInvocation invocation)
{
var key = invocation.Arguments.Length > 0 ? $"-{invocation.Arguments[0]}" : "";
var cacheKey = $"{invocation.TargetType.FullName.Replace('.', ':')}" +
$".{invocation.Method.Name}" +
$"{key}";
return cacheKey;
}
}
and My autofac module is like this:
public class RepositoryModule : Autofac.Module
{
protected override void Load(ContainerBuilder builder)
{
builder.RegisterType<CacheInterceptor>();
var flightDataAccess = Assembly.Load("DataAccess");
builder.RegisterAssemblyTypes(flightDataAccess)
.Where(x => x.Name.EndsWith("Repository"))
.AsImplementedInterfaces()
.InstancePerLifetimeScope().InterceptedBy(typeof(CacheInterceptor)).EnableInterfaceInterceptors();
}
}
When I resolve IClassRepository and call Method2, CacheInterceptor execute successfully for Method2. but inside Method2 I called Method1 and it doesn't work for Method1. I'll appreciate if anyone can help.
After a while, I could solve it. At first I had to change my methods to virtual
methods.
Note: for private methods I change them to
protected virtual
Final change Is :
public class ClassRepository : IClassRepository
{
[Cache]
public virtual async Task<List<string>> Method1()
{
//Do some thing
}
[Cache]
public virtual async Task<List<string>> Method2()
{
var methodCall = await Method1()
}
}
In Module
class I changed EnableInterfaceInterceptors
to EnableClassInterceptors
and final change is :
public class RepositoryModule : Autofac.Module
{
protected override void Load(ContainerBuilder builder)
{
builder.RegisterType<CacheInterceptor>();
var flightDataAccess = Assembly.Load("DataAccess");
builder.RegisterAssemblyTypes(flightDataAccess)
.Where(x => x.Name.EndsWith("Repository"))
.AsImplementedInterfaces()
.InstancePerLifetimeScope().InterceptedBy(typeof(CacheInterceptor)).EnableClassInterceptors();
}
}