Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace in-memory caching, with Redis caching. #59

Merged
merged 3 commits into from
Apr 27, 2023
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
1 change: 1 addition & 0 deletions app/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<AzureStorageBlobsVersion>12.16.0</AzureStorageBlobsVersion>
<SemanticKernelVersion>0.13.277.1-preview</SemanticKernelVersion>
<AzureOpenAIVersion>1.0.0-beta.5</AzureOpenAIVersion>
<WebAssemblyDevServerVersion>7.0.5</WebAssemblyDevServerVersion>
</PropertyGroup>

</Project>
3 changes: 2 additions & 1 deletion app/backend/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
global using System.Diagnostics;
global using System.Runtime.CompilerServices;
global using System.Text;
global using System.Text.Json;
global using Azure.AI.OpenAI;
global using Azure.Identity;
global using Azure.Search.Documents;
global using Azure.Search.Documents.Models;
global using Azure.Storage.Blobs;
global using Microsoft.AspNetCore.Mvc;
global using Microsoft.AspNetCore.Mvc.RazorPages;
global using Microsoft.Extensions.Caching.Memory;
global using Microsoft.Extensions.Caching.Distributed;
global using Microsoft.Net.Http.Headers;
global using Microsoft.SemanticKernel;
global using Microsoft.SemanticKernel.AI;
Expand Down
3 changes: 2 additions & 1 deletion app/backend/MinimalApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
<PackageReference Include="Azure.Search.Documents" Version="$(AzureSearchDocumentsVersion)" />
<PackageReference Include="Azure.Storage.Blobs" Version="$(AzureStorageBlobsVersion)" />
<PackageReference Include="Azure.AI.OpenAI" Version="$(AzureOpenAIVersion)" />
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="7.0.5" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="7.0.4" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="$(WebAssemblyDevServerVersion)" />
<PackageReference Include="Microsoft.SemanticKernel" Version="$(SemanticKernelVersion)" />
</ItemGroup>

Expand Down
14 changes: 13 additions & 1 deletion app/backend/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,25 @@
// See: https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddMemoryCache();
builder.Services.AddOutputCache();
builder.Services.AddControllersWithViews();
builder.Services.AddRazorPages();
builder.Services.AddCrossOriginResourceSharing();
builder.Services.AddAzureServices();

if (builder.Environment.IsDevelopment())
{
builder.Services.AddDistributedMemoryCache();
}
else
{
builder.Services.AddStackExchangeRedisCache(options =>
IEvangelist marked this conversation as resolved.
Show resolved Hide resolved
{
options.Configuration = builder.Configuration.GetConnectionString("RedisCacheConnectionString");
options.InstanceName = builder.Configuration["RedisCacheInstanceName"];
});
}

var app = builder.Build();
if (app.Environment.IsDevelopment())
{
Expand Down
101 changes: 67 additions & 34 deletions app/backend/Services/ApproachServiceResponseFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ internal sealed class ApproachServiceResponseFactory
{
private readonly ILogger<ApproachServiceResponseFactory> _logger;
private readonly IEnumerable<IApproachBasedService> _approachBasedServices;
private readonly IMemoryCache _cache;
private readonly IDistributedCache _cache;

public ApproachServiceResponseFactory(
ILogger<ApproachServiceResponseFactory> logger,
IEnumerable<IApproachBasedService> services, IMemoryCache cache) =>
IEnumerable<IApproachBasedService> services, IDistributedCache cache) =>
(_logger, _approachBasedServices, _cache) = (logger, services, cache);

internal async Task<ApproachResponse> GetApproachResponseAsync(
Expand All @@ -24,41 +24,74 @@ internal async Task<ApproachResponse> GetApproachResponseAsync(
?? throw new ArgumentOutOfRangeException(
nameof(approach), $"Approach: {approach} value isn't supported.");

var key = new CacheKey(approach, question, overrides)
.ToCacheKeyString();

var options = new JsonSerializerOptions(JsonSerializerDefaults.Web);

// If the value is cached, return it.
var cachedValue = await _cache.GetStringAsync(key, cancellationToken);
if (cachedValue is { Length: > 0 } &&
JsonSerializer.Deserialize<ApproachResponse>(cachedValue, options) is ApproachResponse cachedResponse)
{
_logger.LogDebug(
"Returning cached value for key ({Key}): {Approach}\n{Response}",
key, approach, cachedResponse);

return cachedResponse;
}

var approachResponse =
await _cache.GetOrCreateAsync(
new CacheKey(approach, question, overrides),
async entry =>
{
// Cache each unique request for 30 mins and log when evicted...
entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(30);
entry.RegisterPostEvictionCallback(OnPostEviction, _logger);

var response = await service.ReplyAsync(question, overrides, cancellationToken);

_logger.LogInformation("{Approach}\n{Response}", approach, response);

return response;

static void OnPostEviction(
object key, object? value, EvictionReason reason, object? state)
{
if (value is ApproachResponse response &&
state is ILogger<ApproachServiceResponseFactory> logger)
{
logger.LogInformation(
"Evicted cached approach response: {Response}",
response);
}
}
});

return approachResponse ?? throw new AIException(
AIException.ErrorCodes.ServiceError,
$"The approach response for '{approach}' was null.");
await service.ReplyAsync(question, overrides, cancellationToken)
?? throw new AIException(
AIException.ErrorCodes.ServiceError,
$"The approach response for '{approach}' was null.");

var json = JsonSerializer.Serialize(approachResponse, options);
var entryOptions = new DistributedCacheEntryOptions
{
// Cache each unique request for 30 mins and log when evicted...
AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(30)
};

await _cache.SetStringAsync(key, json, entryOptions, cancellationToken);

_logger.LogDebug(
"Returning new value for key ({Key}): {Approach}\n{Response}",
key, approach, approachResponse);

return approachResponse;
}
}

readonly file record struct CacheKey(
internal readonly record struct CacheKey(
Approach Approach,
string Question,
RequestOverrides? Overrides);
RequestOverrides? Overrides)
{
/// <summary>
/// Converts the given <paramref name="cacheKey"/> instance into a <see cref="string"/>
/// that will uniquely identify the approach, question and optional override pairing.
/// </summary>
internal string ToCacheKeyString()
{
var (approach, question, overrides) = this;

string? overridesString = null;
if (overrides is { } o)
{
static string Bit(bool value) => value ? "1" : "0";

var bits = $"""
{Bit(o.SemanticCaptions.GetValueOrDefault())}.{Bit(o.SemanticRanker)}.{Bit(o.SuggestFollowupQuestions)}
""";

overridesString =
$":{o.ExcludeCategory}-{o.PromptTemplate}-{o.PromptTemplatePrefix}-{o.PromptTemplateSuffix}-{bits}";
}

return $"""
{approach}:{question}{overridesString}
""";
}
}
2 changes: 1 addition & 1 deletion app/frontend/ClientApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="7.0.5" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="7.0.5" PrivateAssets="all" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="$(WebAssemblyDevServerVersion)" PrivateAssets="all" />
<PackageReference Include="Microsoft.Extensions.Http" Version="7.0.0" />
<PackageReference Include="Blazor.LocalStorage.WebAssembly" Version="7.0.3" />
<PackageReference Include="Blazor.SessionStorage.WebAssembly" Version="7.0.3" />
Expand Down