No description
Find a file
Renovate Bot dd6461d310
All checks were successful
Build SimpleCache / build (pull_request) Successful in 57s
Build SimpleCache / build (push) Successful in 1m5s
chore(deps): update dependency system.runtime.caching to 10.0.4
2026-03-10 16:01:45 +00:00
.forgejo/workflows chore(deps): update git.dutches.de/dutch/workflow-dotnet:9.0-10.0 docker digest to f4a81da 2026-02-12 22:05:06 +00:00
SimpleCache chore: update to .NET 10 (#6) 2025-11-19 22:15:52 +01:00
SimpleCache.Benchmarks chore(deps): update dependency system.runtime.caching to 10.0.4 2026-03-10 16:01:45 +00:00
SimpleCache.Tests chore(deps): update dependency microsoft.net.test.sdk to 18.3.0 2026-02-24 16:02:04 +00:00
.gitignore feat: initial implementation 2025-11-12 20:24:53 +01:00
LICENSE feat: initial implementation 2025-11-12 20:24:53 +01:00
README.md feat: initial implementation 2025-11-12 20:24:53 +01:00
renovate.json chore: renatove-groupallnonmajor 2025-11-12 21:48:41 +01:00
SimpleCache.sln feat: initial implementation 2025-11-12 20:24:53 +01:00

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