RingKeyBuffer 0.0.5

dotnet add package RingKeyBuffer --version 0.0.5
NuGet\Install-Package RingKeyBuffer -Version 0.0.5
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.5" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add RingKeyBuffer --version 0.0.5
#r "nuget: RingKeyBuffer, 0.0.5"
#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.5

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

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 classes:

  • RingKeyBuffer
  • ThreadSafeRingKeyBuffer
  • ThreadSafeNonBlockingRingKeyBuffer

ThreadSafeNonBlockingRingKeyBuffer uses TryEnterWriteLock and TryEnterReadLock methods and don't acquire lock when they return false.

They 2 generic parameters: TKey and TValue (key type and value type respectively). TKey is a string by default.

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 thread-safe buffers:

class Item
{
    public string Id;
}

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

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

Use thread-safe buffers:

var buffer = new ThreadSafeRingKeyBuffer<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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net7.0

    • No dependencies.
  • 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
0.0.5 103 3/18/2024
0.0.3 102 2/9/2024
0.0.2 107 9/18/2023
0.0.1 97 9/17/2023