Skip to content

About

High performance caching library for Entity Framework Core with automatic cache invalidation

Resources

Stars

3 stars

Watchers

1 watching

Forks

Latest commit

 

History

116 Commits

Folders and files

Repository files navigation

CachedEfCore

Main Build

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

Configuration

    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();

            });
        });
    }

Full Configuration

    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;
                    });
                });
            });
        });
    }

Usage

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;
}

Performance impact

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)

Important

Don't save the instances returned by the cache

About

High performance caching library for Entity Framework Core with automatic cache invalidation

Resources

Stars

3 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages