CachedQueries 1.0.17

dotnet add package CachedQueries --version 1.0.17
NuGet\Install-Package CachedQueries -Version 1.0.17
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="CachedQueries" Version="1.0.17" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add CachedQueries --version 1.0.17
#r "nuget: CachedQueries, 1.0.17"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install CachedQueries as a Cake Addin
#addin nuget:?package=CachedQueries&version=1.0.17

// Install CachedQueries as a Cake Tool
#tool nuget:?package=CachedQueries&version=1.0.17

CachedQueries

License NuGet version (CachedQueries) NuGet downloads (CachedQueries) Build status Coverage Status CodeFactor

A library provides IQueryable results caching with smart invalidation.

The main goal is to bring caching to ef queries with flexible invalidation without any abstraction over DbSet.

For example:

    await context.Customers
        .Query(request)
        .ToCachedListAsync(cancellationToken);
        
    await context.Customers
        .FindById(id)
        .CachedFirstOrDefaultAsync(cancellationToken);

Query and FindById extensions could contain filters, include related entities, etc. Using these queries in different places still returns cached results until the Customer entity will be modified.

Setup

Setup with DI

// services is IServicesCollection
services.AddQueriesCaching(options =>
    options.UseEntityFramework());

...
// app is IApplicationBuilder
app.UseQueriesCaching();

Options also allows to define own implementations of cache key factory, lock manager, cache options, cache invalidator.

Usage

Cache collection

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

// with expiration
var results = context.Posts
    .ToCachedListAsync(Timespan.FromHours(8), cancellationToken);
    
// with custom tags
var results = context.Posts
    .ToCachedListAsync(Timespan.FromHours(8), new List<string> { "all" }, cancellationToken);

Cache item

var result = context.Blogs
    .Include(x => x.Posts)
    .CachedFirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken);

// with expiration
var result = context.Posts
    .CachedFirstOrDefaultAsync(Timespan.FromHours(8), cancellationToken);
    
// with custom tags
var result = context.Posts
    .CachedFirstOrDefaultAsync(Timespan.FromHours(8), new List<string> { "all" }, cancellationToken);

Invalidate cache

By default, all invalidation tags are retrieved from Include and ThenInclude as navigation properties type names. To invalidate the cache just call this extension before context save changes.

await context.ChangeTracker.ExpireEntitiesCacheAsync();

For more details pls take a look on the tests =)

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net7.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.17 291 2/7/2023
1.0.16 68,594 1/31/2023
1.0.15 621 1/29/2023
1.0.14 1,076 1/25/2023
1.0.13 3,812 1/5/2023
1.0.12 4,349 12/7/2022
1.0.11 6,635 10/21/2022
1.0.10 11,180 7/14/2022
1.0.9 1,939 7/1/2022
1.0.8 397 7/1/2022
1.0.7 402 7/1/2022
1.0.6 11,991 4/11/2022
1.0.5 1,837 3/4/2022
1.0.4 696 2/24/2022
1.0.3 403 2/22/2022
1.0.2 402 2/22/2022
1.0.1 461 2/22/2022
1.0.0 495 2/22/2022