问题
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...
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?What is the best way to identify each cached value from each method? Using a combination of
invocation.TargetType.Name
andinvocation.Method.Name
for example?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
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).
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