DiskMemCache 2.2.0

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

// Install DiskMemCache as a Cake Tool
#tool nuget:?package=DiskMemCache&version=2.2.0                

DiskMemCache

Minimalistic persistence and caching library. Nuget available here.

Which problem it solves ?

Library was designed to solve issue of not wasting time while developing and application that needs to do some heavy or long computation of some data and you do not want to wait for that result every time you are developing an app (hot reloading or restarting and app), but you need the data to be there.

TLDR

Every time you want to cache/persist result of operation you can wrap this call with:

private static async Task<Item> LongRunningOperationFunctionToCacheAsync()
{
   // expensive computation / long IO operation ...
   await Task.Delay(TimeSpan.FromSeconds(30));
   return new Item { Value = 9000 };
}

// first operation with this key will call function and cache result in memory cache + also on file as JSON
var result = await DiskMemCache.GetOrComputeAsync("my-expensive-func", LongRunningOperationFunctionToCacheAsync);

// all next results are returned from cache
// if cached item is than 1 hour old
for (int i = 0; i < 100; i++)
{
   var resultFromMemCache = await DiskMemCache.GetOrComputeAsync("my-expensive-func", LongRunningOperationFunctionToCacheAsync,
       t => t > TimeSpan.FromHours(1));
}

// process can be re-started and result will still be returned from persistent cache
// if cached item is than 1 hour old
var resultReturnedFromSerializedFile = await DiskMemCache.GetOrComputeAsync("my-expensive-func", LongRunningOperationFunctionToCacheAsync,
   t => t > TimeSpan.FromHours(1));

See tests for more examples.

Manual cache invalidation

You can invalidate cache either manually by calling

// Removes all cached entries stored in memory/files on disk
DiskMemCache.PurgeAll();

// Removes only operation with concrete key
DiskMemCache.Purge(key => key == "123");

Note

I do not want this library to be super extendable or configurable. If you are looking for something more complex and configurable look for more mature libraries.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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
2.2.0 179 2/19/2024
2.1.0 120 2/18/2024
2.0.0 120 2/18/2024
1.0.0 115 2/18/2024

Fully thread safe