Garbage collection Bard
This commit is contained in:
parent
d4e327aa32
commit
1425521d26
@ -61,11 +61,12 @@ type Answer struct {
|
||||
|
||||
// Bard is the main struct for the Bard AI
|
||||
type Bard struct {
|
||||
Cookie string
|
||||
ChoiceID string
|
||||
ConversationID string
|
||||
ResponseID string
|
||||
SNlM0e string
|
||||
Cookie string
|
||||
ChoiceID string
|
||||
ConversationID string
|
||||
ResponseID string
|
||||
SNlM0e string
|
||||
LastInteractionTime time.Time
|
||||
}
|
||||
|
||||
// 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
|
||||
func (b *Bard) Ask(prompt string) (*Answer, error) {
|
||||
b.LastInteractionTime = time.Now()
|
||||
|
||||
// req paramters for the actual request
|
||||
reqParams := map[string]string{
|
||||
|
@ -1 +1,29 @@
|
||||
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)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
Reference in New Issue
Block a user