memory leak
This commit is contained in:
35
database/red/ai_chats.go
Normal file
35
database/red/ai_chats.go
Normal 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()
|
||||
}
|
||||
Reference in New Issue
Block a user