EviCache 2.0.0

dotnet add package EviCache --version 2.0.0
                    
NuGet\Install-Package EviCache -Version 2.0.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="EviCache" Version="2.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="EviCache" Version="2.0.0" />
                    
Directory.Packages.props
<PackageReference Include="EviCache" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add EviCache --version 2.0.0
                    
#r "nuget: EviCache, 2.0.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.
#addin nuget:?package=EviCache&version=2.0.0
                    
Install EviCache as a Cake Addin
#tool nuget:?package=EviCache&version=2.0.0
                    
Install EviCache as a Cake Tool

About

EviCache is a lightweight, thread-safe, in-memory caching library for .NET.

It supports multiple eviction policies and offers extended cache operations. Moreover, it provides metrics and inspection capabilities.

Key Features

  • Thread-safe operations: All cache operations are synchronized, ensuring thread safety for concurrent access.
  • Multiple eviction policies:
    • Least Recently Used (LRU): Evicts the item that has not been accessed for the longest period.
    • Least Frequently Used (LFU): Evicts the item with the lowest access frequency.
    • First-In, First-Out (FIFO): Evicts the item that was inserted first.
    • No Eviction: New items are not accepted when the cache is full.
  • Built-in metrics: Tracks cache count, hits, misses, and evictions.
  • Cache inspection: Retrieves snapshots and list of keys currently in the cache.

How to Use

Interfaces

  • ICache<TKey, TValue>: Combines all functionality exposed by the interfaces below.
  • ICacheOperations<TKey, TValue>: Provides cache operations (Get, Put, etc.).
  • ICacheOperationsAsync<TKey, TValue>: Provides asynchronous versions of cache operations (GetAsync, PutAsync, etc.).
  • ICacheMetrics: Access performance and usage metrics.
  • ICacheInspection<TKey, TValue>: Inspect cache contents (keys and snapshots).

Exceptions

  • KeyNotFoundException: Thrown when attempting to retrieve a key that does not exist (via Get).
  • CacheFullException: Thrown when the cache is full and (a) eviction fails, or (b) eviction is disabled (NoEviction policy).

Initializing the cache

using EviCache;
using EviCache.Options;
using EviCache.Enums;

// Create cache options with a capacity of 5 and LRU eviction policy
var cacheOptions = new CacheOptions(5, EvictionPolicy.LRU);

// Instantiate the cache
var cache = new Cache<int, string>(cacheOptions);

// Optionally, provide an ILogger instance
var cache = new Cache<int, string>(cacheOptions, logger);

Inserting and retrieving values

// Insert a new value into the cache
cache.Put(1, "one");

// Retrieve the value (throws KeyNotFoundException if key does not exist)
string value = cache.Get(1);

// Retrieve the value without throwing an exception if the key is missing
bool retrieved = cache.TryGet(1, out string value);

Conditional retrieval and updates

// Return the existing value for a key or add a new key/value pair if not found
string value = cache.GetOrAdd(2, "two");

// Update the value if the key exists; otherwise, add it
string newValue = cache.AddOrUpdate(1, "newOne");

Checking, removing, and clearing entries

// Check if a key exists in the cache
bool exists = cache.ContainsKey(1);

// Remove a key from the cache
bool removed = cache.Remove(1);

// Clear the entire cache
cache.Clear();

Inspecting cache contents

// Retrieve a snapshot of the cache
ImmutableList<KeyValuePair<TKey, TValue>> snapshot = cache.GetSnapshot();

// Retrieve all keys
ImmutableList<TKey> keys = cache.GetKeys();

Accessing cache metrics

// Access cache metrics
Console.WriteLine($"Cache Capacity: {cache.Capacity}");
Console.WriteLine($"Cache Count: {cache.Count}");
Console.WriteLine($"Cache Hits: {cache.Hits}");
Console.WriteLine($"Cache Misses: {cache.Misses}");
Console.WriteLine($"Cache Evictions: {cache.Evictions}");
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 is compatible.  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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.0.0 121 3/16/2025
1.4.1 184 3/3/2025
1.4.0 83 3/1/2025
1.3.1 88 2/28/2025
1.3.0 93 2/24/2025
1.2.0 95 2/22/2025
1.1.0 92 2/19/2025
1.0.0 105 2/17/2025