Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ internal sealed partial class AzureStorageResponseCache(
private const string EntryAndContentsFilesNotFound = "Cache entry file {0} and contents file {1} were not found.";

private readonly string _iterationPath = $"cache/{scenarioName}/{iterationName}";
private readonly Func<DateTime> _provideDateTime = provideDateTime;
private readonly TimeSpan _timeToLiveForCacheEntries =
timeToLiveForCacheEntries ?? Defaults.DefaultTimeToLiveForCacheEntries;
private readonly Func<DateTime> _provideDateTime = provideDateTime;

public byte[]? Get(string key)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ public static class Defaults
/// </summary>
public static TimeSpan DefaultTimeToLiveForCacheEntries { get; } = TimeSpan.FromDays(14);

/// <summary>
/// Defines the version number for the reporting format. If and when the serialized format undergoes
/// breaking changes, this number will be incremented.
/// </summary>
// Defines the version number for the reporting format. If and when the serialized format undergoes
// breaking changes, this number should be incremented.
internal const int ReportingFormatVersion = 1;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ internal static class Default
internal static JsonSerializerOptions Options => _options ??= CreateJsonSerializerOptions(writeIndented: true);
internal static JsonTypeInfo<Dataset> DatasetTypeInfo => Options.GetTypeInfo<Dataset>();
internal static JsonTypeInfo<CacheEntry> CacheEntryTypeInfo => Options.GetTypeInfo<CacheEntry>();
internal static JsonTypeInfo<CacheOptions> CacheOptionsTypeInfo => Options.GetTypeInfo<CacheOptions>();
internal static JsonTypeInfo<ScenarioRunResult> ScenarioRunResultTypeInfo => Options.GetTypeInfo<ScenarioRunResult>();
}

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

