11// Licensed to the .NET Foundation under one or more agreements.
22// The .NET Foundation licenses this file to you under the MIT license.
33
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+
49#pragma warning disable CA1725
510// CA1725: Parameter names should match base declaration.
611// All functions on 'IDistributedCache' use the parameter name 'token' in place of 'cancellationToken'. However,
@@ -26,90 +31,63 @@ internal sealed partial class DiskBasedResponseCache : IDistributedCache
2631 private readonly string _scenarioName ;
2732 private readonly string _iterationName ;
2833
29- private readonly CacheOptions _options ;
3034 private readonly string _iterationPath ;
3135 private readonly Func < DateTime > _provideDateTime ;
36+ private readonly TimeSpan _timeToLiveForCacheEntries ;
3237
33- public DiskBasedResponseCache (
38+ internal DiskBasedResponseCache (
3439 string storageRootPath ,
3540 string scenarioName ,
3641 string iterationName ,
37- Func < DateTime > provideDateTime )
42+ Func < DateTime > provideDateTime ,
43+ TimeSpan ? timeToLiveForCacheEntries = null )
3844 {
3945 _scenarioName = scenarioName ;
4046 _iterationName = iterationName ;
4147
4248 storageRootPath = Path . GetFullPath ( storageRootPath ) ;
4349 string cacheRootPath = GetCacheRootPath ( storageRootPath ) ;
44- string optionsFilePath = GetOptionsFilePath ( cacheRootPath ) ;
45- _options = File . Exists ( optionsFilePath ) ? CacheOptions . Read ( optionsFilePath ) : CacheOptions . Default ;
50+
4651 _iterationPath = Path . Combine ( cacheRootPath , scenarioName , iterationName ) ;
4752 _provideDateTime = provideDateTime ;
53+ _timeToLiveForCacheEntries = timeToLiveForCacheEntries ?? Defaults . DefaultTimeToLiveForCacheEntries ;
4854 }
4955
5056 public byte [ ] ? Get ( string key )
5157 {
52- if ( _options . Mode is CacheMode . Disabled )
53- {
54- return null ;
55- }
56-
5758 ( _ , string entryFilePath , string contentsFilePath , bool filesExist ) = GetPaths ( key ) ;
59+
5860 if ( ! filesExist )
5961 {
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 ;
6863 }
6964
70- if ( _options . Mode is not CacheMode . EnabledOfflineOnly )
65+ CacheEntry entry = CacheEntry . Read ( entryFilePath ) ;
66+ if ( entry . Expiration <= _provideDateTime ( ) )
7167 {
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 ;
7870 }
7971
8072 return File . ReadAllBytes ( contentsFilePath ) ;
8173 }
8274
8375 public async Task < byte [ ] ? > GetAsync ( string key , CancellationToken cancellationToken = default )
8476 {
85- if ( _options . Mode is CacheMode . Disabled )
86- {
87- return null ;
88- }
89-
9077 ( string _ , string entryFilePath , string contentsFilePath , bool filesExist ) = GetPaths ( key ) ;
78+
9179 if ( ! filesExist )
9280 {
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 ;
10182 }
10283
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 ) ;
10786
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 ;
11391 }
11492
11593#if NET
@@ -162,12 +140,8 @@ await stream.ReadAsync(
162140
163141 public void Refresh ( string key )
164142 {
165- if ( _options . Mode is CacheMode . Disabled or CacheMode . EnabledOfflineOnly )
166- {
167- return ;
168- }
169-
170143 ( _ , string entryFilePath , string contentsFilePath , bool filesExist ) = GetPaths ( key ) ;
144+
171145 if ( ! filesExist )
172146 {
173147 throw new FileNotFoundException (
@@ -184,12 +158,8 @@ public void Refresh(string key)
184158
185159 public async Task RefreshAsync ( string key , CancellationToken cancellationToken = default )
186160 {
187- if ( _options . Mode is CacheMode . Disabled or CacheMode . EnabledOfflineOnly )
188- {
189- return ;
190- }
191-
192161 ( _ , string entryFilePath , string contentsFilePath , bool filesExist ) = GetPaths ( key ) ;
162+
193163 if ( ! filesExist )
194164 {
195165 throw new FileNotFoundException (
@@ -206,33 +176,20 @@ public async Task RefreshAsync(string key, CancellationToken cancellationToken =
206176
207177 public void Remove ( string key )
208178 {
209- if ( _options . Mode is CacheMode . Disabled or CacheMode . EnabledOfflineOnly )
210- {
211- return ;
212- }
213-
214179 ( string keyPath , _ , _ , _ ) = GetPaths ( key ) ;
180+
215181 Directory . Delete ( keyPath , recursive : true ) ;
216182 }
217183
218184 public Task RemoveAsync ( string key , CancellationToken cancellationToken = default )
219185 {
220- if ( _options . Mode is CacheMode . Disabled or CacheMode . EnabledOfflineOnly )
221- {
222- return Task . CompletedTask ;
223- }
224-
225186 Remove ( key ) ;
187+
226188 return Task . CompletedTask ;
227189 }
228190
229191 public void Set ( string key , byte [ ] value , DistributedCacheEntryOptions options )
230192 {
231- if ( _options . Mode is CacheMode . Disabled or CacheMode . EnabledOfflineOnly )
232- {
233- return ;
234- }
235-
236193 ( string keyPath , string entryFilePath , string contentsFilePath , _ ) = GetPaths ( key ) ;
237194
238195 _ = Directory . CreateDirectory ( keyPath ) ;
@@ -249,11 +206,6 @@ public async Task SetAsync(
249206 DistributedCacheEntryOptions options ,
250207 CancellationToken cancellationToken = default )
251208 {
252- if ( _options . Mode is CacheMode . Disabled or CacheMode . EnabledOfflineOnly )
253- {
254- return ;
255- }
256-
257209 ( string keyPath , string entryFilePath , string contentsFilePath , _ ) = GetPaths ( key ) ;
258210
259211 Directory . CreateDirectory ( keyPath ) ;
@@ -334,9 +286,6 @@ await CacheEntry.ReadAsync(
334286 private static string GetCacheRootPath ( string storageRootPath )
335287 => Path . Combine ( storageRootPath , "cache" ) ;
336288
337- private static string GetOptionsFilePath ( string cacheRootPath )
338- => Path . Combine ( cacheRootPath , "options.json" ) ;
339-
340289 private static string GetEntryFilePath ( string keyPath )
341290 => Path . Combine ( keyPath , "entry.json" ) ;
342291
@@ -368,7 +317,7 @@ private static string GetContentsFilePath(string keyPath)
368317 private CacheEntry CreateEntry ( )
369318 {
370319 DateTime creation = _provideDateTime ( ) ;
371- DateTime expiration = creation . Add ( _options . TimeToLiveForCacheEntries ) ;
320+ DateTime expiration = creation . Add ( _timeToLiveForCacheEntries ) ;
372321
373322 return new CacheEntry ( _scenarioName , _iterationName , creation , expiration ) ;
374323 }
0 commit comments