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.
public class GetSomething : CacheableQuery<string>;
CacheableQuery<TKey> already implements IQuery<TKey> and adds two properties:
CacheKey — uniquely identifies the cached entry.CacheTimeInSeconds — how long the entry stays valid.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.
See Configuration: Caching for both.