Making.MemoryCache
1.0.1-preview
This is a prerelease version of Making.MemoryCache.
dotnet add package Making.MemoryCache --version 1.0.1-preview
NuGet\Install-Package Making.MemoryCache -Version 1.0.1-preview
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="Making.MemoryCache" Version="1.0.1-preview" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Making.MemoryCache" Version="1.0.1-preview" />
<PackageReference Include="Making.MemoryCache" />
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 Making.MemoryCache --version 1.0.1-preview
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Making.MemoryCache, 1.0.1-preview"
#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 Making.MemoryCache@1.0.1-preview
#: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=Making.MemoryCache&version=1.0.1-preview&prerelease
#tool nuget:?package=Making.MemoryCache&version=1.0.1-preview&prerelease
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Making.MemoryCache
In-memory cache implementation for the Making framework.
Overview
Making.MemoryCache provides a robust in-memory caching implementation for the Making framework. It offers high-performance caching with statistics tracking, expiration policies, and seamless integration with the Making caching abstractions.
Features
- In-Memory Caching: Fast, local memory-based caching
- Statistics Tracking: Comprehensive cache performance metrics
- Expiration Policies: Sliding and absolute expiration support
- Pattern-based Removal: Remove cache entries by key patterns
- Thread-Safe: Concurrent access support
- JSON Serialization: Automatic serialization for complex types
Installation
dotnet add package Making.MemoryCache
Usage
Register Services
services.AddMakingMemoryCache();
Basic Caching
public class UserService
{
private readonly IMemoryCacheService _cache;
public UserService(IMemoryCacheService cache)
{
_cache = cache;
}
public async Task<User> GetUserAsync(int userId)
{
var cacheKey = $"user:{userId}";
return await _cache.GetOrSetAsync(cacheKey, async () =>
{
// Fetch from database
return await _userRepository.GetByIdAsync(userId);
}, TimeSpan.FromMinutes(30));
}
public async Task UpdateUserAsync(User user)
{
await _userRepository.UpdateAsync(user);
// Invalidate cache
var cacheKey = $"user:{user.Id}";
await _cache.RemoveAsync(cacheKey);
}
}
Advanced Caching Scenarios
public class ProductService
{
private readonly IMemoryCacheService _cache;
public ProductService(IMemoryCacheService cache)
{
_cache = cache;
}
public async Task<List<Product>> GetProductsByCategoryAsync(int categoryId)
{
var cacheKey = $"products:category:{categoryId}";
return await _cache.GetOrSetAsync(cacheKey, async () =>
{
return await _productRepository.GetByCategoryAsync(categoryId);
}, TimeSpan.FromHours(1));
}
public async Task InvalidateCategoryCache(int categoryId)
{
// Remove all cache entries for this category
await _cache.RemoveByPatternAsync($"products:category:{categoryId}*");
}
public async Task ClearAllProductCache()
{
// Remove all product-related cache entries
await _cache.RemoveByPatternAsync("products:*");
}
}
Cache Statistics and Monitoring
public class CacheMonitoringService
{
private readonly IMemoryCacheService _cache;
private readonly ILogger<CacheMonitoringService> _logger;
public CacheMonitoringService(IMemoryCacheService cache, ILogger<CacheMonitoringService> logger)
{
_cache = cache;
_logger = logger;
}
public async Task LogCacheStatistics()
{
var stats = await _cache.GetStatisticsAsync();
_logger.LogInformation("Cache Statistics: " +
"Hit Count: {HitCount}, " +
"Miss Count: {MissCount}, " +
"Hit Ratio: {HitRatio:P2}, " +
"Total Items: {ItemCount}",
stats.HitCount,
stats.MissCount,
stats.HitRatio,
stats.ItemCount);
}
public async Task<bool> IsCacheHealthy()
{
var stats = await _cache.GetStatisticsAsync();
// Consider cache healthy if hit ratio is above 70%
return stats.HitRatio >= 0.7;
}
}
Configuration Options
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMakingMemoryCache(options =>
{
options.DefaultExpiration = TimeSpan.FromMinutes(30);
options.MaxItemCount = 10000;
options.CompactionPercentage = 0.1; // Remove 10% when limit reached
options.ScanFrequency = TimeSpan.FromMinutes(5);
});
}
}
Custom Serialization
public class ComplexDataService
{
private readonly IMemoryCacheService _cache;
public ComplexDataService(IMemoryCacheService cache)
{
_cache = cache;
}
public async Task<ComplexObject> GetComplexDataAsync(string id)
{
var cacheKey = $"complex:{id}";
return await _cache.GetOrSetAsync(cacheKey, async () =>
{
// Complex computation or external API call
var data = await _externalService.GetDataAsync(id);
return new ComplexObject
{
Id = id,
Data = data,
ProcessedAt = DateTime.UtcNow
};
}, TimeSpan.FromHours(2));
}
}
Cache Warming
public class CacheWarmupService : IHostedService
{
private readonly IMemoryCacheService _cache;
private readonly IProductService _productService;
public CacheWarmupService(IMemoryCacheService cache, IProductService productService)
{
_cache = cache;
_productService = productService;
}
public async Task StartAsync(CancellationToken cancellationToken)
{
// Warm up cache with frequently accessed data
var popularProducts = await _productService.GetPopularProductsAsync();
foreach (var product in popularProducts)
{
var cacheKey = $"product:{product.Id}";
await _cache.SetAsync(cacheKey, product, TimeSpan.FromHours(1));
}
}
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
}
Requirements
- .NET Standard 2.0+
- Microsoft.Extensions.Caching.Memory
- Microsoft.Extensions.Logging.Abstractions
- System.Text.Json
- Making.Core
- Making.MemoryCache.Abstractions
License
This project is part of the Making framework.
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 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.
-
net8.0
- Making.Core (>= 1.0.1-preview)
- Making.MemoryCache.Abstractions (>= 1.0.1-preview)
- Microsoft.Extensions.Caching.Memory (>= 8.0.6)
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.6)
- System.Text.Json (>= 8.0.6)
-
net9.0
- Making.Core (>= 1.0.1-preview)
- Making.MemoryCache.Abstractions (>= 1.0.1-preview)
- Microsoft.Extensions.Caching.Memory (>= 9.0.7)
- Microsoft.Extensions.Logging.Abstractions (>= 9.0.7)
- System.Text.Json (>= 9.0.7)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Making.MemoryCache:
Package | Downloads |
---|---|
Making.MemoryCache.Redis
Redis-based memory cache implementation for the Making framework |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last Updated |
---|---|---|
1.0.1-preview | 318 | 7/25/2025 |
1.0.0-preview | 391 | 7/25/2025 |