ImanSoftware.Caching 1.0.2

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

ImanSoftware.Caching

Version: 1.0.0 | Last Updated: August 2026

A lightweight and extensible caching abstraction for .NET with a default in-memory implementation.

ImanSoftware.Caching provides a clean caching API that decouples your application from any specific cache provider. The default implementation uses IMemoryCache and integrates with IJsonSerializer for flexible object serialization.


Installation

Install the package using the .NET CLI:

dotnet add package ImanSoftware.Caching

Why CacheService?

Caching is a common optimization, but coupling your application directly to a cache implementation makes future changes more difficult.

Instead of:

_memoryCache.Set(key, value);

Use:

_cache.Set(key, value);

This abstraction allows the underlying cache provider to change without affecting your business logic.

Benefits include:

  • Clean architecture
  • Improved application performance
  • Dependency Injection friendly
  • Easy unit testing
  • Replaceable cache implementations

Features

  • ICacheService abstraction
  • ✅ In-memory cache implementation
  • ✅ Object serialization support
  • ✅ Configurable expiration
  • ✅ Thread-safe GetOrSet()
  • ✅ Race condition protection
  • ✅ Dependency Injection support
  • ✅ Lightweight and extensible

ICacheService

The core caching abstraction.

Available methods:

  • Get<T>()
  • Set()
  • Remove()
  • Exists()
  • GetOrSet()

Example:

_cache.Set("product", product);

var item = _cache.Get<Product>("product");

bool exists = _cache.Exists("product");

_cache.Remove("product");

MemoryCacheService

The default implementation is built on top of IMemoryCache.

Features include:

  • High-performance in-memory caching
  • Automatic object serialization using IJsonSerializer
  • Configurable expiration
  • Thread-safe operations
  • Suitable for ASP.NET Core applications

Cache Expiration

Specify an expiration time when storing values.

_cache.Set(
    "products",
    products,
    TimeSpan.FromMinutes(10));

Expired items are automatically removed by the underlying cache implementation.


GetOrSet()

The GetOrSet() method simplifies cache-aside scenarios.

If the value exists, it is returned immediately.

Otherwise:

  1. The factory function is executed.
  2. The result is cached.
  3. The cached value is returned.
var product = _cache.GetOrSet(
    key,
    () => GetFromDatabase(id),
    TimeSpan.FromMinutes(5));

Thread Safety

GetOrSet() uses synchronization internally to prevent multiple concurrent requests from executing the same expensive operation.

This helps prevent:

  • Duplicate database queries
  • Race conditions
  • Cache stampedes

Dependency Injection

Register the cache service in your application:

services.AddCacheService();

After registration, ICacheService can be injected into any service.


Example

using ImanSoftware.Caching;

public class ProductService
{
    private readonly ICacheService _cache;

    public ProductService(ICacheService cache)
    {
        _cache = cache;
    }

    public async Task<Product> GetProductAsync(int id)
    {
        var key = $"product_{id}";

        return _cache.GetOrSet(
            key,
            () => GetFromDatabase(id),
            TimeSpan.FromMinutes(5));
    }
}

Unit Testing

Because caching is abstracted behind an interface, it can easily be mocked.

var mock = new Mock<ICacheService>();

mock.Setup(x =>
    x.Get<Product>("product_1"))
    .Returns(product);

This keeps your tests isolated from the underlying cache implementation.


Design Goals

  • High performance
  • Clean abstraction
  • Dependency Injection friendly
  • Thread-safe operations
  • Cache provider independent
  • Lightweight
  • Extensible
  • Mock-friendly
  • Clean Architecture compatible

Requirements

  • .NET Standard 2.0+
  • .NET 6+
  • .NET 7+
  • .NET 8+
  • .NET 9+
  • .NET 10.0+

Author

Iman Estiri

📧 contact.imanestiri@gmail.com

📧 dev.imanestiri@gmail.com

GitHub:

https://github.com/ImanEstiri


License

This project is licensed under the MIT License.

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 (2)

Showing the top 2 NuGet packages that depend on ImanSoftware.Caching:

Package Downloads
ImanSoftware.Branding

Display ImanSoftware branding badge with contact information in Blazor apps.

ImanSoftware.Framework

Umbrella package that bundles all ImanSoftware modules for a unified development experience.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.2 228 9/3/2026
1.0.1 109 8/18/2026
1.0.0 136 8/3/2026