使用 Castle Windsor 进行 AOP 缓存 [英] AOP Caching with Castle Windsor
问题描述
谁能提供一个关于使用 Castle Windsor 进行缓存的工作示例.
Can anyone provide a working example of how caching with Castle Windsor would work.
我假设作为起点,我定义了从 IInterceptor 继承的 CacheAspect
如下:
I presume as a starting point I define my CacheAspect
which inherits from IInterceptor as follows:
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
...
I can then decorate any method with my CacheAspect
...
[Interceptor(typeof(CacheAspect))]
public List<string> GetStaticData()
{
}
.. 当然,在 Windsor 容器中注册整个内容.
.. and of course register the whole thing in the Windsor container.
不过……
如何改变每次方法调用在缓存中需要的时间?在这个例子中,我可能希望它被缓存 60 分钟.对于一天等的其他示例,我是否必须为每个缓存持续时间创建一个
CacheAspect
?
从每个方法中识别每个缓存值的最佳方法是什么?例如,使用 invocation.TargetType.Name
和 invocation.Method.Name
的组合?
What is the best way to identify each cached value from each method? Using a combination of invocation.TargetType.Name
and invocation.Method.Name
for example?
扩展问题 2 - 如果有参数传入怎么办?然后我需要确定我是否缓存了匹配一组特定参数的数据.
Expanding on question 2 - what if there are parameters passed in? Then I need to determine if I have cached data matching a specific set of parameters.
谢谢.
推荐答案
我构建了 mbcache,http://code.google.com/p/mbcache,前段时间使用动态代理启用缓存.它在内部处理您的问题.在源代码中有使用 Castle Windsor(和 LinFu)的实现.看看它(或者如果它适合你的需要,直接使用框架).
I built mbcache, http://code.google.com/p/mbcache, some time ago to enable caching using dynamic proxies. It handles your questions internally. In the source code there is implementations using Castle Windsor (and LinFu). Take a look at it (or use the framework directly if it suits your needs).
简短回答您的问题
AFAIK - Windsor(或其他代理固件)对此没有任何内置支持.您需要使用一些缓存框架(或者自己构建一些缓存逻辑,即使用弱引用).
AFAIK - Windsor (or other proxy fw) doesn't have any built in support for this. You need to use some cache framework (or build some cache logic yourself, ie using weak references).
和 3. 大多数缓存键建立在字符串上.如果是这样 - 您需要以某种方式将类型、方法和参数转换为字符串.
and 3. Most cache keys are built on strings. If so - you need to convert the type, method and parameters into a string somehow.
这篇关于使用 Castle Windsor 进行 AOP 缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!