pvNugsCacheNc9Abstractions 9.0.1
dotnet add package pvNugsCacheNc9Abstractions --version 9.0.1
NuGet\Install-Package pvNugsCacheNc9Abstractions -Version 9.0.1
<PackageReference Include="pvNugsCacheNc9Abstractions" Version="9.0.1" />
<PackageVersion Include="pvNugsCacheNc9Abstractions" Version="9.0.1" />
<PackageReference Include="pvNugsCacheNc9Abstractions" />
paket add pvNugsCacheNc9Abstractions --version 9.0.1
#r "nuget: pvNugsCacheNc9Abstractions, 9.0.1"
#:package pvNugsCacheNc9Abstractions@9.0.1
#addin nuget:?package=pvNugsCacheNc9Abstractions&version=9.0.1
#tool nuget:?package=pvNugsCacheNc9Abstractions&version=9.0.1
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
CancellationTokensupport - 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. |
-
net9.0
- pvNugsLoggerNc9Abstractions (>= 9.1.0)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on pvNugsCacheNc9Abstractions:
| Package | Downloads |
|---|---|
|
pvNugsCacheNc9Local
High-performance in-memory caching solution for .NET 9 applications. Built on Microsoft.Extensions.Caching.Memory, it provides thread-safe cache operations with configurable TTL, integrated logging, and async/await support. Simple dependency injection setup for local caching needs. |
|
|
pvNugsSecretManagerNc9Azure
Package Description |
GitHub repositories
This package is not used by any popular GitHub repositories.
Aligned with logger abstraction version 9.1.0