AOP缓存与温莎城堡缓存、城堡、温莎、AOP

2023-09-04 23:31:10 作者:萌比女汉子

任何人都可以提供如何缓存与温莎城堡将努力工作的例子。

Can anyone provide a working example of how caching with Castle Windsor would work.

我presume为出发点,我定义我的 CacheAspect 继承自IInterceptor如下:

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 ...

    [Interceptor(typeof(CacheAspect))]
    public List<string> GetStaticData()
    {
    }

......,当然登记在温莎容器整个事情。

.. and of course register the whole thing in the Windsor container.

不过...

我如何可以改变的时候,我想要的东西在我的每个方法调用缓存中的量?在这个例子中,我可能希望它被缓存60分钟。对于其他的例子一天等等等等,我必须创建一个 CacheAspect 每个高速缓存的时间?

How can I vary the amount of time I want something in my cache per method call? In this example I may want it to be cached for 60 minutes. For other examples for a day etc etc. Do I have to create a CacheAspect for each cache duration?

什么是确定每个方法每个缓存价值的最佳方法是什么?使用 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 ://$c$c.google.com/p/mbcache ,前一段时间使用动态代理启用缓存。它在内部处理你的问题。在源$ C ​​$ C有使用温莎城堡(与李林甫)实现。看看它(或直接使用该框架,如果它适合你的需要)。

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 - 温莎(或其他代理FW)没有任何内置该支持。你需要使用一些缓存框架(或建立使用弱引用一些缓存自己的逻辑,即)。

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.

 
精彩推荐