-
Notifications
You must be signed in to change notification settings - Fork 11
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
5 changed files
with
49 additions
and
14 deletions.
There are no files selected for viewing
Binary file not shown.
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 |
---|---|---|
|
@@ -2,7 +2,6 @@ | |
using System.Linq; | ||
using Verse.AI; | ||
using Verse; | ||
using UnityEngine.VR; | ||
|
||
namespace AchtungMod | ||
{ | ||
|
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,35 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace AchtungMod | ||
{ | ||
public class QuotaCache<S, T> | ||
{ | ||
private readonly Dictionary<S, (T value, int count)> cache = new(); | ||
private readonly int maxRetrievals; | ||
|
||
public QuotaCache(int maxRetrievals) | ||
{ | ||
this.maxRetrievals = maxRetrievals; | ||
} | ||
|
||
private void Add(S key, T value) | ||
{ | ||
cache[key] = (value, 0); | ||
} | ||
|
||
public T Get(S key, Func<S, T> fetchCallback) | ||
{ | ||
if (!cache.ContainsKey(key) || cache[key].count >= maxRetrievals) | ||
{ | ||
T newValue = fetchCallback(key); | ||
Add(key, newValue); | ||
} | ||
|
||
var (value, count) = cache[key]; | ||
cache[key] = (value, count + 1); | ||
|
||
return value; | ||
} | ||
} | ||
} |