增加es归档 分布式和constants变量
This commit is contained in:
@@ -95,6 +95,13 @@ func AddToStream(ctx context.Context, streamKey string, msg interface{}) (messag
|
||||
return
|
||||
}
|
||||
|
||||
// CreateConsumerGroup 创建消费者组(如果不存在)
|
||||
// XGROUP CREATE streamKey groupName $ MKSTREAM
|
||||
func CreateConsumerGroup(ctx context.Context, streamKey, groupName string) error {
|
||||
_, err := redisClient.Do(ctx, "XGROUP", "CREATE", streamKey, groupName, "$", "MKSTREAM")
|
||||
return err
|
||||
}
|
||||
|
||||
// ReadFromStream 从 Stream 读取消息(消费者组模式)
|
||||
// 使用 gredis Do() 方法执行 XREADGROUP 命令
|
||||
func ReadFromStream(ctx context.Context, streamKey, groupName, consumerName string, count int64, blockMs int64) ([]StreamMessage, error) {
|
||||
@@ -447,3 +454,24 @@ func DelSessionCache(ctx context.Context, userId string) error {
|
||||
_, err := redisClient.Del(ctx, key)
|
||||
return err
|
||||
}
|
||||
|
||||
// TryLock 尝试获取分布式锁(非阻塞)
|
||||
// key: 锁的键名
|
||||
// expireSeconds: 锁的过期时间(秒),防止死锁
|
||||
// 返回 true 表示获取成功,false 表示锁已被其他节点持有
|
||||
func TryLock(ctx context.Context, key string, expireSeconds int) bool {
|
||||
// SET key value NX EX expireSeconds
|
||||
result, err := redisClient.Do(ctx, "SET", key, gtime.Now().String(), "NX", "EX", expireSeconds)
|
||||
if err != nil {
|
||||
glog.Errorf(ctx, "获取分布式锁失败: %v", err)
|
||||
return false
|
||||
}
|
||||
return result.String() == "OK"
|
||||
}
|
||||
|
||||
// Unlock 释放分布式锁
|
||||
func Unlock(ctx context.Context, key string) {
|
||||
if _, err := redisClient.Del(ctx, key); err != nil {
|
||||
glog.Errorf(ctx, "释放分布式锁失败: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user