Caching

Last Updated: 2018-10-18

All of the standard repositories have built in support for caching. If enabled, all models are stored in cache before model transformation and invalidated from cache when the model is updated or deleted.

The models are stored in cache before transformation to ensure that they can be used in several output formats, both Typed Models, Dynamic Models and flat Preview Models.

How To Enable Cache

To enable the repository cache, simple register a cache service that implements Piranha.ICache in your Startup.cs. The following cache enables the standard memory cache:

using Piranha;

public void ConfigureServices(IServiceCollection services)
{
...

services.AddSingleton<ICache, Piranha.Cache.MemCache>();

...
}

This will cause the cache to be injected to all repositories when an Api object is created. Most cache services should be registered as singletons as it is desired that all instances reference the same cache object.

Setting The Cache Level

By default Piranha caches EVERYTHING when a Cache Service is registered, but you can configure the cache level by setting the property Piranha.App.CacheLevel. You can choose between the following levels:

CacheLevel.None

Piranha.App.CacheLevel = Piranha.Caching.CacheLevel.None;

Nothing is cached even if a cache service is registered.

CacheLevel.Minimal

Piranha.App.CacheLevel = Piranha.Caching.CacheLevel.Minimal;

The following data is kept in cache:

  • Sites
  • Params

CacheLevel.Basic

Piranha.App.CacheLevel = Piranha.Caching.CacheLevel.Basic;

The following data is kept in cache:

  • Sites
  • Params
  • PageTypes
  • PostTypes

CacheLevel.Full

Piranha.App.CacheLevel = Piranha.Caching.CacheLevel.Full;

Everything is kept in cache, including all Blocks and Fields for Content Types. This is the default cache level.

A Note On Caching

At the moment, all methods in the Piranha.ICache interface are synchronous. The reason for this, even though many available cache implementations are async is simply because almost all of the repositories in Piranha.IApi run their methods synchronously.