Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Mark 122 posts 255 karma points
    Mar 05, 2019 @ 19:04
    Mark
    0

    Outputcache in a multi-tenant site

    Guys,

    Looking at implementing some simple output caching on a multi tenant site (sites share the same codebase) and am wondering if people have a tried and tested way of doing this..

    I have created a simple cache profile that varies by both url (varyByCustom) and by host (varyByHeader, to handle multiple root paths ['/']). This works lovely, and is nice and simple, but.. I want to be able to clear a URL from the cache when a node is published (preferably ONLY the URL of the updated node, I am not even sure you can blat the entire output cache in one hit?)

    Now, I have a method attached to the

    PageCacheRefresher.CacheUpdated 
    

    event, this is called just fine.. but.. how do I remove the correct item from the cache. Normally you would call:

    HttpResponse.RemoveOutputCacheItem(myAbsoluteVirtualPath);
    

    But, if saving a node with a different hostname, not sure how to go about clearing it, as RemoveOutputCacheItem only wants a virtual path, and I can't seem to target a specific hostname (at least I am unaware of how to..)

    If there's a better package to use that I can do what I'd need, then would love to hear about it. Or am I doomed to roll my own solution, ignoring outputcache entirely and talking directly to the cache?

    All/any ideas welcome!

    Thanks,

    Mark

  • Jason Elkin 38 posts 351 karma points MVP 2x c-trib
    Mar 06, 2019 @ 10:22
    Jason Elkin
    1

    WRT Packages, take a look at MvCDonutCaching it will solve a lot of problems that you are about to have with things like navigation etc.

    Paul Seal has a blog post on it with examples: https://codeshare.co.uk/blog/how-to-use-donut-caching-in-umbraco-and-mvc/

    There's some example code from Dave Woestenborghs here too: https://gist.github.com/dawoe/f8d649c09f4f6341ea9630acbab44cf0

    The key thing is how you construct the varyByCustom string. This gets inserted into the caching key. You can put any (string) data in here that you want then use it to selectively clear the cache later.

    At OnActionExecuting you have access to UmbracoContext.Current.PublishedContentRequest.PublishedContent so you can use any properties you want from the document to selectively clear the cache. At the bare minimum I store the document Id and the Path (so you can clear parents and/or children as well).

    I ultimately went down the route of writing a wrapper for the output cache that stores and parses small JSON encoded PoCOs in the varyByCustom string. We used JSON as it actually turned out quicker than processing the cache keys with methods like Contains int.TryParse(s) etc.

Please Sign in or register to post replies

Write your reply to:

Draft