-
Notifications
You must be signed in to change notification settings - Fork 1
/
IMRUCache.cs
67 lines (61 loc) · 2.17 KB
/
IMRUCache.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using System.Collections.Generic;
namespace Grammophone.Caching
{
/// <summary>
/// Interface for a cache of <typeparamref name="V"/> items indexed by
/// key type <typeparamref name="K"/>. Items are kept up
/// to a <see cref="MaxCount"/>, above which the least recently
/// used ones are evicted.
/// </summary>
/// <typeparam name="K">The type of item keys.</typeparam>
/// <typeparam name="V">The type of items cached.</typeparam>
public interface IMRUCache<K, V>
{
/// <summary>
/// The maximum items count in the cache.
/// </summary>
int MaxCount { get; set; }
/// <summary>
/// Evict all items from the cache.
/// </summary>
/// <returns>
/// Returns the evicted cache entries.
/// </returns>
IEnumerable<KeyValuePair<K, V>> Clear();
/// <summary>
/// Get an item from the cache or return a newly created one.
/// </summary>
/// <param name="key">The key defining the item.</param>
/// <returns>
/// Returns the item corresponding to the supplied <paramref name="key"/>,
/// either from the cache, in case of a cache miss, or newly created.
/// In the latter case, the item is inserted in the cache as the most
/// recently used.
/// </returns>
/// <remarks>
/// If the cache is full and there is a cache miss,
/// the cache makes room form the new item until
/// the total size reaches <see cref="MaxCount"/> by evicting
/// the least recently used items.
/// </remarks>
V Get(K key);
/// <summary>
/// Removes an item from the cache.
/// </summary>
/// <param name="key">The key defining the item.</param>
/// <returns>Returns true if the item was found in the cache, else false.</returns>
bool Remove(K key);
/// <summary>
/// Removes an item from the cache.
/// </summary>
/// <param name="key">The key defining the item.</param>
/// <param name="value">Set to the removed value, if found in the cache.</param>
/// <returns>Returns true if the item was found in the cache, else false.</returns>
bool Remove(K key, out V value);
/// <summary>
/// Remove the least recently used item.
/// </summary>
/// <returns>Returns the item entry found, else null.</returns>
KeyValuePair<K, V>? RemoveLRU();
}
}