服务器准备

This commit is contained in:
Cold
2025-12-18 18:01:21 +08:00
committed by 张斌
parent f130c230e8
commit 53c3392227
7 changed files with 682 additions and 5 deletions

View File

@@ -485,10 +485,11 @@ const (
UserStateExpireSeconds = 300
)
// UserState 用户会话状态(阶段+对话计数统一5分钟过期
// UserState 用户会话状态(阶段+对话计数+咨询方向,统一5分钟过期
type UserState struct {
Stage int `json:"stage"` // 用户阶段0=AI模型 1=打招呼 2=业务 3=发卡片
Count int64 `json:"count"` // 对话计数
Stage int `json:"stage"` // 用户阶段:5=未选择方向 0=AI模型 1=打招呼 2=业务 3=发卡片
Count int64 `json:"count"` // 对话计数
Direction string `json:"direction"` // 用户选择的咨询方向(如:产品咨询、售后服务)
}
// GetUserState 获取用户状态(阶段+计数)
@@ -499,14 +500,15 @@ func GetUserState(ctx context.Context, userId, platform string) (state *UserStat
return
}
state = &UserState{}
state = &UserState{Stage: 5} // 默认状态5未选择方向
if result.IsEmpty() {
return // 返回默认值 stage=0, count=0
return
}
m := result.Map()
state.Stage = gconv.Int(m["stage"])
state.Count = gconv.Int64(m["count"])
state.Direction = gconv.String(m["direction"])
return
}
@@ -521,6 +523,17 @@ func SetUserStage(ctx context.Context, userId, platform string, stage int) error
return err
}
// SetUserDirection 设置用户选择的咨询方向,并刷新过期时间
func SetUserDirection(ctx context.Context, userId, platform, direction string) error {
key := UserStateKeyPrefix + userId + "_" + platform
_, err := redisClient.Do(ctx, "HSET", key, "direction", direction)
if err != nil {
return err
}
_, err = redisClient.Do(ctx, "EXPIRE", key, UserStateExpireSeconds)
return err
}
// IncrUserCount 增加用户对话计数,返回当前轮数,并刷新过期时间
func IncrUserCount(ctx context.Context, userId, platform string) (count int64, err error) {
key := UserStateKeyPrefix + userId + "_" + platform
@@ -540,6 +553,59 @@ func ResetUserState(ctx context.Context, userId, platform string) error {
return err
}
// ============== 对话缓存相关5句落库==============
const (
// ConversationCacheKeyPrefix 对话缓存 Key 前缀
ConversationCacheKeyPrefix = "ragflow:conversation:cache:"
// ConversationCacheExpireSeconds 对话缓存过期时间10分钟
ConversationCacheExpireSeconds = 600
)
// CacheConversation 缓存单条对话到Redis List
func CacheConversation(ctx context.Context, userId, platform string, data []byte) error {
key := ConversationCacheKeyPrefix + userId + "_" + platform
_, err := redisClient.Do(ctx, "RPUSH", key, string(data))
if err != nil {
return err
}
_, err = redisClient.Do(ctx, "EXPIRE", key, ConversationCacheExpireSeconds)
return err
}
// GetCachedConversations 获取缓存的对话列表并清空
func GetCachedConversations(ctx context.Context, userId, platform string) (list []string, err error) {
key := ConversationCacheKeyPrefix + userId + "_" + platform
result, err := redisClient.Do(ctx, "LRANGE", key, 0, -1)
if err != nil {
return
}
if result.IsEmpty() {
return
}
list = result.Strings()
// 清空缓存
redisClient.Del(ctx, key)
return
}
// GetCachedConversationCount 获取缓存的对话数量
func GetCachedConversationCount(ctx context.Context, userId, platform string) (count int64, err error) {
key := ConversationCacheKeyPrefix + userId + "_" + platform
result, err := redisClient.Do(ctx, "LLEN", key)
if err != nil {
return
}
return result.Int64(), nil
}
// ClearCachedConversations 清空对话缓存(归档时调用)
func ClearCachedConversations(ctx context.Context, userId, platform string) error {
key := ConversationCacheKeyPrefix + userId + "_" + platform
_, err := redisClient.Del(ctx, key)
return err
}
// ========== 以下为兼容旧接口(内部调用新实现)==========
// IncrConversationCount 增加用户对话计数(兼容旧接口)