SimpleCache (0.1.0)

Published 2025-11-12 20:36:14 +01:00 by Dutch in Dutch/SimpleCache

Installation

dotnet nuget add source --name Dutch --username your_username --password your_token 
dotnet add package --source Dutch --version 0.1.0 SimpleCache

About this package

A thread-safe caching library for .NET with support for different expiration strategies.

SimpleCache

A thread-safe caching library for .NET with support for different expiration strategies.

Features

  • Thread-safe concurrent access based on ConcurrentDictionary
  • Support for multiple expiration modes:
    • Time-to-live (TTL): Entries expire after a fixed duration
    • Sliding expiration: Entries reset their expiration timer on access
  • Automatic cleanup of expired entries
  • Lightweight and efficient implementation
  • No external dependencies

Benchmark

Method Mean Error StdDev Gen0 Allocated
DictionaryLookup 25.08 ns 0.755 ns 0.196 ns - -
SimpleCacheLookup (Sliding) 41.63 ns 0.244 ns 0.063 ns - -
SimpleCacheLookup (TTL) 38.66 ns 0.362 ns 0.094 ns - -
MemoryCacheLookup 239.41 ns 1.855 ns 0.482 ns 0.0076 128 B
SimpleCacheGetOrAdd (Sliding) 73.21 ns 2.523 ns 0.655 ns 0.0210 352 B
SimpleCacheGetOrAdd (TTL) 56.54 ns 0.792 ns 0.123 ns 0.0210 352 B
MemoryCacheGetOrAdd 524.66 ns 9.418 ns 1.457 ns 0.0687 1152 B
SimpleCacheAddRemove (Sliding) 49.43 ns 1.366 ns 0.355 ns 0.0105 176 B
SimpleCacheAddRemove (TTL) 49.17 ns 2.132 ns 0.330 ns 0.0105 176 B
MemoryCacheAddRemove 298.84 ns 5.938 ns 0.919 ns 0.0191 320 B

Usage Examples

Basic Usage

// Create cache
var cache = new SimpleCache<string, int>(ExpirationMode.TimeToLive);

// Add items to cache
cache.AddOrUpdate("counter", 1, TimeSpan.FromMinutes(5));

// Get items from cache
if (cache.TryGet("counter", out int value))
{
    Console.WriteLine(value); // Outputs: 1
}

// Get item from cache, or add if not found
int value = cache.GetOrAdd("counter", () => 2, TimeSpan.FromHours(1));
Console.WriteLine(value); // Outputs: 1

NuGet

To download this package as a NuGet, a new NuGet source has to be added:

dotnet nuget add source https://git.dutches.de/api/packages/Dutch/nuget/index.json -n "Dutch@DutchGit"

License

MIT License

Details
NuGet
2025-11-12 20:36:14 +01:00
72
Marcel van der Heide
9 KiB
Assets (2)
Versions (1) View all
0.1.0 2025-11-12