Persistence Caching
Caching persistence data in RCommon
Occasionally there is the need to cache data that is coming from your persistence layer so that you can avoid hitting your data store repeatedly for data that is commonly used. RCommon provides a proxy for all repository implementations available in RCommon which allows you to use the repository pattern in conjunction with caching services.
Configuration
var host = Host.CreateDefaultBuilder(args)
.ConfigureServices(services =>
{
// Configure RCommon
services.AddRCommon()
.WithPersistence<EFCorePerisistenceBuilder>(ef => // Repository/ORM configuration. We could easily swap out to Linq2Db without impact to domain service up through the stack
{
// Add all the DbContexts here
ef.AddDbContext<TestDbContext>("TestDbContext", ef =>
{
ef.UseSqlServer(config.GetConnectionString("TestDbContext"));
});
ef.AddInMemoryPersistenceCaching(); // This gives us access to the caching repository interfaces/implementations
})
// You still need to configure this as well!
.WithMemoryCaching<InMemoryCachingBuilder>(cache =>
{
cache.Configure(x =>
{
x.ExpirationScanFrequency = TimeSpan.FromMinutes(1);
});
});
}).Build();
It is still important that you configure caching in addition to adding persistence caching since that is what implements the caching abstractions.
Usage
Behind the Scenes
This is what is happening behind the scenes in the caching repository proxy
Last updated