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.
- 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
LocalMemoryCacheis used. - Expiration Support: Cache objects with defined expiration times.
- Thread-Safe Access: Ensures efficient caching in multi-threaded environments.
To use NetTools.Cache, add the library via NuGet Package Manager:
Install-Package NetTools.CacheBy 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.
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}");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}");
}
}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.
We welcome contributions! To get involved:
- Fork NetTools.Cache, make improvements, and submit a pull request.
- Code will be reviewed upon submission.
- Join discussions via the issues board.
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!