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" />
<PackageReference Include="EviCache" />
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
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#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
#tool nuget:?package=EviCache&version=2.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
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 (viaGet
).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 | Versions 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.
-
net6.0
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.3)
-
net8.0
- Microsoft.Extensions.Logging.Abstractions (>= 9.0.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.