Expand All @@ -50,12 +48,10 @@ private static JsonSerializerOptions CreateJsonSerializerOptions(bool writeInden
[JsonSerializable(typeof(EvaluationResult))]
[JsonSerializable(typeof(Dataset))]
[JsonSerializable(typeof(CacheEntry))]
[JsonSerializable(typeof(CacheOptions))]
[JsonSourceGenerationOptions(
Converters = [
typeof(CamelCaseEnumConverter<EvaluationDiagnosticSeverity>),
typeof(CamelCaseEnumConverter<EvaluationRating>),
typeof(CamelCaseEnumConverter<CacheMode>),
typeof(TimeSpanConverter),
typeof(EvaluationContextConverter)
],
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#pragma warning disable S3604
// S3604: Member initializer values should not be redundant.
// We disable this warning because it is a false positive arising from the analyzer's lack of support for C#'s primary
// constructor syntax.

#pragma warning disable CA1725
// CA1725: Parameter names should match base declaration.
// All functions on 'IDistributedCache' use the parameter name 'token' in place of 'cancellationToken'. However,
Expand All @@ -26,90 +31,63 @@ internal sealed partial class DiskBasedResponseCache : IDistributedCache
private readonly string _scenarioName;
private readonly string _iterationName;

private readonly CacheOptions _options;
private readonly string _iterationPath;
private readonly Func<DateTime> _provideDateTime;
private readonly TimeSpan _timeToLiveForCacheEntries;

public DiskBasedResponseCache(
internal DiskBasedResponseCache(
string storageRootPath,
string scenarioName,
string iterationName,
Func<DateTime> provideDateTime)
Func<DateTime> provideDateTime,
TimeSpan? timeToLiveForCacheEntries = null)
{
_scenarioName = scenarioName;
_iterationName = iterationName;

storageRootPath = Path.GetFullPath(storageRootPath);
string cacheRootPath = GetCacheRootPath(storageRootPath);
string optionsFilePath = GetOptionsFilePath(cacheRootPath);
_options = File.Exists(optionsFilePath) ? CacheOptions.Read(optionsFilePath) : CacheOptions.Default;

_iterationPath = Path.Combine(cacheRootPath, scenarioName, iterationName);
_provideDateTime = provideDateTime;
_timeToLiveForCacheEntries = timeToLiveForCacheEntries ?? Defaults.DefaultTimeToLiveForCacheEntries;
}

public byte[]? Get(string key)
{
if (_options.Mode is CacheMode.Disabled)
{
return null;
}

(_, string entryFilePath, string contentsFilePath, bool filesExist) = GetPaths(key);

if (!filesExist)
{
return _options.Mode is CacheMode.EnabledOfflineOnly
? throw new FileNotFoundException(
string.Format(
CultureInfo.CurrentCulture,
EntryAndContentsFilesNotFound,
entryFilePath,
contentsFilePath))
: null;
return null;
}

if (_options.Mode is not CacheMode.EnabledOfflineOnly)
CacheEntry entry = CacheEntry.Read(entryFilePath);
if (entry.Expiration <= _provideDateTime())
{
CacheEntry entry = CacheEntry.Read(entryFilePath);
if (entry.Expiration <= _provideDateTime())
{
Remove(key);
return null;
}
Remove(key);
return null;
}

return File.ReadAllBytes(contentsFilePath);
}

public async Task<byte[]?> GetAsync(string key, CancellationToken cancellationToken = default)
{
if (_options.Mode is CacheMode.Disabled)
{
return null;
}

(string _, string entryFilePath, string contentsFilePath, bool filesExist) = GetPaths(key);

if (!filesExist)
{
return _options.Mode is CacheMode.EnabledOfflineOnly
? throw new FileNotFoundException(
string.Format(
CultureInfo.CurrentCulture,
EntryAndContentsFilesNotFound,
entryFilePath,
contentsFilePath))
: null;
return null;
}

if (_options.Mode is not CacheMode.EnabledOfflineOnly)
{
CacheEntry entry =
await CacheEntry.ReadAsync(entryFilePath, cancellationToken: cancellationToken).ConfigureAwait(false);
CacheEntry entry =
await CacheEntry.ReadAsync(entryFilePath, cancellationToken: cancellationToken).ConfigureAwait(false);

if (entry.Expiration <= _provideDateTime())
{
await RemoveAsync(key, cancellationToken).ConfigureAwait(false);
return null;
}
if (entry.Expiration <= _provideDateTime())
{
await RemoveAsync(key, cancellationToken).ConfigureAwait(false);
return null;
}

#if NET
Expand Down Expand Up @@ -162,12 +140,8 @@ await stream.ReadAsync(

public void Refresh(string key)
{
if (_options.Mode is CacheMode.Disabled or CacheMode.EnabledOfflineOnly)
{
return;
}

(_, string entryFilePath, string contentsFilePath, bool filesExist) = GetPaths(key);

if (!filesExist)
{
throw new FileNotFoundException(
Expand All @@ -184,12 +158,8 @@ public void Refresh(string key)

public async Task RefreshAsync(string key, CancellationToken cancellationToken = default)
{
if (_options.Mode is CacheMode.Disabled or CacheMode.EnabledOfflineOnly)
{
return;
}

(_, string entryFilePath, string contentsFilePath, bool filesExist) = GetPaths(key);

if (!filesExist)
{
throw new FileNotFoundException(
Expand All @@ -206,33 +176,20 @@ public async Task RefreshAsync(string key, CancellationToken cancellationToken =

public void Remove(string key)
{
if (_options.Mode is CacheMode.Disabled or CacheMode.EnabledOfflineOnly)
{
return;
}

(string keyPath, _, _, _) = GetPaths(key);

Directory.Delete(keyPath, recursive: true);
}

public Task RemoveAsync(string key, CancellationToken cancellationToken = default)
{
if (_options.Mode is CacheMode.Disabled or CacheMode.EnabledOfflineOnly)
{
return Task.CompletedTask;
}

Remove(key);

return Task.CompletedTask;
}

public void Set(string key, byte[] value, DistributedCacheEntryOptions options)
{
if (_options.Mode is CacheMode.Disabled or CacheMode.EnabledOfflineOnly)
{
return;
}

(string keyPath, string entryFilePath, string contentsFilePath, _) = GetPaths(key);

_ = Directory.CreateDirectory(keyPath);
Expand All @@ -249,11 +206,6 @@ public async Task SetAsync(
DistributedCacheEntryOptions options,
CancellationToken cancellationToken = default)
{
if (_options.Mode is CacheMode.Disabled or CacheMode.EnabledOfflineOnly)
{
return;
}

(string keyPath, string entryFilePath, string contentsFilePath, _) = GetPaths(key);

Directory.CreateDirectory(keyPath);
Expand Down Expand Up @@ -334,9 +286,6 @@ await CacheEntry.ReadAsync(
private static string GetCacheRootPath(string storageRootPath)
=> Path.Combine(storageRootPath, "cache");

private static string GetOptionsFilePath(string cacheRootPath)
=> Path.Combine(cacheRootPath, "options.json");

private static string GetEntryFilePath(string keyPath)
=> Path.Combine(keyPath, "entry.json");

Expand Down Expand Up @@ -368,7 +317,7 @@ private static string GetContentsFilePath(string keyPath)
private CacheEntry CreateEntry()
{
DateTime creation = _provideDateTime();
DateTime expiration = creation.Add(_options.TimeToLiveForCacheEntries);
DateTime expiration = creation.Add(_timeToLiveForCacheEntries);

return new CacheEntry(_scenarioName, _iterationName, creation, expiration);
}
Expand Down
Loading
Loading