-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathStubFileStoreCacheString.cs
More file actions
130 lines (114 loc) · 6.08 KB
/
Copy pathStubFileStoreCacheString.cs
File metadata and controls
130 lines (114 loc) · 6.08 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright (c) 2022 Jon P Smith, GitHub: JonPSmith, web: http://www.thereformedprogrammer.net/
// Licensed under MIT license. See License.txt in the project root for license information.
using System.Collections.ObjectModel;
using Microsoft.Extensions.Caching.Distributed;
using Net.DistributedFileStoreCache;
namespace Test.TestHelpers;
public class StubFileStoreCacheString : IDistributedFileStoreCacheString
{
private Dictionary<string, string> _cache = new Dictionary<string, string>();
/// <summary>Gets a value with the given key.</summary>
/// <param name="key">A string identifying the requested value.</param>
/// <returns>The located value or null.</returns>
public string? Get(string key)
{
return _cache.TryGetValue(key, out string? value) ? value : null;
}
/// <summary>Gets a value with the given key.</summary>
/// <param name="key">A string identifying the requested value.</param>
/// <param name="token">Optional. The <see cref="T:System.Threading.CancellationToken" /> used to propagate notifications that the operation should be canceled.</param>
/// <returns>The <see cref="T:System.Threading.Tasks.Task" /> that represents the asynchronous operation, containing the located value or null.</returns>
public Task<string?> GetAsync(string key, CancellationToken token = new CancellationToken())
{
return Task.FromResult(_cache.TryGetValue(key, out string? value) ? value : null);
}
/// <summary>Sets a value with the given key.</summary>
/// <param name="key">A string identifying the requested value.</param>
/// <param name="value">The value to set in the cache.</param>
/// <param name="options">The cache options for the value.</param>
public void Set(string key, string value, DistributedCacheEntryOptions? options = null)
{
_cache[key] = value;
}
/// <summary>Sets the value with the given key.</summary>
/// <param name="key">A string identifying the requested value.</param>
/// <param name="value">The value to set in the cache.</param>
/// <param name="options">The cache options for the value.</param>
/// <param name="token">Optional. The <see cref="T:System.Threading.CancellationToken" /> used to propagate notifications that the operation should be canceled.</param>
/// <returns>The <see cref="T:System.Threading.Tasks.Task" /> that represents the asynchronous operation.</returns>
public Task SetAsync(string key, string value, DistributedCacheEntryOptions? options = null,
CancellationToken token = new CancellationToken())
{
_cache[key] = value;
return Task.CompletedTask;
}
/// <summary>Sets many entries via a list of KeyValues</summary>
/// <param name="manyEntries">List of KeyValuePairs to be added to the cache.</param>
/// <param name="options">Optional: The cache options for the value.</param>
public void SetMany(List<KeyValuePair<string, string>> manyEntries, DistributedCacheEntryOptions? options = null)
{
foreach (var keyValuePair in manyEntries)
{
_cache[keyValuePair.Key] = keyValuePair.Value;
}
}
/// <summary>Sets many entries via a list of KeyValues</summary>
/// <param name="manyEntries">List of KeyValuePairs to be added to the cache.</param>
/// <param name="options">Optional: The cache options for the value.</param>
/// <param name="token">Optional. The <see cref="T:System.Threading.CancellationToken" /> used to propagate notifications that the operation should be canceled.</param>
public Task SetManyAsync(List<KeyValuePair<string, string>> manyEntries, DistributedCacheEntryOptions? options = null,
CancellationToken token = new CancellationToken())
{
foreach (var keyValuePair in manyEntries)
{
_cache[keyValuePair.Key] = keyValuePair.Value;
}
return Task.CompletedTask;
}
/// <summary>Removes the value with the given key.</summary>
/// <param name="key">A string identifying the requested value.</param>
public void Remove(string key)
{
_cache.Remove(key);
}
/// <summary>Removes the value with the given key.</summary>
/// <param name="key">A string identifying the requested value.</param>
/// <param name="token">Optional. The <see cref="T:System.Threading.CancellationToken" /> used to propagate notifications that the operation should be canceled.</param>
/// <returns>The <see cref="T:System.Threading.Tasks.Task" /> that represents the asynchronous operation.</returns>
public Task RemoveAsync(string key, CancellationToken token = new CancellationToken())
{
_cache.Remove(key);
return Task.CompletedTask;
}
/// <summary>
/// This clears all the key/value pairs from the json cache file, with option to add entries after the cache is cleared.
/// </summary>
/// <param name="manyEntries">Optional: After of the clearing the cache these KeyValues will written into the cache</param>
/// <param name="entryOptions">Optional: If there are entries to add to the cache, this will set the timeout time.</param>
public void ClearAll(List<KeyValuePair<string, string>>? manyEntries = null, DistributedCacheEntryOptions? entryOptions = null)
{
_cache = new Dictionary<string, string>();
if (manyEntries == null)
return;
foreach (var keyValuePair in manyEntries)
{
_cache[keyValuePair.Key] = keyValuePair.Value;
}
}
/// <summary>
/// This return all the cached values as a dictionary
/// </summary>
/// <returns></returns>
public IReadOnlyDictionary<string, string> GetAllKeyValues()
{
return new ReadOnlyDictionary<string, string>(_cache);
}
/// <summary>
/// This return all the cached values as a dictionary
/// </summary>
/// <returns></returns>
public Task<IReadOnlyDictionary<string, string>> GetAllKeyValuesAsync(CancellationToken token = new CancellationToken())
{
return Task.FromResult(new ReadOnlyDictionary<string, string>(_cache) as IReadOnlyDictionary<string, string>);
}
}