No description
Find a file
Renovate Bot 3bbc533cad
All checks were successful
Build SimpleCache / build (pull_request) Successful in 3m3s
Build SimpleCache / build (push) Successful in 37s
chore(deps): update git.dutches.de/dutch/workflow-dotnet:9.0-10.0 docker digest to ebf689b
2025-12-10 18:45:09 +00:00
.forgejo/workflows chore(deps): update git.dutches.de/dutch/workflow-dotnet:9.0-10.0 docker digest to ebf689b 2025-12-10 18:45:09 +00:00
SimpleCache
SimpleCache.Benchmarks chore(deps): update dependency system.runtime.caching to 10.0.1 2025-12-09 18:02:44 +00:00
SimpleCache.Tests
.gitignore
LICENSE
README.md
renovate.json
SimpleCache.sln

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