-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
131 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<Project> | ||
<PropertyGroup> | ||
<AccelerateBuildsInVisualStudio>true</AccelerateBuildsInVisualStudio> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup Condition=" '$(Configuration)' == 'Release' "> | ||
<Optimize>true</Optimize> | ||
</PropertyGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
namespace EvenireDB.Utils | ||
{ | ||
public class LRUCache<TKey, TValue> | ||
where TKey : notnull | ||
{ | ||
private class Node | ||
{ | ||
public TKey Key { get; init; } | ||
public TValue Value { get; init; } | ||
public Node? Next { get; set; } | ||
public Node? Previous { get; set; } | ||
} | ||
|
||
private readonly Dictionary<TKey, Node> _cache; | ||
private readonly uint _capacity; | ||
private Node? _head; | ||
private Node? _tail; | ||
|
||
public LRUCache(uint capacity) | ||
{ | ||
_capacity = capacity; | ||
_cache = new Dictionary<TKey, Node>((int)capacity); | ||
} | ||
|
||
public async ValueTask<TValue> GetOrAddAsync( | ||
TKey key, | ||
Func<TKey, CancellationToken, ValueTask<TValue>> valueFactory, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
if (_cache.TryGetValue(key, out var node)) | ||
{ | ||
MoveToHead(node); | ||
return node.Value; | ||
} | ||
|
||
var value = await valueFactory(key, cancellationToken).ConfigureAwait(false); | ||
if (_cache.Count == _capacity) | ||
{ | ||
_cache.Remove(_tail.Key); | ||
|
||
_tail = _tail.Previous; | ||
|
||
if(_tail != null) | ||
_tail.Next = null; | ||
} | ||
|
||
node = new Node { Key = key, Value = value, Next = _head }; | ||
if (_head != null) | ||
_head.Previous = node; | ||
|
||
_head = node; | ||
if (_tail == null) | ||
_tail = node; | ||
|
||
_cache.Add(key, node); | ||
|
||
return node.Value; | ||
} | ||
|
||
private void MoveToHead(Node node) | ||
{ | ||
if (node == _head) | ||
return; | ||
|
||
if (node.Previous != null) | ||
node.Previous.Next = node.Next; | ||
|
||
if (node.Next != null) | ||
node.Next.Previous = node.Previous; | ||
|
||
if (_tail == node) | ||
_tail = node.Previous; | ||
|
||
node.Previous = null; | ||
node.Next = _head; | ||
|
||
if (_head != null) | ||
_head.Previous = node; | ||
|
||
_head = node; | ||
} | ||
|
||
public int Count => _cache.Count; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
global using Xunit; | ||
global using FluentAssertions; | ||
global using NSubstitute; | ||
global using EvenireDB.Utils; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
namespace EvenireDB.Tests | ||
{ | ||
public class LRUCacheTests | ||
{ | ||
[Fact] | ||
public async Task GetOrAddAsync_should_add_when_not_existing() | ||
{ | ||
var cache = new LRUCache<string, string>(1); | ||
cache.Count.Should().Be(0); | ||
|
||
var value = await cache.GetOrAddAsync("key", (_, _) => new ValueTask<string>("value")); | ||
value.Should().Be("value"); | ||
|
||
cache.Count.Should().Be(1); | ||
} | ||
|
||
[Fact] | ||
public async Task GetOrAddAsync_should_keep_capacity() | ||
{ | ||
var cache = new LRUCache<string, string>(1); | ||
cache.Count.Should().Be(0); | ||
|
||
var value = await cache.GetOrAddAsync("key", (_, _) => new ValueTask<string>("value")); | ||
value.Should().Be("value"); | ||
|
||
cache.Count.Should().Be(1); | ||
|
||
value = await cache.GetOrAddAsync("key2", (_, _) => new ValueTask<string>("value2")); | ||
value.Should().Be("value2"); | ||
|
||
cache.Count.Should().Be(1); | ||
} | ||
} | ||
} |