I am trying to add the Azure Redis Cache to my app. I am trying to do it with the interceptor, so when a query is done if the result is present in the cache I replace the result and I do one less query, otherwise I get the result of the query and I store it in the cache. Of course I am doing it only when certain table are involved. I have added the Redis Cache to my startup:
services.AddDistributedRedisCache(config =>
{
config.Configuration = GetAppSettingsValue("RedisConnectionString", Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"));
});
Now I need to use it in my interceptor:
public class RedisCacheInterceptor : DbCommandInterceptor
{
private readonly IDistributedCache _distributedCache;
public RedisCacheInterceptor(IDistributedCache distributedCache)
{
_distributedCache = distributedCache;
}
}
At this point when I try to add the interceptor to the db connection I have no idea on how to instantiate the interceptor using the dependency injection.
How should I solve this problem? Am I using the interceptor in the wrong way?
This doesn't answer your question to use an interceptor, but maybe this pattern may help. Review this code:
It's a function I call to get 'something' from Redis Cache. I also pass a initialization function to call when the data is not present. So basically:
See this demo repo containing this very example:
https://github.com/4DotNet/WebCasts-Caching