Default Keys - Jaxelr/Nancy.RapidCache GitHub Wiki

By default, the cache uses the base url path as a key for returning an object from the cache, so for example the following urls:

    http://localhost:5000/api/mypath
    http://localhost:5000/api/mypath?option=affirmative
    http://localhost:5000/api/mypath?option=affirmative&effort=low

Are equally cached as http://localhost:5000/api/mypath since the query strings are not part of the base url. Certainly, this is not suitable for REST URIs, but we can easily adapt to this situation using the Default Key Generator:

using Nancy.RapidCache.Extensions;
using Nancy.Routing;
using Nancy.TinyIoc;
using Nancy.Bootstrapper;

namespace MyCustomWebApp
{
    public class ApplicationBootrapper : DefaultNancyBootstrapper
    {
        protected override void ApplicationStartup
        (TinyIoCContainer container, IPipelines pipelines)
        {
            base.ApplicationStartup(container, pipelines);
            this.EnableRapidCache(container.Resolve<IRouteResolver>(), 
                 ApplicationPipelines, new[] { "query" });
        }
    }
}

This way, we can easily cache specifically by different (optional) query strings added.

Caching while using content negotiation on NancyFx

Nancy has the capability of allowing us to use Content Negotiation to return a response serialized as requested on the Accept header. See here for more details...

But the way rapid cache is currently configured, it doesn't validate the Accept headers to create a filter key, therefore adding Accept headers of application/json or application/xml to our requests will be equated. To solve this a declaration of accept is allowed on the Default key generator:

using Nancy.RapidCache.Extensions;
using Nancy.Routing;
using Nancy.TinyIoc;
using Nancy.Bootstrapper;

namespace MyCustomWebApp
{
    public class ApplicationBootrapper : DefaultNancyBootstrapper
    {
        protected override void ApplicationStartup
        (TinyIoCContainer container, IPipelines pipelines)
        {
            base.ApplicationStartup(container, pipelines, new[] { "accept" });
            this.EnableRapidCache(container.Resolve<IRouteResolver>(), 
                 ApplicationPipelines);
        }
    }
}

Default Keys options

These are the options provided by the default cache key that can be combined to generate a unique resource based on the granularity described:

Option Description
query The query string optional values provided
accept the accept headers included on the requests
form the form posted from the request (useful for webpages)

If these options are not detailed enough for the requests given, there is always the possibility to extend the ICacheKeyGenerator to use the desired request information. See this link