要从内存缓存中添加和检索对象,我有一个带有这些方法的缓存util类:
public static T GetNativeItem<T>(string itemKey)
public static void AddNativeItem(string key, object item, TimeSpan timeout)
从数据访问类中删除一些噪音我想使用Castle Dynamic Proxy,在这种特殊情况下我会使用Ninject.Extensions.Interception。
问题是缓存util的GetNativeItem<T>(string itemKey)
,在我的拦截器中如何从T
检索invocation
?
NinjectWebCommon绑定:
kernel.Bind<IMatchDataAccess>().To<MatchDataAccess>().Intercept().With<CacheInterceptor>();
IMatchDataAccess接口具有以下签名:
public interface IMatchDataAccess
{
[Cached(minutes: 10)]
IEnumerable<DomainModel.Match> GetMatches(MatchFilterDto matchFilter);
}
并且CacheInterceptor具有此实现:
public class CacheInterceptor : IInterceptor
{
public void Intercept(IInvocation invocation)
{
var cachedAttr = invocation.Request.Method.GetAttribute<CachedAttribute>();
var p = invocation;
if (cachedAttr == null)
{
invocation.Proceed();
return;
}
var cacheKey = String.Concat(invocation.Request.Method.ReturnType.ToString(), ".", invocation.Request.Method.Name, "(", String.Join(", ", invocation.Request.Arguments), ")");
/*
problem is here
*/
var p = invocation.Request.Method.ReturnType;
var objInCache = CacheUtil.GetNativeItem<p>(cacheKey);
if (objInCache != null)
invocation.ReturnValue = objInCache;
else
{
invocation.Proceed();
var timeout = cachedAttr.Minutes > 0 ? new TimeSpan(0, cachedAttr.Minutes, 0) : new TimeSpan(0, 60, 0);
CacheUtil.AddNativeItem(cacheKey, invocation.ReturnValue, timeout);
}
}
}