CypherPotato.MemoryCacheStorage 2.4.0

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

// Install CypherPotato.MemoryCacheStorage as a Cake Tool
#tool nuget:?package=CypherPotato.MemoryCacheStorage&version=2.4.0                

<div align="center" style="display:grid;place-items:center;"> <p> <img src="./.assets/icon.png"> </p> <h1>Memory Cache Storage</h1> </div>

This is a tiny implementation of a TTL (time-to-live) in-memory cache, where each stored object has a lifetime to be retrieved.

It is an lightweight alternative to System.Runtime.Caching, which in turn offers:

  • thread-safe add, remove or query cache items.
  • async API.
  • almost 10x faster than System.Runtime.Caching. - benchmark
  • supports enumerating and querying cached items, using their IDs.
  • hybrid caching strategy of lazy caching + pooling.

The "lazy" strategy consists of validating whether the cached object is still fresh at the time you obtain it. The other strategy, long-pooling, regularly runs a check on all cached objects in order to validate whether they are expired or not, allowing the GC to do its work for objects that should no longer be kept in the cache.

The storage mechanism of this implementation allows an object to be stored with multiple keys, and multiple objects can share the same key. This is possible because it makes grouping of cache items possible, making it easy to get the objects with the GetAll() and RemoveAll() query functions.

Creating the MemoryCacheStorage

// defines an in-memory cache where each cached item
// is fresh for 10 minutes
MemoryCacheStorage<int, string> cache = new MemoryCacheStorage<int, string> () {
    DefaultExpiration = TimeSpan.FromMinutes ( 10 )
};

// or create an list-powered cache with the same behavior
MemoryCacheList<string> listCache = new MemoryCacheList<string> ();

Adding items to cache

// add an item to the cache
cache.Add ( 10, "hello" );

// add or renew an item in the cache. if an item with the same key
// already exists, it won't be replaced, but have its expiration renewed.
// if the item does not exist, it will be added to the cache.
cache.AddOrRenew ( 10, "hey" );

// get an item from the cache using the following expression.
// if the item does not exist, the lambda expression will be executed
string result = cache.GetOrAdd ( 10, () => "hello" );

Querying items from cache

// gets an cached item by their key. throws an exception if the item
// doenst exists or is expired
string item = cache [ 10 ];

// attempts to get an item by their key. if the item is expired or
// not found, the result will be false and no result will be outputted
if (cache.TryGetValue ( 10, out string result )) {
    ;
}

// checks if an item is present and not expired
if (cache.ContainsKey ( 10 )) {
    ;
}

// IEnumerable over the cache store
if (cache.Where ( s => s.StartsWith ( "hello" ) ).Any ()) {
    ;
}

Removing items from cache

 // attempts to remove an cached item by their id
 cache.Remove ( 10 );
 
 // or removes only if expired
 cache.RemoveIfExpired ( 10 );

 // or removes all expired items
 cache.RemoveExpiredEntities ();
 
 // or just remove everything
 cache.Clear();

Cache events

cache.AddItemCallback += ( sender, addedItem ) => {
    // do something
};

cache.RemoveItemCallback += ( sender, removedItem ) => {
    // dispose the removed item
    (removedItem as IDisposable)?.Dispose ();
};

Cache-collect pool

// creates an cache pool which collects multiple expired items from the specified 
// cache storages every 30 minutes.
CachePoolingContext pool = new CachePoolingContext ( TimeSpan.FromMinutes ( 30 ) );
pool.CollectingCaches.Add ( cache1 );
pool.CollectingCaches.Add ( cache2 );
pool.CollectingCaches.Add ( cache3 );
pool.StartCollecting ();

// or
CachePoolingContext pool = CachePoolingContext.StartNew ( 
    TimeSpan.FromMinutes ( 30 ), cache1, cache2, cache3...);

Contributing

We welcome contributions! If you have ideas for improvements or find any issues, please open an issue or submit a pull request.

License

This project is licensed under the MIT License - see the LICENSE.md file for details.

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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net6.0

    • No dependencies.

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.4.0 66 12/24/2024
2.3.0 108 9/19/2024
2.1.0 65 9/2/2024
2.0.1 104 7/10/2024
2.0.0 122 6/21/2024
1.3.0 132 4/22/2024
1.2.3 129 4/17/2024
1.2.2 242 12/6/2023
1.2.1 133 12/1/2023
1.2.0 127 12/1/2023
1.1.0 165 11/9/2023
1.0.0 151 5/29/2023