perf: use stable sha256 cache keys for summarizer requests
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Net.Http;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Threading;
|
||||
@@ -63,11 +64,20 @@ namespace JobTrackerApi.Services
|
||||
_cache = cache;
|
||||
}
|
||||
|
||||
private static string BuildCacheKey(string text, int maxLength, int minLength)
|
||||
{
|
||||
var payload = $"{text}\n::{maxLength}:{minLength}";
|
||||
var hash = Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(payload))).ToLowerInvariant();
|
||||
return $"summ:{hash}";
|
||||
}
|
||||
|
||||
public async Task<string?> SummarizeAsync(string text, int maxLength = 150, int minLength = 30)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(text)) return null;
|
||||
|
||||
var key = $"summ:{text.GetHashCode()}:{maxLength}:{minLength}";
|
||||
// Use a deterministic content hash instead of string.GetHashCode() so cache keys
|
||||
// are collision-resistant and stable across process restarts.
|
||||
var key = BuildCacheKey(text, maxLength, minLength);
|
||||
Interlocked.Increment(ref _requests);
|
||||
|
||||
if (_cache.TryGetValue<string>(key, out var cached))
|
||||
|
||||
Reference in New Issue
Block a user