High-performance in-memory caching solution for .NET 6 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.
$ dotnet add package pvNugsCacheNc6Local
pvNugsCacheNc6Local is a high-performance, thread-safe in-memory caching solution for .NET 6 applications. Built on top of Microsoft.Extensions.Caching.Memory, it provides a simple yet powerful abstraction for storing and retrieving cached data with configurable time-to-live (TTL) settings and comprehensive logging support.
dotnet add package pvNugsCacheNc6Local
{
"PvNugsCacheConfig": {
"DefaultTimeToLive": "00:10:00"
}
}
using pvNugsCacheNc6Local;
var builder = WebApplication.CreateBuilder(args);
// Register the cache service
builder.Services.TryAddPvNugsCacheNc6Local(builder.Configuration);
var app = builder.Build();
using pvNugsCacheNc6Abstractions;
public class MyService
{
private readonly IPvNugsCache _cache;
public MyService(IPvNugsCache cache)
{
_cache = cache;
}
public async Task<User> GetUserAsync(string userId)
{
var cacheKey = $"user:{userId}";
// Try to get from cache
var cachedUser = await _cache.GetAsync<User>(cacheKey);
if (cachedUser != null)
return cachedUser;
// Load from database
var user = await LoadUserFromDatabaseAsync(userId);
// Store in cache with 5-minute TTL
await _cache.SetAsync(cacheKey, user, TimeSpan.FromMinutes(5));
return user;
}
public async Task UpdateUserAsync(User user)
{
await SaveUserToDatabaseAsync(user);
// Invalidate cache
await _cache.RemoveAsync($"user:{user.Id}");
}
}
Task SetAsync<TValue>(
string key,
TValue value,
TimeSpan? timeToLive = null,
CancellationToken cancellationToken = default)
Stores a value in the cache with an optional time-to-live.
Parameters:
key - Unique cache key (required)value - Value to cachetimeToLive - Optional TTL (uses default if not specified)cancellationToken - Cancellation tokenTask<TValue?> GetAsync<TValue>(
string key,
CancellationToken cancellationToken = default)
Retrieves a value from the cache.
Parameters:
key - Cache key to retrievecancellationToken - Cancellation tokenReturns: The cached value or default(TValue) if not found
Task RemoveAsync(
string key,
CancellationToken cancellationToken = default)
Removes a cached item.
Parameters:
key - Cache key to removecancellationToken - Cancellation token| Property | Type | Description |
|---|---|---|
DefaultTimeToLive | TimeSpan? | Default expiration time for cached items. If null, items don't expire automatically. |
{
"PvNugsCacheConfig": {
"DefaultTimeToLive": "01:00:00" // 1 hour default TTL
}
}
The cache service integrates with the pvNugs logging framework to provide trace-level logging:
Example log output:
[Trace] Cache hit for key: user:12345
[Trace] Cache miss for key: product:67890
[Trace] Cache set for key: order:54321 with TTL: 00:05:00
user:123, order:456)GetAsync<T>Contributions are welcome! Please feel free to submit issues or pull requests.
This project is licensed under the MIT License.
Pierre Van Wallendael - pvWay Ltd
For issues, questions, or suggestions:
⭐ If you find this package useful, please consider giving it a star on GitHub!