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
                    
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="RepletoryLib.Caching.InMemory" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="RepletoryLib.Caching.InMemory" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="RepletoryLib.Caching.InMemory" />
                    
Project file
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 RepletoryLib.Caching.InMemory --version 1.0.0
                    
#r "nuget: RepletoryLib.Caching.InMemory, 1.0.0"
                    
#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 RepletoryLib.Caching.InMemory@1.0.0
                    
#: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=RepletoryLib.Caching.InMemory&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=RepletoryLib.Caching.InMemory&version=1.0.0
                    
Install as a Cake Tool

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.

NuGet .NET 10 License: MIT


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 -- IDistributedLockService implementation 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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