ConcurrentCaching 0.2.2

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

concurrent-memory-cache

Build Coverage Status

A simple wrapper over MemoryCache to prevent concurrent cache missing.

Purpose

The Microsoft MemoryCache API provides the ability to cache locally, however it does not guarantee the atomicity when multiple threads call GetOrCreate() with the same entry (at the moment this was written), which can lead to multiple cache missing and duplicated data fetching.

How it works

It was implemented based on the same idea used by ConcurrentDictionary, which divides the Hashmap into a fixed number of segments (16 by default). Each of them is protected by a segment lock. Write operations that fall into the same segment are sequentialized, while write operations falling into different segments are handled concurrently. All read operations are lock-free.

How to use

  1. Install package:

    dotnet add package ConcurrentCaching
    
  2. Inject caching service:

    services.AddMemoryCache(options =>
    {
        //... Setup e.g. max cache limit for LRU
    });
    services.AddSingleton<IConcurrentMemoryCache, ConcurrentMemoryCache>();
    
  3. Fetch data from cache:

    var item = cache.GetOrCreate<TItem>("<key>", entry =>
    {
        // Fetch data from elsewhere and return it ...
    });
    
    var item = await cache.GetOrCreateAsync<TItem>("<key>", async entry =>
    {
        // Fetch data from elsewhere and return it ...
    });
    
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 was computed.  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 was computed.  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 was computed.  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.

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
0.2.2 970 10/23/2022
0.2.1 634 11/17/2021
0.2.0 378 11/16/2021
0.1.0 530 3/6/2021
0.0.3 563 10/17/2020
0.0.2 549 10/17/2020
0.0.1 546 10/17/2020