Last Updated: 2018-12-07

For production scenarios we recommend a cache level of at least Basic to acheive maximum performance. This cache level will hold all meta-data in cache and avoid unneccesary roundtrips to the database.

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:


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

Nothing is cached even if a cache service is registered.


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

The following data is kept in cache:

  • Sites
  • Params


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

The following data is kept in cache:

  • Sites
  • Params
  • PageTypes
  • PostTypes


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.