增加 图片识别 缓存,缓存日期有等级
This commit is contained in:
1 parent
150e8644b7
commit
9bb17a67d2
5 files changed
+176
-19
No files matched your search
@@ -1,4 +1,5 @@
|
||||
using FreeSql.DataAnnotations;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace Store.Modules.CloudProduct.Entities;
|
||||
|
||||
@@ -14,4 +15,31 @@ public sealed class ImageAnalysisCache
|
||||
public string RecognitionResult { get; set; } = "";
|
||||
/// <summary>缓存最近更新时间,UTC。</summary>
|
||||
public DateTime UpdatedAt { get; set; }
|
||||
|
||||
/// <summary>从 UpdatedAt 起算的缓存有效小时数,命中时不续期。</summary>
|
||||
public int ExpiresAt { get; set; }
|
||||
|
||||
/// <summary>每轮成功识别后更新有效期:空结果逐级延长,有内容则重置级别。</summary>
|
||||
public void RecordExpiration(bool isEmpty, DateTime utcNow)
|
||||
{
|
||||
// 新记录和上次有内容的记录,首次为空时均从 24 小时起算。
|
||||
ExpiresAt = isEmpty ? ExpiresAt switch
|
||||
{
|
||||
24 => 48,
|
||||
48 => 96,
|
||||
96 => 192,
|
||||
192 or 384 => 384,
|
||||
_ => 24
|
||||
} : 720;
|
||||
UpdatedAt = utcNow;
|
||||
}
|
||||
|
||||
/// <summary>仅复用有效期内的数组,旧对象格式需要重新识别。</summary>
|
||||
public string[]? ReadFreshResult(DateTime utcNow)
|
||||
{
|
||||
if (utcNow >= UpdatedAt.AddHours(ExpiresAt) || !RecognitionResult.TrimStart().StartsWith('['))
|
||||
return null;
|
||||
|
||||
return JsonSerializer.Deserialize<string[]>(RecognitionResult);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user