Skip to content

6. Customization

Vladyslav Lobyntsev edited this page Mar 5, 2024 · 2 revisions

To use custom cache implementation instead of in-memory or distributed cache

  1. Implement the following interface
public interface ICacheImplementation
{
    ValueTask<TEntity?> RetrieveAsync<TEntity>(string key);

    ValueTask CacheAsync<TEntity>(string key, TEntity entity, CacheOptions options) where TEntity : notnull;

    ValueTask RemoveAsync(string key);
}
  1. Call StoreIn with the custom cache implementation instance to configure the custom cache implementation for the particular entity, or SetGenericCache to configure the custom cache implementation globally.

To use a custom distributed cache storage format (JSON is used by default) for a particular entity (or for many entities, depending on CanBeUsedForType implementation)

  1. Implement the following interface
public interface IDistributedCacheSerializer
{
    ValueTask<byte[]> SerializeAsync<TEntity>(TEntity entity);

    ValueTask<TEntity?> DeserializeAsync<TEntity>(byte[] bytes);

    bool CanBeUsedForType(Type type);
}
  1. For non-ASP.NET applications, pass the serializer(s) object to StoreInDistributedCache or SetDistributedAsDefaultCache method.
  2. For ASP.NET application, register serializer(s) in ASP.NET DI container.
Clone this wiki locally