Fingent-InMemory-Caching
1.0.1
dotnet add package Fingent-InMemory-Caching --version 1.0.1
NuGet\Install-Package Fingent-InMemory-Caching -Version 1.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="Fingent-InMemory-Caching" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Fingent-InMemory-Caching" Version="1.0.1" />
<PackageReference Include="Fingent-InMemory-Caching" />
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 Fingent-InMemory-Caching --version 1.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Fingent-InMemory-Caching, 1.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 Fingent-InMemory-Caching@1.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=Fingent-InMemory-Caching&version=1.0.1
#tool nuget:?package=Fingent-InMemory-Caching&version=1.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Fingent In-Memory Caching
A simple and high-performance in-memory caching library for .NET applications. Provides an easy way to store, retrieve, and manage frequently used data in memory, reducing expensive database or API calls and improving overall application performance.
?? Features
- High Performance: Fast in-memory data access with minimal overhead
- Easy Integration: Simple API for caching operations
- Expiration Support: Time-based and sliding expiration policies
- Memory Management: Efficient memory usage with configurable limits
- Thread-Safe: Safe for concurrent access in multi-threaded applications
- Eviction Policies: LRU (Least Recently Used) and size-based eviction
- Cache Statistics: Monitor cache hit/miss ratios and performance metrics
?? Installation
dotnet add package Fingent-InMemory-Caching
?? Dependencies
- .NET 8.0
- Microsoft.Extensions.Caching.Abstractions 8.0.0
- Microsoft.Extensions.Caching.Memory 8.0.1
- Microsoft.Extensions.Logging.Abstractions 8.0.2
?? Usage
Basic Setup
// Program.cs or Startup.cs
services.AddMemoryCaching(options =>
{
options.SizeLimit = 1000;
options.CompactionPercentage = 0.25;
options.ExpirationScanFrequency = TimeSpan.FromMinutes(5);
});
Simple Caching
public class UserService
{
private readonly IMemoryCache _cache;
private readonly ILogger<UserService> _logger;
public UserService(IMemoryCache cache, ILogger<UserService> logger)
{
_cache = cache;
_logger = logger;
}
public async Task<User> GetUserAsync(int id)
{
string cacheKey = $"user_{id}";
if (_cache.TryGetValue(cacheKey, out User cachedUser))
{
_logger.LogInformation("Cache hit for user {UserId}", id);
return cachedUser;
}
_logger.LogInformation("Cache miss for user {UserId}", id);
var user = await GetUserFromDatabaseAsync(id);
// Cache for 30 minutes
_cache.Set(cacheKey, user, TimeSpan.FromMinutes(30));
return user;
}
}
Advanced Caching with Options
public class ProductService
{
private readonly IMemoryCache _cache;
public ProductService(IMemoryCache cache)
{
_cache = cache;
}
public async Task<Product> GetProductAsync(int id)
{
string cacheKey = $"product_{id}";
return await _cache.GetOrCreateAsync(cacheKey, async entry =>
{
// Configure cache entry options
entry.SlidingExpiration = TimeSpan.FromMinutes(15);
entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(1);
entry.Priority = CacheItemPriority.High;
entry.Size = 1;
// Set up cache dependencies
entry.RegisterPostEvictionCallback((key, value, reason, state) =>
{
Console.WriteLine($"Cache entry {key} was evicted due to {reason}");
});
return await GetProductFromDatabaseAsync(id);
});
}
}
Cache with Expiration Policies
public class CacheService
{
private readonly IMemoryCache _cache;
public CacheService(IMemoryCache cache)
{
_cache = cache;
}
// Absolute expiration (expires at specific time)
public void SetWithAbsoluteExpiration<T>(string key, T value, DateTime expireAt)
{
_cache.Set(key, value, expireAt);
}
// Relative expiration (expires after duration)
public void SetWithRelativeExpiration<T>(string key, T value, TimeSpan duration)
{
_cache.Set(key, value, duration);
}
// Sliding expiration (resets on access)
public void SetWithSlidingExpiration<T>(string key, T value, TimeSpan slidingExpiration)
{
var options = new MemoryCacheEntryOptions
{
SlidingExpiration = slidingExpiration
};
_cache.Set(key, value, options);
}
// Never expires (manual removal only)
public void SetNeverExpire<T>(string key, T value)
{
var options = new MemoryCacheEntryOptions
{
Priority = CacheItemPriority.NeverRemove
};
_cache.Set(key, value, options);
}
}
Cache Invalidation
public class CacheManager
{
private readonly IMemoryCache _cache;
private readonly List<string> _cacheKeys;
public CacheManager(IMemoryCache cache)
{
_cache = cache;
_cacheKeys = new List<string>();
}
public void Set<T>(string key, T value, TimeSpan? expiration = null)
{
var options = new MemoryCacheEntryOptions();
if (expiration.HasValue)
{
options.AbsoluteExpirationRelativeToNow = expiration;
}
// Track cache keys for bulk operations
_cacheKeys.Add(key);
_cache.Set(key, value, options);
}
public void Remove(string key)
{
_cache.Remove(key);
_cacheKeys.Remove(key);
}
public void RemoveByPattern(string pattern)
{
var keysToRemove = _cacheKeys.Where(k => k.Contains(pattern)).ToList();
foreach (var key in keysToRemove)
{
_cache.Remove(key);
_cacheKeys.Remove(key);
}
}
public void ClearAll()
{
foreach (var key in _cacheKeys.ToList())
{
_cache.Remove(key);
}
_cacheKeys.Clear();
}
}
Configuration Options
// Advanced configuration
services.AddMemoryCache(options =>
{
// Maximum number of entries
options.SizeLimit = 1000;
// When to compact cache (remove expired entries)
options.CompactionPercentage = 0.25; // Remove 25% when limit reached
// How often to scan for expired entries
options.ExpirationScanFrequency = TimeSpan.FromMinutes(1);
});
// With custom implementation
services.AddSingleton<IMemoryCache>(provider =>
{
return new MemoryCache(new MemoryCacheOptions
{
SizeLimit = 2000,
CompactionPercentage = 0.20
});
});
?? Performance Considerations
Best Practices
- Key Naming: Use consistent, descriptive cache keys
- Size Management: Monitor memory usage and set appropriate limits
- Expiration Strategy: Choose appropriate expiration policies
- Thread Safety: The built-in MemoryCache is thread-safe
Cache Hit Optimization
public class OptimizedCacheService
{
private readonly IMemoryCache _cache;
private readonly SemaphoreSlim _semaphore = new(1, 1);
public async Task<T> GetOrSetAsync<T>(string key, Func<Task<T>> factory, TimeSpan? expiration = null)
{
if (_cache.TryGetValue(key, out T cachedValue))
{
return cachedValue;
}
await _semaphore.WaitAsync();
try
{
// Double-check pattern to prevent multiple factory calls
if (_cache.TryGetValue(key, out cachedValue))
{
return cachedValue;
}
var value = await factory();
var options = new MemoryCacheEntryOptions();
if (expiration.HasValue)
{
options.AbsoluteExpirationRelativeToNow = expiration;
}
_cache.Set(key, value, options);
return value;
}
finally
{
_semaphore.Release();
}
}
}
??? Architecture
The memory caching system provides:
- Fast Access: O(1) lookup performance
- Memory Efficiency: Automatic cleanup and compaction
- Scalability: Configurable size limits and eviction policies
- Monitoring: Built-in cache statistics and callbacks
????? Author
Frebin Francis
Fingent Technology Solutions Pvt Ltd
?? License
Copyright � 2025 Fingent Technology Solutions Pvt Ltd
| 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. net9.0 was computed. 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.
-
net8.0
- Microsoft.Extensions.Caching.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Caching.Memory (>= 8.0.1)
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.2)
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.1 | 352 | 9/24/2025 |