service 修改 Redis 存储 KV
This commit is contained in:
@@ -1,54 +1,59 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"strings"
|
||||
predis "ai-chat-service/pkg/db/redis"
|
||||
"context"
|
||||
"encoding/json"
|
||||
|
||||
redis "github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
type IChatRecordsData interface {
|
||||
Add(record *ChatRecord) error
|
||||
GetById(id int64) (record *ChatRecord, err error)
|
||||
GetById(id string) (record *ChatRecord, err error)
|
||||
}
|
||||
|
||||
type ChatRecord struct {
|
||||
ID int64 `json:"id"`
|
||||
UserMsg string `json:"user_msg"`
|
||||
UserMsgTokens int `json:"user_msg_tokens"`
|
||||
UserMsgKeywords []string `json:"user_msg_keywords"`
|
||||
AIMsg string `json:"ai_msg"`
|
||||
AIMsgTokens int `json:"ai_msg_tokens"`
|
||||
ReqTokens int `json:"req_tokens"`
|
||||
CreateAt int64 `json:"create_at"`
|
||||
ID string `json:"-"`
|
||||
Question string `json:"q"`
|
||||
Answer string `json:"a"`
|
||||
}
|
||||
|
||||
type chatRecordsData struct {
|
||||
db *sql.DB
|
||||
type chatRecordsData struct{}
|
||||
|
||||
func NewChatRecordsData() IChatRecordsData {
|
||||
return &chatRecordsData{}
|
||||
}
|
||||
|
||||
func NewChatRecordsData(db *sql.DB) IChatRecordsData {
|
||||
return &chatRecordsData{
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
func (data *chatRecordsData) Add(record *ChatRecord) error {
|
||||
client := predis.GetPool().Get()
|
||||
defer predis.GetPool().Put(client)
|
||||
|
||||
func (data *chatRecordsData) Add(cr *ChatRecord) (err error) {
|
||||
sqlStr := "insert into chat_records(user_msg,user_msg_tokens,user_msg_keywords,ai_msg,ai_msg_tokens,req_tokens,create_at)values(?,?,?,?,?,?,?)"
|
||||
res, err := data.db.Exec(sqlStr, cr.UserMsg, cr.UserMsgTokens, strings.Join(cr.UserMsgKeywords, ","), cr.AIMsg, cr.AIMsgTokens, cr.ReqTokens, cr.CreateAt)
|
||||
payload, err := json.Marshal(&ChatRecord{
|
||||
Question: record.Question,
|
||||
Answer: record.Answer,
|
||||
})
|
||||
if err != nil {
|
||||
return
|
||||
return err
|
||||
}
|
||||
cr.ID, _ = res.LastInsertId()
|
||||
return
|
||||
return client.Set(context.Background(), predis.GetKey("qa", record.ID), string(payload), 0).Err()
|
||||
}
|
||||
func (data *chatRecordsData) GetById(id int64) (cr *ChatRecord, err error) {
|
||||
sqlStr := "select id,user_msg,user_msg_tokens,user_msg_keywords,ai_msg,ai_msg_tokens,req_tokens,create_at from chat_records where id = ?"
|
||||
row := data.db.QueryRow(sqlStr, id)
|
||||
cr = &ChatRecord{}
|
||||
var keywords string
|
||||
err = row.Scan(&cr.ID, &cr.UserMsg, &cr.UserMsgTokens, &keywords, &cr.AIMsg, &cr.AIMsgTokens, &cr.ReqTokens, &cr.CreateAt)
|
||||
|
||||
func (data *chatRecordsData) GetById(id string) (*ChatRecord, error) {
|
||||
client := predis.GetPool().Get()
|
||||
defer predis.GetPool().Put(client)
|
||||
|
||||
value, err := client.Get(context.Background(), predis.GetKey("qa", id)).Result()
|
||||
if err == redis.Nil {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cr.UserMsgKeywords = strings.Split(keywords, ",")
|
||||
return cr, err
|
||||
|
||||
record := &ChatRecord{ID: id}
|
||||
if err = json.Unmarshal([]byte(value), record); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return record, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user