1
0

Garbage collection Bard

This commit is contained in:
Antonio 2023-06-13 00:17:56 +08:00
parent d4e327aa32
commit 1425521d26
2 changed files with 35 additions and 5 deletions

View File

@ -61,11 +61,12 @@ type Answer struct {
// Bard is the main struct for the Bard AI // Bard is the main struct for the Bard AI
type Bard struct { type Bard struct {
Cookie string Cookie string
ChoiceID string ChoiceID string
ConversationID string ConversationID string
ResponseID string ResponseID string
SNlM0e string SNlM0e string
LastInteractionTime time.Time
} }
// New creates a new Bard AI instance. Cookie is the __Secure-1PSID cookie from Google // New creates a new Bard AI instance. Cookie is the __Secure-1PSID cookie from Google
@ -104,6 +105,7 @@ func (b *Bard) getSNlM0e() error {
// Ask generates a Bard AI response and returns it to the user // Ask generates a Bard AI response and returns it to the user
func (b *Bard) Ask(prompt string) (*Answer, error) { func (b *Bard) Ask(prompt string) (*Answer, error) {
b.LastInteractionTime = time.Now()
// req paramters for the actual request // req paramters for the actual request
reqParams := map[string]string{ reqParams := map[string]string{

View File

@ -1 +1,29 @@
package bard package bard
import "time"
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() {
cache = &BardCache{
Bards: make(map[string]Bard),
}
go func() {
for {
GarbageCollectCache(cache)
time.Sleep(time.Minute)
}
}()
}