LightMemoryCache 1.0.10

dotnet add package LightMemoryCache --version 1.0.10
NuGet\Install-Package LightMemoryCache -Version 1.0.10
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="LightMemoryCache" Version="1.0.10" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add LightMemoryCache --version 1.0.10
#r "nuget: LightMemoryCache, 1.0.10"
#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.
// Install LightMemoryCache as a Cake Addin
#addin nuget:?package=LightMemoryCache&version=1.0.10

// Install LightMemoryCache as a Cake Tool
#tool nuget:?package=LightMemoryCache&version=1.0.10

LightMemoryCache

LightMemoryCache is a lightweight, thread-safe, in-memory caching library for .NET applications. It provides a simple and efficient way to cache data objects with customizable expiration times.

Build and Test Coverage Status NuGet

Features

  • Simple API: Easy-to-use methods for adding, retrieving, and invalidating cached items.
  • Thread-Safe: Utilizes ConcurrentDictionary along with the lock statement for thread safety.
  • Expiration: Supports customizable expiration times for cached items.
  • Automatic Cleanup: Periodically cleans up expired items using a background timer.
  • Configurable: Allows configuration of default expiration time via options.

Installation

You can install LightMemoryCache via NuGet Package Manager:

Install-Package LightMemoryCache

or dotnet CLI

dotnet add package LightMemoryCache

Usage

Register the component:

services.AddLightMemoryCache();

or

var configurationRoot = new ConfigurationBuilder()
                        .SetBasePath(Directory.GetCurrentDirectory())
                        .AddJsonFile("appsettings.json", false)
                        .Build();

var defaultExpirationInMinutes = configurationRoot.GetValue<int>("CacheOptions:DefaultExpirationInMinutes");

services.AddLightMemoryCache(options => { options.DefaultExpirationInMinutes = defaultExpirationInMinutes; });

appsettings.json

"CacheOptions": {
    "DefaultExpirationInMinutes": "15"
}

Dependency injection

public class CountryService : ICountryService
{
    private readonly ILightMemoryCache _cache;
    private readonly ICountryRepository _countryRepository;
    private const string CountryCacheKey = "AllCountries";

    public CountryService(ILightMemoryCache cache, ICountryRepository countryRepository)
    {
        _cache = cache;
        _countryRepository = countryRepository;
    }

    public async Task<IEnumerable<CountryDto>> GetAllAsync()
    {
        if (_cache.TryGetValue(CountryCacheKey, out IEnumerable<CountryDto> cachedCountries))
        {
            return cachedCountries;
        }

        var countries = await _countryRepository.GetAllAsync();
        _cache.Add(CountryCacheKey, countries, TimeSpan.FromMinutes(30));
        return countries;
    }
    // ...
}

Benchmark

LightMemoryCache vs MemoryCache

Both LightMemoryCache and MemoryCache were benchmarked under similar conditions to evaluate their performance:

Method Mean Error StdDev
LightMemoryCache_AddAndRetrieve 12.19 us 0.029 us 0.027 us
MemoryCache_AddAndRetrieve 13.60 us 0.149 us 0.116 us
LightMemoryCache_AddAndRetrieveComplexObject 23.83 us 0.093 us 0.072 us
MemoryCache_AddAndRetrieveComplexObject 34.23 us 0.083 us 0.077 us
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.10 68 6/20/2024
1.0.9 70 6/20/2024
1.0.8 68 6/20/2024
1.0.7 68 6/20/2024
1.0.6 69 6/20/2024
1.0.5 66 6/20/2024
1.0.4 70 6/20/2024
1.0.3 67 6/20/2024
1.0.2 72 6/20/2024
1.0.1 78 6/19/2024