| File: Storage\AzureStorageResponseCacheProvider.cs | Web Access |
| Project: src\src\Libraries\Microsoft.Extensions.AI.Evaluation.Reporting.Azure\Microsoft.Extensions.AI.Evaluation.Reporting.Azure.csproj (Microsoft.Extensions.AI.Evaluation.Reporting.Azure) |
// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using System; using System.Threading; using System.Threading.Tasks; using Azure.Storage.Files.DataLake; using Microsoft.Extensions.Caching.Distributed; namespace Microsoft.Extensions.AI.Evaluation.Reporting.Storage; /// <summary> /// An <see cref="IEvaluationResponseCacheProvider"/> that returns an <see cref="IDistributedCache"/> that can cache AI /// responses for a particular <see cref="ScenarioRun"/> under an Azure Storage container. /// </summary> /// <param name="client"> /// A <see cref="DataLakeDirectoryClient"/> with access to an Azure Storage container under which the cached AI /// responses should be stored. /// </param> /// <param name="timeToLiveForCacheEntries"> /// An optional <see cref="TimeSpan"/> that specifies the maximum amount of time that cached AI responses should /// survive in the cache before they are considered expired and evicted. /// </param> public sealed class AzureStorageResponseCacheProvider( DataLakeDirectoryClient client, TimeSpan? timeToLiveForCacheEntries = null) : IEvaluationResponseCacheProvider { private readonly Func<DateTime> _provideDateTime = () => DateTime.Now; /// <remarks> /// Intended for testing purposes only. /// </remarks> internal AzureStorageResponseCacheProvider( DataLakeDirectoryClient client, Func<DateTime> provideDateTime, TimeSpan? timeToLiveForCacheEntries = null) : this(client, timeToLiveForCacheEntries) { _provideDateTime = provideDateTime; } /// <inheritdoc/> public ValueTask<IDistributedCache> GetCacheAsync( string scenarioName, string iterationName, CancellationToken cancellationToken = default) { var cache = new AzureStorageResponseCache( client, scenarioName, iterationName, _provideDateTime, timeToLiveForCacheEntries); return new ValueTask<IDistributedCache>(cache); } /// <inheritdoc/> public ValueTask ResetAsync(CancellationToken cancellationToken = default) => AzureStorageResponseCache.ResetStorageAsync(client, cancellationToken); /// <inheritdoc/> public ValueTask DeleteExpiredCacheEntriesAsync(CancellationToken cancellationToken = default) => AzureStorageResponseCache.DeleteExpiredEntriesAsync(client, _provideDateTime, cancellationToken); }