Memoria

Cache query results

Memoria can cache query results automatically. Inherit from CacheableQuery<TKey>, supply a cache key and TTL on the query instance, and the dispatcher returns the cached value when one exists.

This guide assumes a caching provider is registered — see Configuration: Caching.

Make the query cacheable

public class GetSomething : CacheableQuery<string>;

CacheableQuery<TKey> already implements IQuery<TKey> and adds two properties:

Set the key and TTL at dispatch

var result = await dispatcher.Get(new GetSomething
{
    CacheKey = "product:123",
    CacheTimeInSeconds = 600
});

The first call runs the handler and stores the result; subsequent calls within the TTL return the cached value without invoking the handler.

Picking the cache provider

See Configuration: Caching for both.