1
1
// Licensed to the .NET Foundation under one or more agreements.
2
2
// The .NET Foundation licenses this file to you under the MIT license.
3
3
4
+ #pragma warning disable S3604
5
+ // S3604: Member initializer values should not be redundant.
6
+ // We disable this warning because it is a false positive arising from the analyzer's lack of support for C#'s primary
7
+ // constructor syntax.
8
+
4
9
#pragma warning disable CA1725
5
10
// CA1725: Parameter names should match base declaration.
6
11
// All functions on 'IDistributedCache' use the parameter name 'token' in place of 'cancellationToken'. However,
@@ -26,90 +31,63 @@ internal sealed partial class DiskBasedResponseCache : IDistributedCache
26
31
private readonly string _scenarioName ;
27
32
private readonly string _iterationName ;
28
33
29
- private readonly CacheOptions _options ;
30
34
private readonly string _iterationPath ;
31
35
private readonly Func < DateTime > _provideDateTime ;
36
+ private readonly TimeSpan _timeToLiveForCacheEntries ;
32
37
33
- public DiskBasedResponseCache (
38
+ internal DiskBasedResponseCache (
34
39
string storageRootPath ,
35
40
string scenarioName ,
36
41
string iterationName ,
37
- Func < DateTime > provideDateTime )
42
+ Func < DateTime > provideDateTime ,
43
+ TimeSpan ? timeToLiveForCacheEntries = null )
38
44
{
39
45
_scenarioName = scenarioName ;
40
46
_iterationName = iterationName ;
41
47
42
48
storageRootPath = Path . GetFullPath ( storageRootPath ) ;
43
49
string cacheRootPath = GetCacheRootPath ( storageRootPath ) ;
44
- string optionsFilePath = GetOptionsFilePath ( cacheRootPath ) ;
45
- _options = File . Exists ( optionsFilePath ) ? CacheOptions . Read ( optionsFilePath ) : CacheOptions . Default ;
50
+
46
51
_iterationPath = Path . Combine ( cacheRootPath , scenarioName , iterationName ) ;
47
52
_provideDateTime = provideDateTime ;
53
+ _timeToLiveForCacheEntries = timeToLiveForCacheEntries ?? Defaults . DefaultTimeToLiveForCacheEntries ;
48
54
}
49
55
50
56
public byte [ ] ? Get ( string key )
51
57
{
52
- if ( _options . Mode is CacheMode . Disabled )
53
- {
54
- return null ;
55
- }
56
-
57
58
( _ , string entryFilePath , string contentsFilePath , bool filesExist ) = GetPaths ( key ) ;
59
+
58
60
if ( ! filesExist )
59
61
{
60
- return _options . Mode is CacheMode . EnabledOfflineOnly
61
- ? throw new FileNotFoundException (
62
- string . Format (
63
- CultureInfo . CurrentCulture ,
64
- EntryAndContentsFilesNotFound ,
65
- entryFilePath ,
66
- contentsFilePath ) )
67
- : null ;
62
+ return null ;
68
63
}
69
64
70
- if ( _options . Mode is not CacheMode . EnabledOfflineOnly )
65
+ CacheEntry entry = CacheEntry . Read ( entryFilePath ) ;
66
+ if ( entry . Expiration <= _provideDateTime ( ) )
71
67
{
72
- CacheEntry entry = CacheEntry . Read ( entryFilePath ) ;
73
- if ( entry . Expiration <= _provideDateTime ( ) )
74
- {
75
- Remove ( key ) ;
76
- return null ;
77
- }
68
+ Remove ( key ) ;
69
+ return null ;
78
70
}
79
71
80
72
return File . ReadAllBytes ( contentsFilePath ) ;
81
73
}
82
74
83
75
public async Task < byte [ ] ? > GetAsync ( string key , CancellationToken cancellationToken = default )
84
76
{
85
- if ( _options . Mode is CacheMode . Disabled )
86
- {
87
- return null ;
88
- }
89
-
90
77
( string _ , string entryFilePath , string contentsFilePath , bool filesExist ) = GetPaths ( key ) ;
78
+
91
79
if ( ! filesExist )
92
80
{
93
- return _options . Mode is CacheMode . EnabledOfflineOnly
94
- ? throw new FileNotFoundException (
95
- string . Format (
96
- CultureInfo . CurrentCulture ,
97
- EntryAndContentsFilesNotFound ,
98
- entryFilePath ,
99
- contentsFilePath ) )
100
- : null ;
81
+ return null ;
101
82
}
102
83
103
- if ( _options . Mode is not CacheMode . EnabledOfflineOnly )
104
- {
105
- CacheEntry entry =
106
- await CacheEntry . ReadAsync ( entryFilePath , cancellationToken : cancellationToken ) . ConfigureAwait ( false ) ;
84
+ CacheEntry entry =
85
+ await CacheEntry . ReadAsync ( entryFilePath , cancellationToken : cancellationToken ) . ConfigureAwait ( false ) ;
107
86
108
- if ( entry . Expiration <= _provideDateTime ( ) )
109
- {
110
- await RemoveAsync ( key , cancellationToken ) . ConfigureAwait ( false ) ;
111
- return null ;
112
- }
87
+ if ( entry . Expiration <= _provideDateTime ( ) )
88
+ {
89
+ await RemoveAsync ( key , cancellationToken ) . ConfigureAwait ( false ) ;
90
+ return null ;
113
91
}
114
92
115
93
#if NET
@@ -162,12 +140,8 @@ await stream.ReadAsync(
162
140
163
141
public void Refresh ( string key )
164
142
{
165
- if ( _options . Mode is CacheMode . Disabled or CacheMode . EnabledOfflineOnly )
166
- {
167
- return ;
168
- }
169
-
170
143
( _ , string entryFilePath , string contentsFilePath , bool filesExist ) = GetPaths ( key ) ;
144
+
171
145
if ( ! filesExist )
172
146
{
173
147
throw new FileNotFoundException (
@@ -184,12 +158,8 @@ public void Refresh(string key)
184
158
185
159
public async Task RefreshAsync ( string key , CancellationToken cancellationToken = default )
186
160
{
187
- if ( _options . Mode is CacheMode . Disabled or CacheMode . EnabledOfflineOnly )
188
- {
189
- return ;
190
- }
191
-
192
161
( _ , string entryFilePath , string contentsFilePath , bool filesExist ) = GetPaths ( key ) ;
162
+
193
163
if ( ! filesExist )
194
164
{
195
165
throw new FileNotFoundException (
@@ -206,33 +176,20 @@ public async Task RefreshAsync(string key, CancellationToken cancellationToken =
206
176
207
177
public void Remove ( string key )
208
178
{
209
- if ( _options . Mode is CacheMode . Disabled or CacheMode . EnabledOfflineOnly )
210
- {
211
- return ;
212
- }
213
-
214
179
( string keyPath , _ , _ , _ ) = GetPaths ( key ) ;
180
+
215
181
Directory . Delete ( keyPath , recursive : true ) ;
216
182
}
217
183
218
184
public Task RemoveAsync ( string key , CancellationToken cancellationToken = default )
219
185
{
220
- if ( _options . Mode is CacheMode . Disabled or CacheMode . EnabledOfflineOnly )
221
- {
222
- return Task . CompletedTask ;
223
- }
224
-
225
186
Remove ( key ) ;
187
+
226
188
return Task . CompletedTask ;
227
189
}
228
190
229
191
public void Set ( string key , byte [ ] value , DistributedCacheEntryOptions options )
230
192
{
231
- if ( _options . Mode is CacheMode . Disabled or CacheMode . EnabledOfflineOnly )
232
- {
233
- return ;
234
- }
235
-
236
193
( string keyPath , string entryFilePath , string contentsFilePath , _ ) = GetPaths ( key ) ;
237
194
238
195
_ = Directory . CreateDirectory ( keyPath ) ;
@@ -249,11 +206,6 @@ public async Task SetAsync(
249
206
DistributedCacheEntryOptions options ,
250
207
CancellationToken cancellationToken = default )
251
208
{
252
- if ( _options . Mode is CacheMode . Disabled or CacheMode . EnabledOfflineOnly )
253
- {
254
- return ;
255
- }
256
-
257
209
( string keyPath , string entryFilePath , string contentsFilePath , _ ) = GetPaths ( key ) ;
258
210
259
211
Directory . CreateDirectory ( keyPath ) ;
@@ -334,9 +286,6 @@ await CacheEntry.ReadAsync(
334
286
private static string GetCacheRootPath ( string storageRootPath )
335
287
=> Path . Combine ( storageRootPath , "cache" ) ;
336
288
337
- private static string GetOptionsFilePath ( string cacheRootPath )
338
- => Path . Combine ( cacheRootPath , "options.json" ) ;
339
-
340
289
private static string GetEntryFilePath ( string keyPath )
341
290
=> Path . Combine ( keyPath , "entry.json" ) ;
342
291
@@ -368,7 +317,7 @@ private static string GetContentsFilePath(string keyPath)
368
317
private CacheEntry CreateEntry ( )
369
318
{
370
319
DateTime creation = _provideDateTime ( ) ;
371
- DateTime expiration = creation . Add ( _options . TimeToLiveForCacheEntries ) ;
320
+ DateTime expiration = creation . Add ( _timeToLiveForCacheEntries ) ;
372
321
373
322
return new CacheEntry ( _scenarioName , _iterationName , creation , expiration ) ;
374
323
}
0 commit comments