Skip to content

Commit 1c02345

Browse files
[stg] Bring in MemoryCache
Because I'm too lazy to rework it.
1 parent 58a05ea commit 1c02345

4 files changed

Lines changed: 58 additions & 3 deletions

File tree

LeveHelper/Caches/ItemQuantityCache.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using FFXIVClientStructs.FFXIV.Client.Game;
2-
using HaselCommon.Cache;
2+
using LeveHelper.Utils;
33

44
namespace LeveHelper.Caches;
55

LeveHelper/Caches/LeveIssuerCache.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System.Collections.Generic;
22
using System.Linq;
3-
using HaselCommon.Cache;
43
using HaselCommon.Services;
4+
using LeveHelper.Utils;
55
using Lumina.Excel.Sheets;
66

77
namespace LeveHelper.Caches;

LeveHelper/Caches/ResidentLevelCache.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
using HaselCommon.Cache;
21
using HaselCommon.Services;
2+
using LeveHelper.Utils;
33
using Lumina.Excel.Sheets;
44

55
namespace LeveHelper.Caches;

LeveHelper/Utils/MemoryCache.cs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System.Collections.Concurrent;
2+
using System.Diagnostics.CodeAnalysis;
3+
using HaselCommon.Extensions;
4+
5+
namespace LeveHelper.Utils;
6+
7+
public abstract class MemoryCache<TKey, TValue> where TKey : notnull, IEquatable<TKey>
8+
{
9+
protected readonly ConcurrentDictionary<TKey, TValue?> Data = [];
10+
11+
public virtual void Dispose()
12+
{
13+
Clear();
14+
}
15+
16+
public abstract TValue? CreateEntry(TKey key);
17+
18+
public virtual TValue? GetValue(TKey key)
19+
{
20+
TryGetValue(key, out var value);
21+
return value;
22+
}
23+
24+
public virtual bool TryGetValue(TKey key, [NotNullWhen(returnValue: true)] out TValue? value)
25+
{
26+
if (Data.TryGetValue(key, out var _value))
27+
{
28+
if (_value == null)
29+
{
30+
value = default;
31+
return false;
32+
}
33+
34+
value = _value!;
35+
return true;
36+
}
37+
38+
var entry = CreateEntry(key);
39+
40+
Data.TryAdd(key, entry);
41+
42+
value = entry;
43+
return entry != null;
44+
}
45+
46+
public virtual bool Remove(TKey key)
47+
{
48+
return Data.TryRemove(key, out var _);
49+
}
50+
51+
public virtual void Clear()
52+
{
53+
Data.Dispose();
54+
}
55+
}

0 commit comments

Comments
 (0)