pvNugsCacheNc9Abstractions 9.0.1
dotnet add package pvNugsCacheNc9Abstractions --version 9.0.1
NuGet\Install-Package pvNugsCacheNc9Abstractions -Version 9.0.1
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="pvNugsCacheNc9Abstractions" Version="9.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="pvNugsCacheNc9Abstractions" Version="9.0.1" />
<PackageReference Include="pvNugsCacheNc9Abstractions" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add pvNugsCacheNc9Abstractions --version 9.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: pvNugsCacheNc9Abstractions, 9.0.1"
#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.
#:package pvNugsCacheNc9Abstractions@9.0.1
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=pvNugsCacheNc9Abstractions&version=9.0.1
#tool nuget:?package=pvNugsCacheNc9Abstractions&version=9.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
pvNugsCacheNc9Abstractions
A lightweight .NET caching abstraction library that provides a simple, async-first interface for caching operations with optional time-to-live support.
Features
- Async-First Design: All operations are asynchronous with
CancellationToken
support - Generic Type Support: Store and retrieve any type of data with full type safety
- Flexible Expiration: Optional time-to-live (TTL) configuration per cache entry
- Simple Interface: Clean, minimal API surface with just three core operations
- Framework Agnostic: Pure abstraction with no dependencies on specific cache implementations
Installation
bash
dotnet add package pvNugsCacheNc9Abstractions
Quick Start
Basic Usage
csharp
public class UserService
{
private readonly IPvNugsCache _cache;
public UserService(IPvNugsCache cache)
{
_cache = cache;
}
public async Task<User?> GetUserAsync(int userId, CancellationToken ct = default)
{
// Try to get from cache first
var cachedUser = await _cache.GetAsync<User>($"user:{userId}", ct);
if (cachedUser != null)
return cachedUser;
// Fetch from database if not in cache
var user = await FetchUserFromDatabase(userId, ct);
// Cache for 30 minutes
await _cache.SetAsync($"user:{userId}", user, TimeSpan.FromMinutes(30), ct);
return user;
}
}
Dependency Injection Setup
// Register your cache implementation
services.AddSingleton<IPvNugsCache, YourCacheImplementation>();
// Or use with existing cache providers
services.AddMemoryCache();
services.AddSingleton<IPvNugsCache, MemoryCacheAdapter>();
API Reference
IPvNugsCache Interface
SetAsync<TValue>(string key, TValue value, TimeSpan? timeToLive, CancellationToken cancellationToken)
Stores a value in the cache with an optional expiration time.
- key: Unique identifier for the cached value
- value: The data to cache
- timeToLive: Optional expiration time (null uses default policy)
- cancellationToken: Cancellation token for the operation
GetAsync<TValue>(string key, CancellationToken cancellationToken)
Retrieves a value from the cache.
- key: Unique identifier for the cached value
- cancellationToken: Cancellation token for the operation
- Returns: The cached value or null if not found/expired
RemoveAsync(string key, CancellationToken cancellationToken)
Removes a value from the cache.
- key: Unique identifier for the value to remove
- cancellationToken: Cancellation token for the operation
Usage Examples
Caching with Different TTL Values
// Short-lived cache (5 minutes)
await cache.SetAsync("temp-data", tempData, TimeSpan.FromMinutes(5));
// Long-lived cache (1 hour)
await cache.SetAsync("config", configuration, TimeSpan.FromHours(1));
// Use default TTL policy
await cache.SetAsync("default-ttl", data);
Type-Safe Caching
// Cache complex objects
var user = new User { Id = 1, Name = "John Doe" };
await cache.SetAsync("user:1", user, TimeSpan.FromMinutes(30));
// Retrieve with type safety
var cachedUser = await cache.GetAsync<User>("user:1");
// Cache collections
var users = new List<User> { user1, user2, user3 };
await cache.SetAsync("users:active", users, TimeSpan.FromMinutes(15));
Cache Invalidation
public async Task UpdateUserAsync(User user)
{
// Update in database
await SaveUserToDatabase(user);
// Invalidate cache
await _cache.RemoveAsync($"user:{user.Id}");
// Optionally refresh cache
await _cache.SetAsync($"user:{user.Id}", user, TimeSpan.FromMinutes(30));
}
Implementation Guidelines
When implementing IPvNugsCache
, consider:
- Thread Safety: Ensure your implementation is thread-safe
- Serialization: Handle serialization/deserialization of complex types
- Error Handling: Gracefully handle cache failures
- Memory Management: Implement proper cleanup and eviction policies
- Monitoring: Add logging and metrics for cache operations
Compatible Frameworks
- .NET 6.0+
- .NET Core 3.1+
- .NET Framework 4.8+ (with nullable reference types support)
License
MIT
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net9.0
- pvNugsLoggerNc9Abstractions (>= 9.1.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on pvNugsCacheNc9Abstractions:
Package | Downloads |
---|---|
pvNugsSecretManagerNc9Azure
Package Description |
GitHub repositories
This package is not used by any popular GitHub repositories.
Aligned with logger abstraction version 9.1.0