CachedEfCore is a caching library for entity framework core
The cache of CachedEfCore is always the lastest version of the object cached, the library auto invalidates the cache when some entity related to the cached entity changes state, so is impossible to get an old cache
public void ConfigureServices(IServiceCollection services)
{
services.AddCachedEfCore();
// AddDbContextPool or AddDbContext
services.AddDbContextPool<AppDbContext>(options =>
{
options.UseSqlServer();
options.UseCachedEfCore(cachedEfCoreOptions =>
{
// SQL Server
cachedEfCoreOptions.UseSqlServer();
// PostgreSQL
// cachedEfCoreOptions.UsePostgreSql();
// Any other database
// cachedEfCoreOptions.UseGenericProvider()
cachedEfCoreOptions.UseInMemoryCacheStore();
});
});
} public void ConfigureServices(IServiceCollection services)
{
services.AddCachedEfCore();
// AddDbContextPool or AddDbContext
services.AddDbContextPool<AppDbContext>(options =>
{
options.UseSqlServer();
options.UseCachedEfCore(cachedEfCoreOptions =>
{
// SQL Server
cachedEfCoreOptions.UseSqlServer();
// PostgreSQL
// cachedEfCoreOptions.UsePostgreSql();
// Any other database
// cachedEfCoreOptions.UseGenericProvider()
cachedEfCoreOptions.UseInMemoryCacheStore();
cachedEfCoreOptions.ConfigureKeyGeneration(keyGen =>
{
keyGen.ConfigureNonEvaluableTypes(originals =>
{
originals.Add(typeof(SomeType));
return originals;
});
keyGen.ConfigureJsonSerializer(original =>
{
var newOptions = new JsonSerializerOptions();
return newOptions;
});
});
});
});
}public IEnumerable<TResult> WhereSelect<TResult>(Expression<Func<T, bool>> where, Expression<Func<T, TResult>> selector)
{
var result = _dbQueryCacheHelper.GetOrAdd<IEnumerable<TResult>, T>(_dbContext, () => _dbContext.Entity.Where(where).Select(selector).ToList(), [where, selector]);
return result;
}
public async Task<IEnumerable<TResult>> WhereSelectAsync<TResult>(Expression<Func<T, bool>> where, Expression<Func<T, TResult>> selector)
{
var result = await _dbQueryCacheHelper.GetOrAddAsync<IEnumerable<TResult>, T>(_dbContext, async () => await _dbContext.Entity.Where(where).Select(selector).ToListAsync(), [where, selector]);
return result;
}On my tests in a url shortner api: Without CachedEfCore I was getting 8k requests per second, and then when enabling the CachedEfCore i'm getting 107k requests per seconds improving over 13x the performance with less resource usage (yes LESS resources)
Don't save the instances returned by the cache