第五次对话发卡片, 用redis记录卡片次数

This commit is contained in:
Cold
2025-12-16 11:52:46 +08:00
committed by 张斌
parent c4d232d6ff
commit 03aef62184
3 changed files with 313 additions and 104 deletions

View File

@@ -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
}