servicestackservicestack-razor

ServiceStack Caching No Ceremony Razor


I have a simple website that is using the no ceremony razor views mostly for handling the layouts of a bunch of static pages. This is on an azure web app. The pages seem to load a little slower than I like (Azure has slow I/O), I'm wondering if there is a way to cache all these responses as they don't really need to be processed at all after compilation.


Solution

  • The no ceremony razor pages are executed directly and don't go through the normal request pipeline so there's no opportunity to cache their outputs.

    You'd need to do something like converting it to a Razor View by moving it to the /Views folder, create a Request DTO with the same name as the page, then add a [CacheResponse] attribute on the Service like:

    [Route("/pagename1")] class PageName1 {}
    [Route("/pagename2")] class PageName2 {}
    
    [CacheResponse(Duration = 3600)]
    public class HtmlServices : Service
    {
        public object Any(PageName1 request) => request;
        public object Any(PageName2 request) => request;
    }
    

    Which will cache the HTML Response outputs for the above pages for 1 hour.