RingKeyBuffer 0.0.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package RingKeyBuffer --version 0.0.2                
NuGet\Install-Package RingKeyBuffer -Version 0.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="RingKeyBuffer" Version="0.0.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add RingKeyBuffer --version 0.0.2                
#r "nuget: RingKeyBuffer, 0.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.
// Install RingKeyBuffer as a Cake Addin
#addin nuget:?package=RingKeyBuffer&version=0.0.2

// Install RingKeyBuffer as a Cake Tool
#tool nuget:?package=RingKeyBuffer&version=0.0.2                

The Ring Key Buffer

The data structure combines a ring buffer (circular buffer) and a Dictionary (Map).

It can be used as an LRU cache.

There are 3 top-level classes:

  • RingKeyBuffer
  • ConcurrencyRingKeyBuffer (thread safety supports)
  • ConcurrencyNonBlockingRingKeyBuffer (thread safety supports; don't wait for lock release when it is acquired)

Each of them has 2 generic parameters: TKey and TValue (type of key and type of value respectively). It can be possible to skip TKey, by default it is string.

They implement a common interface:

public interface IRingKeyBuffer<in TKey, TValue>
{
    void Add(TValue item);
    bool TryGet(TKey key, out TValue? item);
    bool Delete(TKey key);

}

Below few examples of usage.

Using of thread-unsafe buffer:

const int BUF_SIZE = 1_000;
IRingKeyBuffer<int, int> buf = new RingKeyBuffer<int, int>(size: BUF_SIZE, getKey: i => i, garbageItem: -1);

for (var i = 0; i < 10_000; i++)
    buf.Add(i);

// buf.TryGet(0, out _) == false
// buf.TryGet(8_999, out _) == false
// buf.TryGet(9_000, out var a) == true && a == 9_000
// buf.TryGet(9_999, out var b) == true && b == 9_999

Creation concurrency buffers:

class Item
{
    public string Id;
}

var garbageItem = new Item()
{
    Id="__GARBAGE__"
};

const int BUF_SIZE = 1_000;
var buffer = new ConcurrentRingKeyBuffer<Item>(BUF_SIZE, (item) => item.Id, garbageItem);
var nonBlockingBuffer = new ConcurrentNonBlockingRingKeyBuffer<Item>(BUF_SIZE, (item) => item.Id, garbageItem);

Use concurrency buffers:

var buffer = new ConcurrentRingKeyBuffer<Item>(BUF_SIZE, (item) => item.Id, garbageItem);

var tasks = new Task[10_000];
void RunTask(int i)
{
    tasks[i] = Task.Run(() =>
    {
        buffer.Add(new Item(){Id=i});
    });
}
for (var i = 0; i < 10_000; i++)
   RunTask(i);

await Task.WhenAll(tasks);

// buf.TryGet("0", out _) == most probably false 
// buf.TryGet("8999", out _) == ?
// buf.TryGet("9000", out _) == ?
// buf.TryGet("9999", out _) == most probably true
Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  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 was computed.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net7.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
0.0.5 137 3/18/2024
0.0.3 140 2/9/2024
0.0.2 139 9/18/2023
0.0.1 124 9/17/2023