RepletoryLib.Caching.InMemory
1.0.0
dotnet add package RepletoryLib.Caching.InMemory --version 1.0.0
NuGet\Install-Package RepletoryLib.Caching.InMemory -Version 1.0.0
<PackageReference Include="RepletoryLib.Caching.InMemory" Version="1.0.0" />
<PackageVersion Include="RepletoryLib.Caching.InMemory" Version="1.0.0" />
<PackageReference Include="RepletoryLib.Caching.InMemory" />
paket add RepletoryLib.Caching.InMemory --version 1.0.0
#r "nuget: RepletoryLib.Caching.InMemory, 1.0.0"
#:package RepletoryLib.Caching.InMemory@1.0.0
#addin nuget:?package=RepletoryLib.Caching.InMemory&version=1.0.0
#tool nuget:?package=RepletoryLib.Caching.InMemory&version=1.0.0
RepletoryLib.Caching.InMemory
In-memory implementation of ICacheService and IDistributedLockService for single-instance applications.
Part of the RepletoryLib ecosystem -- standalone, reusable .NET 10 libraries with zero business logic.
Overview
RepletoryLib.Caching.InMemory provides an in-memory implementation of ICacheService and IDistributedLockService from RepletoryLib.Caching.Abstractions. It uses Microsoft.Extensions.Caching.Memory.IMemoryCache under the hood, providing fast, zero-latency caching without any external infrastructure.
This is ideal for single-instance applications, development environments, and unit testing. For distributed scenarios, use RepletoryLib.Caching.Redis or RepletoryLib.Caching.Hybrid.
Key Features
- Zero infrastructure -- No external dependencies like Redis; runs entirely in-process
- Full
ICacheService-- Get, set, remove, prefix eviction, exists, and cache-aside pattern - In-memory locking --
IDistributedLockServiceimplementation with automatic expiry (single-instance only) - Configurable -- Default expiry, key prefix, and compression via
CacheOptions
Installation
dotnet add package RepletoryLib.Caching.InMemory
Or add to your .csproj:
<PackageReference Include="RepletoryLib.Caching.InMemory" Version="1.0.0" />
Note: RepletoryLib packages are published to a local BaGet feed. See the main repository README for feed configuration.
Dependencies
| Package | Type |
|---|---|
RepletoryLib.Caching.Abstractions |
RepletoryLib |
Microsoft.Extensions.Caching.Memory |
NuGet (10.0.0) |
Quick Start
using RepletoryLib.Caching.InMemory;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddRepletoryInMemoryCache(builder.Configuration);
{
"RepletoryCache": {
"DefaultExpiryMinutes": 60,
"KeyPrefix": "myapp:",
"EnableCompression": false
}
}
That's it. ICacheService and IDistributedLockService are now available for injection.
Usage Examples
Basic Caching
using RepletoryLib.Caching.Abstractions.Interfaces;
public class UserService
{
private readonly ICacheService _cache;
public UserService(ICacheService cache) => _cache = cache;
public async Task<User?> GetUserAsync(Guid id)
{
return await _cache.GetOrSetAsync($"user:{id}", async () =>
{
return await _repository.GetByIdAsync(id);
}, TimeSpan.FromMinutes(15));
}
public async Task InvalidateUserAsync(Guid id)
{
await _cache.RemoveAsync($"user:{id}");
}
public async Task InvalidateAllUsersAsync()
{
await _cache.RemoveByPrefixAsync("user:");
}
}
Swapping to Redis Later
Because you program against ICacheService, switching from in-memory to Redis requires only changing the registration:
// Development
builder.Services.AddRepletoryInMemoryCache(builder.Configuration);
// Production -- just change this line, no business logic changes
// builder.Services.AddRepletoryRedis(builder.Configuration);
In-Memory Locking
using RepletoryLib.Caching.Abstractions.Interfaces;
public class ImportService
{
private readonly IDistributedLockService _locks;
public ImportService(IDistributedLockService locks) => _locks = locks;
public async Task RunImportAsync()
{
var lockId = await _locks.AcquireAsync("import-lock", TimeSpan.FromMinutes(5));
if (lockId is null)
{
Console.WriteLine("Import already running");
return;
}
try
{
await PerformImportAsync();
}
finally
{
await _locks.ReleaseAsync(lockId);
}
}
}
Note: In-memory locks only work within a single application instance. For distributed locking across multiple instances, use
RepletoryLib.Caching.Redis.
Integration with Other RepletoryLib Packages
| Package | Relationship |
|---|---|
RepletoryLib.Caching.Abstractions |
Implements ICacheService and IDistributedLockService |
RepletoryLib.Caching.Redis |
Alternative for distributed caching |
RepletoryLib.Caching.Hybrid |
Uses InMemory as the L1 cache layer |
RepletoryLib.Auth.Jwt |
Provides cache backend for JWT token blacklist |
RepletoryLib.Testing |
MockCacheService provides a test double |
Testing
For unit tests, use MockCacheService from RepletoryLib.Testing instead of the real InMemoryCacheService. The mock provides assertion helpers like GetCalls, SetCalls, and RemoveCalls.
For integration tests that need real caching behavior, register AddRepletoryInMemoryCache in your test service collection.
Troubleshooting
| Issue | Solution |
|---|---|
| Cache not returning values | Check that the key matches exactly, including the configured KeyPrefix |
| Memory growing unbounded | Ensure you set appropriate expiry times. The default is DefaultExpiryMinutes from config. |
| Locks not working across instances | In-memory locks are single-instance only. Use Redis for distributed locking. |
| Cache lost on restart | By design -- in-memory cache is volatile. Use Redis for persistence across restarts. |
License
This project is licensed under the MIT License.
Copyright (c) 2024-2026 Repletory.
For complete documentation, infrastructure setup, and configuration reference, see the RepletoryLib main repository.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. 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. |
-
net10.0
- Microsoft.Extensions.Caching.Memory (>= 10.0.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 10.0.0)
- RepletoryLib.Caching.Abstractions (>= 1.0.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on RepletoryLib.Caching.InMemory:
| Package | Downloads |
|---|---|
|
RepletoryLib.Caching.Hybrid
Hybrid L1/L2 caching (InMemory + Redis) for RepletoryLib |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.0.0 | 80 | 3/2/2026 |