-
Notifications
You must be signed in to change notification settings - Fork 9
/
DistributedFileStoreCacheString.cs
129 lines (113 loc) · 6.26 KB
/
DistributedFileStoreCacheString.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
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
// 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 Microsoft.Extensions.Caching.Distributed;
using Net.DistributedFileStoreCache.SupportCode;
namespace Net.DistributedFileStoreCache;
/// <summary>
/// This is the Distributed FileStore cache that has a value of type string.
/// This is the primary FileStore cache version that the other versions link to this class
/// </summary>
public class DistributedFileStoreCacheString : IDistributedFileStoreCacheString
{
/// <summary>
/// This class directly creates the <see cref="CacheFileHandler"/> which provides read/write access of the cache json file
/// </summary>
protected readonly CacheFileHandler CacheFileHandler;
/// <summary>
/// ctor
/// </summary>
/// <param name="fileStoreCacheOptions"></param>
public DistributedFileStoreCacheString(DistributedFileStoreCacheOptions fileStoreCacheOptions)
{
CacheFileHandler = new CacheFileHandler(fileStoreCacheOptions);
}
/// <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 CacheFileHandler.GetValue(key);
}
/// <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 CacheFileHandler.GetValueAsync(key, token);
}
/// <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)
{
CacheFileHandler.SetKeyValue(key, value, options);
}
/// <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,
CancellationToken token = new CancellationToken())
{
return CacheFileHandler.SetKeyValueAsync(key, value, options, token);
}
/// <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)
{
CacheFileHandler.SetKeyValueMany(manyEntries, options);
}
/// <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,
CancellationToken token = new ())
{
return CacheFileHandler.SetKeyValueManyAsync(manyEntries, options, token);
}
/// <summary>Removes the value with the given key.</summary>
/// <param name="key">A string identifying the requested value.</param>
public void Remove(string key)
{
CacheFileHandler.RemoveKeyValue(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())
{
return CacheFileHandler.RemoveKeyValueAsync(key, token);
}
/// <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)
{
CacheFileHandler.ResetCacheFile(manyEntries, entryOptions);
}
/// <summary>
/// This return all the cached values as a dictionary
/// </summary>
/// <returns></returns>
public IReadOnlyDictionary<string, string> GetAllKeyValues()
{
return CacheFileHandler.GetAllValues();
}
/// <summary>
/// This return all the cached values as a dictionary
/// </summary>
/// <returns></returns>
public Task<IReadOnlyDictionary<string, string>> GetAllKeyValuesAsync(CancellationToken token = new CancellationToken())
{
return CacheFileHandler.GetAllValuesAsync(token);
}
}