From d04d8f94dcfc8f32ea6b7c38cd8fb31834ced1c0 Mon Sep 17 00:00:00 2001 From: Antonio Date: Tue, 13 Jun 2023 00:47:24 +0800 Subject: [PATCH] efficiently update bard convo hash --- internal/bard/request.go | 8 -------- internal/bard/utilities.go | 16 ++++++++++++++++ 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/internal/bard/request.go b/internal/bard/request.go index 84601f6..aacf71d 100644 --- a/internal/bard/request.go +++ b/internal/bard/request.go @@ -6,14 +6,6 @@ type BardCache struct { Bards map[string]*Bard } -func GarbageCollectCache(cache *BardCache) { - for k, v := range cache.Bards { - if time.Since(v.LastInteractionTime) > time.Minute*5 { - delete(cache.Bards, k) - } - } -} - var cache *BardCache func init() { diff --git a/internal/bard/utilities.go b/internal/bard/utilities.go index 669926a..3aad9b6 100644 --- a/internal/bard/utilities.go +++ b/internal/bard/utilities.go @@ -3,6 +3,7 @@ package bard import ( "crypto/md5" "encoding/hex" + "time" ) func HashConversation(conversation []string) string { @@ -12,3 +13,18 @@ func HashConversation(conversation []string) string { } return hex.EncodeToString(hash.Sum(nil)) } + +func GarbageCollectCache(cache *BardCache) { + for k, v := range cache.Bards { + if time.Since(v.LastInteractionTime) > time.Minute*5 { + delete(cache.Bards, k) + } + } +} + +func UpdateBardHash(old_hash, hash string) { + if _, ok := cache.Bards[old_hash]; ok { + cache.Bards[hash] = cache.Bards[old_hash] + delete(cache.Bards, old_hash) + } +}