Skip to content

Commit 3b12514

Browse files
Remove CacheOptions from DiskBasedResponseCache (#6395)
Details for this change are available in #6387. Fixes #6387
1 parent 8271d4e commit 3b12514

File tree

9 files changed

+49
-289
lines changed

9 files changed

+49
-289
lines changed

src/Libraries/Microsoft.Extensions.AI.Evaluation.Reporting.Azure/Storage/AzureStorageResponseCache.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ internal sealed partial class AzureStorageResponseCache(
4040
private const string EntryAndContentsFilesNotFound = "Cache entry file {0} and contents file {1} were not found.";
4141

4242
private readonly string _iterationPath = $"cache/{scenarioName}/{iterationName}";
43+
private readonly Func<DateTime> _provideDateTime = provideDateTime;
4344
private readonly TimeSpan _timeToLiveForCacheEntries =
4445
timeToLiveForCacheEntries ?? Defaults.DefaultTimeToLiveForCacheEntries;
45-
private readonly Func<DateTime> _provideDateTime = provideDateTime;
4646

4747
public byte[]? Get(string key)
4848
{

src/Libraries/Microsoft.Extensions.AI.Evaluation.Reporting/CSharp/Defaults.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ public static class Defaults
3131
/// </summary>
3232
public static TimeSpan DefaultTimeToLiveForCacheEntries { get; } = TimeSpan.FromDays(14);
3333

34-
/// <summary>
35-
/// Defines the version number for the reporting format. If and when the serialized format undergoes
36-
/// breaking changes, this number will be incremented.
37-
/// </summary>
34+
// Defines the version number for the reporting format. If and when the serialized format undergoes
35+
// breaking changes, this number should be incremented.
3836
internal const int ReportingFormatVersion = 1;
3937
}

src/Libraries/Microsoft.Extensions.AI.Evaluation.Reporting/CSharp/JsonSerialization/JsonUtilities.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ internal static class Default
1919
internal static JsonSerializerOptions Options => _options ??= CreateJsonSerializerOptions(writeIndented: true);
2020
internal static JsonTypeInfo<Dataset> DatasetTypeInfo => Options.GetTypeInfo<Dataset>();
2121
internal static JsonTypeInfo<CacheEntry> CacheEntryTypeInfo => Options.GetTypeInfo<CacheEntry>();
22-
internal static JsonTypeInfo<CacheOptions> CacheOptionsTypeInfo => Options.GetTypeInfo<CacheOptions>();
2322
internal static JsonTypeInfo<ScenarioRunResult> ScenarioRunResultTypeInfo => Options.GetTypeInfo<ScenarioRunResult>();
2423
}
2524

@@ -29,7 +28,6 @@ internal static class Compact
2928
internal static JsonSerializerOptions Options => _options ??= CreateJsonSerializerOptions(writeIndented: false);
3029
internal static JsonTypeInfo<Dataset> DatasetTypeInfo => Options.GetTypeInfo<Dataset>();
3130
internal static JsonTypeInfo<CacheEntry> CacheEntryTypeInfo => Options.GetTypeInfo<CacheEntry>();
32-
internal static JsonTypeInfo<CacheOptions> CacheOptionsTypeInfo => Options.GetTypeInfo<CacheOptions>();
3331
internal static JsonTypeInfo<ScenarioRunResult> ScenarioRunResultTypeInfo => Options.GetTypeInfo<ScenarioRunResult>();
3432
}
3533

@@ -50,12 +48,10 @@ private static JsonSerializerOptions CreateJsonSerializerOptions(bool writeInden
5048
[JsonSerializable(typeof(EvaluationResult))]
5149
[JsonSerializable(typeof(Dataset))]
5250
[JsonSerializable(typeof(CacheEntry))]
53-
[JsonSerializable(typeof(CacheOptions))]
5451
[JsonSourceGenerationOptions(
5552
Converters = [
5653
typeof(CamelCaseEnumConverter<EvaluationDiagnosticSeverity>),
5754
typeof(CamelCaseEnumConverter<EvaluationRating>),
58-
typeof(CamelCaseEnumConverter<CacheMode>),
5955
typeof(TimeSpanConverter),
6056
typeof(EvaluationContextConverter)
6157
],

src/Libraries/Microsoft.Extensions.AI.Evaluation.Reporting/CSharp/Storage/DiskBasedResponseCache.CacheMode.cs

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/Libraries/Microsoft.Extensions.AI.Evaluation.Reporting/CSharp/Storage/DiskBasedResponseCache.CacheOptions.cs

Lines changed: 0 additions & 90 deletions
This file was deleted.

src/Libraries/Microsoft.Extensions.AI.Evaluation.Reporting/CSharp/Storage/DiskBasedResponseCache.cs

Lines changed: 30 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
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

Comments
 (0)