Nov 5, 2010 at 6:45 PM

I've created a module that queries a feed to work out what the weather is.

I want to cache the response. I know there is an Orchard Cache API, but I was wondering if there was a good bit of code to look at for an example of its use.

Richard Garside.

Nov 5, 2010 at 7:50 PM

Hi Richard,

Here is an example from the localization files:

private IEnumerable<CultureDictionary> LoadCultures() {
    return _cacheManager.Get("cultures", ctx => {
        var cultures = new List<CultureDictionary>();
        foreach (var culture in _cultureManager.ListCultures()) {
            cultures.Add(new CultureDictionary {
                CultureName = culture,
                Translations = LoadTranslationsForCulture(culture, ctx)
        return cultures;


You need to resolve an ICacheManager through IoC. Then you just call Get(). The first parameter is the unique Key for the cached content. The second one is a lambda which will get called whenever the cache if invalid, like the first time it is called. This will also be the result of the Get().

Inside this lambda you also can use the ctx to define what strategy will be used to invalidate this result. Calling ctx.Monitor() will require an intance of IVolatileToken. There are a couple of providers right now, like one based on file changes. To use it you can inject an IWebSiteFolder instance, and do something like this: context.Monitor(_webSiteFolder.WhenPathChanges(virtualPath));

The first example uses an instance of ISignal to define what whill cause the cache to be invalid. This one lets you create a signal from any other part of the code, like your services. A signal just needs a name, and you call Trigger to sent it.

Nov 6, 2010 at 6:23 AM

We should really have _clock be a source of a volatile token, too.

Nov 8, 2010 at 9:54 AM

Does this mean there isn't currently a way to invalidate a cache based on time? What would the best cache validation strategy be for results from web feeds?

Also, I can't work out how to inject an ICacheManager into a content part driver.


Nov 8, 2010 at 2:51 PM

Can you open a issue on codeplex so we can track this need ? It shouldn't be hard to do, if you look at WhenPathChanges implementation, the same behavior can be done for timing. And it would also be very simple to create an implementation based on IClock.

About injecting an ICacheManager, did you try to create a constructor and to define it as an argument ? Like other services ?

Nov 8, 2010 at 3:03 PM

I've created a work item for this.

Managed to get the IOC injection working. I'd been foolishly trying to add it straight to the display method parameters rather than the constructor.

Nov 9, 2010 at 8:09 PM

This is implemented now