LFUCache 1.0.2

dotnet add package LFUCache --version 1.0.2
                    
NuGet\Install-Package LFUCache -Version 1.0.2
                    
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="LFUCache" Version="1.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="LFUCache" Version="1.0.2" />
                    
Directory.Packages.props
<PackageReference Include="LFUCache" />
                    
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 LFUCache --version 1.0.2
                    
#r "nuget: LFUCache, 1.0.2"
                    
#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.
#:package LFUCache@1.0.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=LFUCache&version=1.0.2
                    
Install as a Cake Addin
#tool nuget:?package=LFUCache&version=1.0.2
                    
Install as a Cake Tool

LFU Cache Implementation

Overview

A cross platform (Windows/Linux/etc) Least Frequently Used (LFU) Cache implementation in .NET that evicts items based on their usage frequency. Each cached item maintains a usage counter that increments upon access. When the cache reaches its capacity limit, it removes the item with the lowest usage count to free up space.

Note: This project has been updated to run on Linux using NUnit for testing and Visual Studio Code as the development environment.

Installation

Install via NuGet Package Manager:

Install-Package LFUCache -Version 1.0.2

Usage Example

ICache<string, string> cache = new LfuCache<string, string>(1000);
cache.Add("name", "Helene");
cache.Add("surname", "Stuart");
var name = cache.Get("name");

Technical Implementation

The LfuCache class implements the ICache interface:

Class Diagram

Data Structure

The implementation uses a hybrid data structure combining:

  • A SortedList where the key is the usage count
  • A LinkedList as the value, containing all elements with the same usage count

This structure is organized as a binary tree of linked lists, enabling O(log n) time complexity for both Add and Get operations.

Binary Tree and Linked List Structure

Performance

Benchmark Results

The cache demonstrates impressive performance:

  • 1,000,000 add/get operations
  • Cache size: 90,000 items
  • Dataset size: 100,000 elements
  • Execution time: 466ms

LFU Cache Performance

Compared to .NET Framework's MemoryCache, this implementation:

  • Executes faster
  • Uses less memory
  • Maintains consistent performance

Memory Cache Performance Comparison

The benchmarks:

  • Use randomly generated Add/Get operation sequences in BitArray
  • Process elements from a fixed-size list
  • Are conducted using BenchmarkDotNet

Benchmark Results

Testing

Unit tests are written using the NUnit framework with comprehensive code coverage tracked through Azure Pipeline.

Code Coverage Report

Product Compatible and additional computed target framework versions.
.NET 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.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.

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
1.0.2 145 6/5/2025
1.0.1 147 6/4/2025
1.0.0 814 2/9/2019