memory leak

This commit is contained in:
2026-02-05 15:14:47 +03:00
parent b894cbd9c3
commit 0fded455b8
22 changed files with 420 additions and 170 deletions

35
database/red/ai_chats.go Normal file
View File

@@ -0,0 +1,35 @@
package red
import (
"fmt"
"git.nix13.pw/scuroneko/laniakea"
"github.com/google/uuid"
"github.com/redis/go-redis/v9"
)
type AiRepository struct {
client *redis.Client
}
func NewAiRepository(db *laniakea.DatabaseContext) *AiRepository {
return &AiRepository{client: db.Redis}
}
func (rep *AiRepository) SetChatId(userId int, chatId string) error {
key := fmt.Sprintf("ai.chats.gpt.%d", userId)
return rep.client.Set(ctx, key, chatId, 0).Err()
}
func (rep *AiRepository) GetChatId(userId int) (string, error) {
key := fmt.Sprintf("ai.chats.gpt.%d", userId)
return rep.client.Get(ctx, key).Result()
}
func (rep *AiRepository) GetOrCreateChatId(userId int) (string, error) {
key := fmt.Sprintf("ai.chats.gpt.%d", userId)
res := rep.client.Get(ctx, key)
if res.Err() != nil {
chatId := uuid.New().String()
return chatId, rep.SetChatId(userId, chatId)
}
return res.Result()
}