CachedQueries 1.0.18
See the version list below for details.
dotnet add package CachedQueries --version 1.0.18
NuGet\Install-Package CachedQueries -Version 1.0.18
<PackageReference Include="CachedQueries" Version="1.0.18" />
paket add CachedQueries --version 1.0.18
#r "nuget: CachedQueries, 1.0.18"
// Install CachedQueries as a Cake Addin #addin nuget:?package=CachedQueries&version=1.0.18 // Install CachedQueries as a Cake Tool #tool nuget:?package=CachedQueries&version=1.0.18
CachedQueries: Efficient Caching for Entity Framework Queries
Introduction
CachedQueries is an open-source .NET library for adding efficient caching to Entity Framework queries. It simplifies caching IQueryable results by enabling caching directly within EF, thus eliminating the need for extra abstraction layers over DbSet.
Getting Started
- Install package
dotnet add package CachedQueries
- Dependency Injection Setup (example using memory cache):
// Setup system cache
services.AddMemoryCache();
// Add CachedQueries to your services
services.AddQueriesCaching(options =>
options
.UseCacheStore(MemoryCache)
.UseEntityFramework());
// Use CachedQueries in your application
app.UseQueriesCaching();
- Cache Invalidation Integration
// Extend SaveChanges and SaveChangesAsync methods in EF context
public override async Task<int> SaveChangesAsync(CancellationToken token = default)
{
// Invoke cache expiration
await ChangeTracker.ExpireEntitiesCacheAsync(token);
return await base.SaveChangesAsync(token);
}
Basic Usage
Cache collection
Easily cache collections, including related data:
// Standard caching
var results = await context.Blogs
.Include(x => x.Posts)
.ToCachedListAsync(cancellationToken);
// Caching with expiration
var results = await context.Posts
.ToCachedListAsync(TimeSpan.FromHours(8), cancellationToken);
// Caching with custom tags
var results = await context.Posts
.ToCachedListAsync(TimeSpan.FromHours(8), new List<string> { "custom_tag" }, cancellationToken);
Caching Individual Items
Cache single entities:
// Cache a single entity based on a condition
var result = await context.Blogs
.Include(x => x.Posts)
.CachedFirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken);
// Cache an entity with a predetermined expiration time
var result = await context.Posts
.CachedFirstOrDefaultAsync(TimeSpan.FromHours(8), cancellationToken);
// Cache using custom tags for nuanced control
var result = await context.Posts
.CachedFirstOrDefaultAsync(TimeSpan.FromHours(8), new List<string> { "custom_tag" }, cancellationToken);
Invalidating Cache
CachedQueries efficiently handles cache invalidation, maintaining data accuracy and relevance.
Auto Invalidation
To integrate automatic cache invalidation within CachedQueries, it is efficient to override the EF context SaveChanges
and SaveChangesAsync
methods.
public override async Task<int> SaveChangesAsync(CancellationToken token = default)
{
await ChangeTracker.ExpireEntitiesCacheAsync(token);
return base.SaveChangesAsync(token);
}
Manual Invalidation Using Custom Tags
Control cache updates in case using custom tags:
// Setup the cache invalidator (typically done during initialization)
private ICacheInvalidator _cacheInvalidator;
...
// Invalidate specific cache segments by custom tag
_cacheInvalidator.InvalidateCacheAsync(new List<string> { "custom_tag" })
Use Redis distributed Cache
Use Redis for scalable, distributed caching:
// Setup distributed cache
services.AddDistributedCache();
// Setup Redis
services.AddStackExchangeRedisCache(config);
// Add CachedQueries to your services
services.AddQueriesCaching(options =>
options
.UseCacheStore(DistributedCache)
.UseLockManager<RedisLockManager>()
.UseEntityFramework());
// Use CachedQueries in your application
app.UseQueriesCaching();
More Customization Options
Customize key aspects for specific needs:
- Cache Key Factory: Allows for the implementation of unique logic in generating cache keys.
- Lock Manager: Customization of concurrent access and locking strategies for cache entries.
- Cache Options: Enables the setting and adjustment of global cache settings.
- Cache Invalidator: Provides the capability to devise specific rules for invalidating cache entries.
Custom Dependency Injection example:
services.AddQueriesCaching(options =>
options
.UseOptions(new CacheOptions {
LockTimeout = TimeSpan.FromSeconds(10),
DefaultExpiration = TimeSpan.FromMinutes(30)
})
.UseCacheStore(CustomCacheStore) // ICacheStore implementation
.UseCacheStoreProvider(CustomCacheStoreProvider) // ICacheStoreProvider implementation
.UseCacheInvalidator(CustomCacheInvalidator) // ICacheInvalidator implementation
.UseLockManager(CustomLockManager) // ILockManager implementation
.UseKeyFactory(CustomKeyFactory) // ICacheKeyFactory implementation
.UseEntityFramework()); // Integration with Entity Framework
// Activation of CachedQueries in applications
app.UseQueriesCaching();
Conclusion
Discover more about CachedQueries through the library's test cases, offering insights into detailed functionalities and advanced usage examples.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. 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. |
-
net8.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 |
---|---|---|
2.0.0 | 1,773 | 10/18/2024 |
1.0.18 | 2,640 | 7/5/2024 |
1.0.17 | 334 | 2/7/2023 |
1.0.16 | 74,368 | 1/31/2023 |
1.0.15 | 655 | 1/29/2023 |
1.0.14 | 1,104 | 1/25/2023 |
1.0.13 | 3,849 | 1/5/2023 |
1.0.12 | 4,381 | 12/7/2022 |
1.0.11 | 6,663 | 10/21/2022 |
1.0.10 | 11,213 | 7/14/2022 |
1.0.9 | 1,977 | 7/1/2022 |
1.0.8 | 425 | 7/1/2022 |
1.0.7 | 430 | 7/1/2022 |
1.0.6 | 12,021 | 4/11/2022 |
1.0.5 | 1,865 | 3/4/2022 |
1.0.4 | 734 | 2/24/2022 |
1.0.3 | 433 | 2/22/2022 |
1.0.2 | 436 | 2/22/2022 |
1.0.1 | 499 | 2/22/2022 |
1.0.0 | 536 | 2/22/2022 |