NetTools.Cache 1.2.0

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

NetTools.Cache

NetTools.Cache is a lightweight .NET Standard 2.1 class library for governance over caching objects using your own implementation. It provides a flexible caching mechanism using an interface-based approach, allowing developers to integrate custom cache controllers seamlessly.

By default, this library uses an in-memory cache so you can get started using it, if only simple memory cache is required, but developers can implement their own cache controllers by conforming to the ICacheController interface.


Features

  • Customizable Cache Controllers: Implement your own caching mechanism by defining an ICacheController.
  • Local Memory Caching: Built-in memory cache for fast data retrieval.
  • Synchronous & Asynchronous Methods: Supports both blocking and async caching operations.
  • Auto-Instantiated Controller: If no cache controller is set, the default LocalMemoryCache is used.
  • Expiration Support: Cache objects with defined expiration times.
  • Thread-Safe Access: Ensures efficient caching in multi-threaded environments.

Getting Started

Installation

To use NetTools.Cache, add the library via NuGet Package Manager:

Install-Package NetTools.Cache

Quick Examples

Setting a Custom Cache Controller

By setting your own cache controller, you can define your own caching mechanism:

using NetTools.Cache;

// Set a custom cache controller
DataStore.SetCacheController(new MyCustomCacheController());

If you're implementing your own (ICacheController), you should assign this before performing any storage/retreival.

Caching an Object

You can store and retrieve cached objects using unique identifiers: Synchronous Caching

using NetTools.Cache;

string data = "Cached Data";
string cacheKey = "example_key";

// Store data in cache with 10-minute expiration
DataStore.SetCache(data, cacheKey, TimeSpan.FromMinutes(10));

// Retrieve data from cache
string cachedData = DataStore.GetCache(cacheKey, "Default Value");
Console.WriteLine($"Cached Data: {cachedData}");
Asynchronous Caching
using System;
using System.Threading.Tasks;
using NetTools.Cache;

class Program
{
    static async Task Main()
    {
        string data = "Cached Async Data";
        string cacheKey = "async_example";

        // Store data in cache asynchronously
        await DataStore.SetCacheAsync(data, cacheKey, TimeSpan.FromMinutes(5));

        // Retrieve data from cache asynchronously
        string cachedData = await DataStore.GetCacheAsync(cacheKey, "Default Value");
        Console.WriteLine($"Cached Data: {cachedData}");
    }
}
Default Values

If a cached object is missing, a default value can be returned or a function can be invoked:

Using Func<T> for Default Values
using NetTools.Cache;

string cachedData = DataStore.GetCache("missing_key", "Generated Default Value");
Console.WriteLine($"Retrieved: {cachedData}");

cachedData = DataStore.GetCache("missing_key", () =>
{
	return "Generated Default Value";
});

Console.WriteLine($"Retrieved: {cachedData}");

This simple library is used by NetModules.Cache.MemoryCache, a simple, commercially battle tested, NetModules module for caching handled events for performance and efficiency.

Contributing

We welcome contributions! To get involved:

  1. Fork NetTools.Cache, make improvements, and submit a pull request.
  2. Code will be reviewed upon submission.
  3. Join discussions via the issues board.

License

NetTools.Cache is licensed under the MIT License, allowing unrestricted use, modification, and distribution. If you use NetTools.Cache in your own project, we’d love to hear about your experience, and possibly feature you on our website!

Full documentation coming soon!

NetModules Foundation

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.  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. 
.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.
  • .NETStandard 2.1

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on NetTools.Cache:

Package Downloads
NetModules.Cache.MemoryCache

A basic cache module that uses an in-memory mechanism to store event output to a cache, using the event name, input, and optional metadata as a storage and lookup identifier.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.2.0 213 5/8/2025