postsharp를 사용하여 구현 된 간단한 Cache 특성이 있습니다. 캐시 정책을 설정할 때 아래와 같이 업데이트 콜백을 설정할 수 있기를 원합니다.다른 클래스의 CacheItemPolicy에 UpdateCallback을 구현하려면 어떻게해야합니까?
private static CacheItemPolicy GetCachePolicy(CacheType type, int expiry)
{
var policy = new CacheItemPolicy();
switch (type)
{
case (CacheType.Absolute):
policy.AbsoluteExpiration = DateTimeOffset.Now.AddSeconds(expiry);
policy.UpdateCallback = new CacheEntryUpdateCallback(UpdateHandler);
break;
case (CacheType.Sliding):
policy.SlidingExpiration = new TimeSpan(0, 0, 0, expiry);
break;
}
return policy;
}
난 그냥이 작업을 수행하려는 경우가 괜찮 :
private static void UpdateHandler(CacheEntryUpdateArguments arguments)
{
throw new NotImplementedException();
}
는 그러나, 나는 동적으로 대리자/방법/메소드 이름과 매개 변수에 전달하고 그것을 실행할 수 있어야합니다. 그래서 내가 좋아하는 무언가를보고 기대 (분명히 구문은 잘못된 것입니다) :
private static CacheItemPolicy GetCachePolicy(CacheType type, int expiry Func<?,?> method)
{
var policy = new CacheItemPolicy();
switch (type)
{
case (CacheType.Absolute):
policy.AbsoluteExpiration = DateTimeOffset.Now.AddSeconds(expiry);
policy.UpdateCallback = new CacheEntryUpdateCallback(method);
break;
case (CacheType.Sliding):
policy.SlidingExpiration = new TimeSpan(0, 0, 0, expiry);
break;
}
return policy;
}
** * ** * ** * *UPDATE* * * * ****
나는 그것을 얻었다. 가장 우아한 방법은 아니고 꽃 봉오리가 작동합니다. 다음과 같이
내 화면 코드는 다음과 같습니다
private static void UpdateHandler(CacheEntryUpdateArguments arguments)
{
CacheObject cacheObject = (CacheObject)arguments.Source.Get(arguments.Key);
cacheObject.Context.Proceed();
cacheObject.CacheValue = cacheObject.Context.ReturnValue;
CacheItem updatedItem = new CacheItem(arguments.Key, cacheObject);
arguments.UpdatedCacheItem = updatedItem;
}
질문에 aspect 코드를 포함시켜 줄 수 있습니까? 네가 성취하려는 것을 이해하지 못한다. 어디에서 GetCachePolicy 메서드를 호출하고 어떤 부분이 적용됩니까? –