Lore.QueryCache

A library provides IQueryable results caching using IMemoryCache or IDistributedCache.

Setup

Setup with DI

services.AddLoreCache(options =>
    options
        .UseCache<DistributedCache>()
        .UseEntityFramework());

...

app.UseLoreCache();

Or Setup with static class

CacheManager.Cache = new MyCacheImplementation();
CacheManager.CacheKeyFactory = new MyCacheKeyFactory();

Usage

By default, all types from Include and ThenInclude methods are used as cache tags.

Cache keys are generated by CacheKeyFactory class implementation.

Cache collection

var results = context.Blogs
    .Include(x => x.Posts)
    .ToCachedListAsync(cancellationToken);

Cache item

var results = context.Blogs
    .Include(x => x.Posts)
    .Where(x => x.Id == request.Id)
    .CachedFirstOrDefault(cancellationToken);

Invalidate cache

By EF change detector extension. It invalidates all cache linked to default query tags.

await context.ChangeTracker.ExpireEntitiesCacheAsync();

Manual cache invalidation

CacheManager.ExpireTagsAsync(new List<string> { "tag_1" });