redis 缓存方式修改
This commit is contained in:
@@ -382,21 +382,13 @@ func IsUserActive(ctx context.Context, userId string, seconds int64) (bool, erro
|
|||||||
return false, nil // 未找到记录,视为不活跃
|
return false, nil // 未找到记录,视为不活跃
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 检查时间差
|
||||||
now := gtime.Now().Timestamp()
|
now := gtime.Now().Timestamp()
|
||||||
return (now - lastActive) < seconds, nil
|
return (now - lastActive) < seconds, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetSessionCache 缓存用户的 RAGFlow Session ID
|
// ============== 限流相关 ==============
|
||||||
// 使用 gredis SetEX 方法
|
|
||||||
func SetSessionCache(ctx context.Context, userId, sessionId string) error {
|
|
||||||
key := SessionLastActiveKeyPrefix + userId + ":session_id"
|
|
||||||
|
|
||||||
// SETEX key 604800 value (604800秒 = 7天)
|
|
||||||
_, err := redisClient.Do(ctx, "SETEX", key, 604800, sessionId)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// 限流相关常量
|
|
||||||
const (
|
const (
|
||||||
// RateLimitKeyPrefix 限流计数器 Key 前缀
|
// RateLimitKeyPrefix 限流计数器 Key 前缀
|
||||||
RateLimitKeyPrefix = "ragflow:ratelimit:"
|
RateLimitKeyPrefix = "ragflow:ratelimit:"
|
||||||
@@ -433,9 +425,17 @@ func GetRateLimit(ctx context.Context, key string) (count int64, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetSessionCache 获取缓存的 RAGFlow Session ID
|
// SetSessionCache 缓存 RAGFlow Session ID(租户+用户+方向隔离)
|
||||||
func GetSessionCache(ctx context.Context, userId string) (string, error) {
|
func SetSessionCache(ctx context.Context, tenantId, userId, chatId, sessionId string) error {
|
||||||
key := SessionLastActiveKeyPrefix + userId + ":session_id"
|
key := SessionLastActiveKeyPrefix + tenantId + ":" + userId + ":" + chatId + ":session_id"
|
||||||
|
// SETEX key 7200 value (7200秒 = 2小时,与last_active保持一致)
|
||||||
|
_, err := redisClient.Do(ctx, "SETEX", key, 7200, sessionId)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetSessionCache 获取缓存的 RAGFlow Session ID(租户+用户+方向隔离)
|
||||||
|
func GetSessionCache(ctx context.Context, tenantId, userId, chatId string) (string, error) {
|
||||||
|
key := SessionLastActiveKeyPrefix + tenantId + ":" + userId + ":" + chatId + ":session_id"
|
||||||
result, err := redisClient.Get(ctx, key)
|
result, err := redisClient.Get(ctx, key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
@@ -448,9 +448,9 @@ func GetSessionCache(ctx context.Context, userId string) (string, error) {
|
|||||||
return result.String(), nil
|
return result.String(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// DelSessionCache 删除缓存的 RAGFlow Session ID(归档时调用)
|
// DelSessionCache 删除缓存的 RAGFlow Session ID(归档时调用,租户+用户+方向隔离)
|
||||||
func DelSessionCache(ctx context.Context, userId string) error {
|
func DelSessionCache(ctx context.Context, tenantId, userId, chatId string) error {
|
||||||
key := SessionLastActiveKeyPrefix + userId + ":session_id"
|
key := SessionLastActiveKeyPrefix + tenantId + ":" + userId + ":" + chatId + ":session_id"
|
||||||
_, err := redisClient.Del(ctx, key)
|
_, err := redisClient.Del(ctx, key)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user