第五次对话发卡片, 用redis记录卡片次数
This commit is contained in:
@@ -475,3 +475,49 @@ func Unlock(ctx context.Context, key string) {
|
||||
glog.Errorf(ctx, "释放分布式锁失败: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// ============== 对话计数相关(用于卡片触发)==============
|
||||
|
||||
const (
|
||||
// ConversationCountKeyPrefix 对话计数 Key 前缀
|
||||
ConversationCountKeyPrefix = "ragflow:conversation:count:"
|
||||
)
|
||||
|
||||
// IncrConversationCount 增加用户对话计数,返回当前轮数
|
||||
// 用于判断是否触发发送卡片(如对话5轮后发送)
|
||||
// expireSeconds: 过期时间(秒),建议与会话超时一致(如7200秒=2小时)
|
||||
func IncrConversationCount(ctx context.Context, userId, platform string, expireSeconds int64) (count int64, err error) {
|
||||
key := ConversationCountKeyPrefix + userId + "_" + platform
|
||||
result, err := redisClient.Do(ctx, "INCR", key)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
count = result.Int64()
|
||||
|
||||
// 首次设置过期时间
|
||||
if count == 1 {
|
||||
redisClient.Do(ctx, "EXPIRE", key, expireSeconds)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// GetConversationCount 获取用户当前对话轮数
|
||||
func GetConversationCount(ctx context.Context, userId, platform string) (count int64, err error) {
|
||||
key := ConversationCountKeyPrefix + userId + "_" + platform
|
||||
result, err := redisClient.Get(ctx, key)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if result.IsEmpty() {
|
||||
return 0, nil
|
||||
}
|
||||
count = result.Int64()
|
||||
return
|
||||
}
|
||||
|
||||
// ResetConversationCount 重置用户对话计数(归档/卡片发送后调用)
|
||||
func ResetConversationCount(ctx context.Context, userId, platform string) error {
|
||||
key := ConversationCountKeyPrefix + userId + "_" + platform
|
||||
_, err := redisClient.Del(ctx, key)
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user