NetMemCache 1.0.2

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

NetMemCache

NetMemCache provides a simple and fast key-value store. No database or other technology is needed to store the data. The data is stored in the file system.

This project is based on a project by Michael Christensen called BinaryRage. Kudos to him for an incredibly clever idea. After some experimentation with the original project, I made changes step by step so that there isn't much left of the old code.

Motivation

With Redis there exists a very powerful project for an ObjectCache. But Redis requires a Linux distribution, which is not so easy to implement in an IT landscape with Windows. WSL even in version 2 is not suitable for long running services and there is an issue with WSL not solved by Microsoft.

Moreover, we think there should be a simple, fast .NET object cache available as open source.

Functionality and Difference to BinaryRage

  • We reworked the classes to be instantiable to support DI, unlike BinaryRage, where all methods are static.
  • We implemented a clean asynchronous architecture using async/await.
  • The old code calculated a wait time after save operations, which slowed down the system unnecessarily. By using async/await, the store was able to be significantly accelerated.
  • We made all core components replacable with DI, e.g. IStorage, IFolderStructure, IObjectSerializer
  • The original implementation serializes the objects in-memory. We write the objects directly into the output stream.
  • We removed the class SimpleObject because, in our opinion, it has no utility value.
  • We implemented an optional expiration date so that cache entries can be removed after a certain period of time.
  • We changed the algorithm that calculates the directory structure so that all objects reside in a maximum of 65536 directories. This behavior can be changed.
  • The original algorithm uses the BinaryFormatter. We changed this in favour of Newtonsoft.Json. The algorithm can now be injected via DependencyInjection.
  • The old algorithm serialized all input objects. We don't serialize strings. These will be compressed directly if they are long enough. It is also saved whether deserialization is necessary.
  • The original algorithm compresses all objects. We only compress objects of a certain size and save whether compression has taken place.
  • We added a WebAPI project to use the ObjectCache as a service.
  • Future feature: Saving objects from a stream, e.g. after upload of data to a web application

Show me the code

The samples are intentionally derived from the BinaryRage samples to allow comparism. Simple class - no serializable attribute needed.

public class Product
{
	public string Title { get; set; }
	public string ThumbUrl { get; set; }
	public string Description { get; set; }
	public float Price { get; set; }
}

var myProduct = new Product(){...};

Insert-syntax (same for create and update)

var memCache = new MemCache("productCache");
memCache.Set("mykey", myProduct);

// removal of an object after a certain time span
memCache.Set("mykey", myProduct, timeSpan);

... or with a list

var myProducts = new List<Product>();
memCache.Insert("mykey", myProducts);

Get the saved data

var myProduct = memCache.Get<Product>("mykey");

... or with a list

var listOfProducts = memCache.Get<List<Product>>("mykey");

Query objects directly with LINQ

var bestsellers = memCache.Get<List<Category>>("bestsellers").Where(p => !string.IsNullOrEmpty(p.Name));

Note: GetJSON and WaitForCompletion have been removed. Key API has been removed.

FAQ

Is it really fast?

On my development machine, which is not the newest technology I measured about 0.6 ms for each read / write pair.

All writes are performed asynchronously. Reads are instantly available (also if writes are not completed) because of the in-memory-cache.

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 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.  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
1.0.2 141 6/19/2025