-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: CacheBehavior caches value even when model request is mutated do…
…wnstream
- Loading branch information
1 parent
2518502
commit 4c2fac0
Showing
3 changed files
with
30 additions
and
59 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,51 @@ | ||
using MediatR; | ||
using System.Security.Cryptography; | ||
using System.Text; | ||
using System.Text.Json; | ||
using Cellm.AddIn; | ||
using MediatR; | ||
using Microsoft.Extensions.Caching.Memory; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Cellm.Models.PipelineBehavior; | ||
|
||
internal class CachingBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse> | ||
where TRequest : IModelRequest<TResponse> | ||
where TResponse : IModelResponse | ||
{ | ||
private readonly Cache _cache; | ||
private readonly IMemoryCache _memoryCache; | ||
private readonly MemoryCacheEntryOptions _memoryCacheEntryOptions; | ||
|
||
public CachingBehavior(Cache cache) | ||
public CachingBehavior(IMemoryCache memoryCache, IOptions<CellmConfiguration> _cellmConfiguration) | ||
{ | ||
_cache = cache; | ||
_memoryCache = memoryCache; | ||
_memoryCacheEntryOptions = new() | ||
{ | ||
SlidingExpiration = TimeSpan.FromSeconds(_cellmConfiguration.Value.CacheTimeoutInSeconds) | ||
}; | ||
} | ||
|
||
public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken) | ||
{ | ||
if (_cache.TryGetValue(request, out object? value) && value is TResponse response) | ||
var key = GetKey(request); | ||
|
||
if (_memoryCache.TryGetValue(key, out object? value) && value is TResponse response) | ||
{ | ||
return response; | ||
} | ||
|
||
response = await next(); | ||
|
||
// Tool results depend on state external to prompt and should not be cached | ||
if (!request.Prompt.Messages.Any(x => x.Role == Prompts.Roles.Tool)) | ||
{ | ||
_cache.Set(request, response); | ||
} | ||
_memoryCache.Set(key, response, _memoryCacheEntryOptions); | ||
|
||
return response; | ||
} | ||
|
||
private static string GetKey<T>(T key) | ||
{ | ||
var json = JsonSerializer.Serialize(key); | ||
var bytes = SHA256.HashData(Encoding.UTF8.GetBytes(json)); | ||
var hash = Convert.ToHexString(bytes); | ||
|
||
return hash; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters