AOP Caching with Castle Windsor

偶尔善良 提交于 2019-12-08 10:19:30

问题


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

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

I can then decorate any method with my CacheAspect...

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

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

However...

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

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

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

Thanks.


回答1:


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

Short answers to your questions

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

  2. and 3. Most cache keys are built on strings. If so - you need to convert the type, method and parameters into a string somehow.



来源:https://stackoverflow.com/questions/10155555/aop-caching-with-castle-windsor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!