This sample demonstrates how to implement and register a custom IFileCache provider, replacing the built-in local or in-memory cache with your own caching logic.
- Implementing the
IFileCacheinterface with a customConcurrentDictionaryFileCache - Registering the custom cache provider after
AddGroupDocsViewerSelfHostApi()to override the default - In-memory caching using
ConcurrentDictionaryas the backing store
- .NET 8.0 SDK or later
cd samples/GroupDocs.Viewer.UI.Sample.CustomCacheProvider
dotnet runOpen your browser at https://localhost:5001 (or the URL shown in the console output).
The custom cache is registered as a singleton after the self-host API setup:
builder.Services
.AddControllers()
.AddGroupDocsViewerSelfHostApi(config => { ... })
.AddLocalStorage("./Files");
// Register custom cache AFTER AddGroupDocsViewerSelfHostApi()
builder.Services.AddSingleton<IFileCache, ConcurrentDictionaryFileCache>();Important: The custom IFileCache registration must come after AddGroupDocsViewerSelfHostApi() to override the default cache implementation.
The IFileCache interface requires four methods:
public interface IFileCache
{
TEntry TryGetValue<TEntry>(string cacheKey, string filePath);
Task<TEntry> TryGetValueAsync<TEntry>(string cacheKey, string filePath);
void Set<TEntry>(string cacheKey, string filePath, TEntry entry);
Task SetAsync<TEntry>(string cacheKey, string filePath, TEntry entry);
}This pattern can be adapted to use Redis, database, or any other caching backend.