Component View Caching with Castle


I had to add some caching for a ViewCompoment in castle recently and, at least for me, it wasnt too clear what to do at first.

A ViewComponent is a very handy thing in castle, amongst other things it allows you to have  aView Component that can be, a login box, a menu,  an rss feed reader.  Sometimes the you need to cache the viewcomponent and this is how you need to go to this post by hammet 

I couldn’t use the decoration

[ViewComponentDetails("comp1", Cache=ViewComponentCache.Always)]

because I needed the component to be refreshed every so often , so I used my own implementation

[ViewComponentDetails("BlogComponent", Cache = ViewComponentCache.UseCustomCacheKeyGenerator, CacheKeyFactory = typeof(YourCacheKeyGenerator))]

YourCacheKeyGenerator is a class that you create that must implement IViewComponentCacheKeyGenerator something similar to

public class YourCacheKeyGenerator: IViewComponentCacheKeyGenerator
public CacheKey Create(string viewComponentName, IDictionary parameters, IEngineContext context)
CacheKey key = new NamedCacheKey();
..... your implementation here
return key;

I made my implementation dependant on time but you can make it dependant on anything you need

If there are any errors or comments please let me know


9 Responses to “Component View Caching with Castle”

  1. 1 rjlopes

    Hi, you said you made your implementation dependant on time.

    How do you do that? do you generate a different key at each time interval, ex: key_yyyyMMddHH to get the component to cache one hour ?


  2. 2 roundcrisis

    Yes i generate the key for an interval, you can set a parameter in the component that you can then pass on to the implementation of your cache key

    When you create an implementation of IViewComponentCacheKeyGenerator, the create method will have a IDictionary called parameters.

    If you are adding hours or minutes to create a key make sure you return the existing key

    Does this answer yr question?
    I hope so and thanks for drooping by

  3. 3 Rjlopes


    Where do the IViewComponentCacheKeyGenerator parameters come from?

    How can i set them?

  4. 4 roundcrisis
  5. 5 Rjlopes

    I saw that file, but how to I pass parameters to the IViewComponentCacheKeyGenerator?

    I looked at the ViewComponentDetailsAttribute and didn’t saw anything, what is sent to the CacheKeyGenerator in the parameters param?

  6. 6 roundcrisis

    the attributes for your view component class will be something like

    [ViewComponentDetails(“YourComponent”, Cache = ViewComponentCache.UseCustomCacheKeyGenerator, CacheKeyFactory = typeof(YourCacheKey))]

    in the definition you will probably have a parameter defined like this

    [ViewComponentParam(Required = false, Default = “something”)]
    public string yourParameter { get; set; }

    you can then get that from you YourCacheKey like this
    var parameter = parameters[“yourParameter”].ToString();

    Hope it helps



  7. 7 Rjlopes

    Thanks, the documentation of the CacheKeyGenerator didn’t said what was the parameters and i didn’t relate it to the ViewComponentParam.

  8. 8 roundcrisis

    No Problem, glad it helped
    Where are you using castle by the way? Any public site?

  9. 9 Rjlopes

    I used RC3 on a small site as proof of concept and I’m using now the current trunk version on a big site that will run on mono. If you want we can continue this conversation by mail.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: