【发布时间】:2012-04-26 16:28:58
【问题描述】:
谁能提供一个工作示例,说明使用 Castle Windsor 进行缓存是如何工作的。
我假设作为起点,我定义了继承自 IInterceptor 的 CacheAspect,如下所示:
public class CacheAspect : IInterceptor
{
public void Intercept(IInvocation invocation)
{
// Code here to check if data is in cache and if so
// put that into invocation.ReturnValue... job done!
// If not then invoke the method
invocation.Proceed();
// Now cache the result of the invocation
}
}
然后我可以用我的CacheAspect 装饰任何方法...
[Interceptor(typeof(CacheAspect))]
public List<string> GetStaticData()
{
}
.. 当然也可以在 Windsor 容器中注册整个内容。
不过……
如何改变每次方法调用在缓存中所需的时间量?在此示例中,我可能希望将其缓存 60 分钟。对于一天等其他示例。我是否必须为每个缓存持续时间创建一个
CacheAspect?从每种方法中识别每个缓存值的最佳方法是什么?例如使用
invocation.TargetType.Name和invocation.Method.Name的组合?扩展问题 2 - 如果有参数传入怎么办?然后我需要确定我是否缓存了与一组特定参数匹配的数据。
谢谢。
【问题讨论】:
标签: c# .net caching castle-windsor aop