AOP缓存与温莎城堡
任何人都可以提供一个工作示例,说明如何使用Castle Windsor进行缓存。
我假设作为一个起点,我定义了从IInterceptorinheritance的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 GetStaticData() { }
..当然,在温莎容器中注册整个东西。
然而…
-
如何根据方法调用改变缓存中我想要的东西的时间量? 在这个例子中,我可能希望它被缓存60分钟。 对于一天等的其他示例等。我是否必须为每个缓存持续时间创建一个
CacheAspect
? -
从每种方法中识别每个缓存值的最佳方法是什么? 例如,使用
invocation.TargetType.Name
和invocation.Method.Name
的组合? -
扩展问题2 – 如果传入参数怎么办? 然后我需要确定我是否缓存了与特定参数集匹配的数据。
谢谢。
我前段时间构建了mbcache, http://code.google.com/p/mbcache ,以便使用动态代理启用缓存。 它在内部处理您的问题。 在源代码中有使用Castle Windsor(和LinFu)的实现。 看看它(如果它符合您的需要,直接使用框架)。
简短回答你的问题
-
AFAIK – Windsor(或其他代理fw)没有任何内置支持。 您需要使用一些缓存框架(或自己构建一些缓存逻辑,即使用弱引用)。
-
3.大多数缓存键都是基于字符串构建的。 如果是这样 – 你需要以某种方式将类型,方法和参数转换为字符串。