Repository navigation
Expand file tree
/
Copy pathstore.go
More file actions
119 lines (108 loc) · 4.71 KB
/
Copy pathstore.go
File metadata and controls
119 lines (108 loc) · 4.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package conduit
import (
"context"
"strings"
"time"
)
// StateStore 全局状态存储接口。
// 业务 Pass 通过此接口读写持久化数据(如用户等级、游戏进度等),
// 无需关心底层是内存、Redis 还是数据库。
//
// 所有方法接受 context.Context 参数,以便支持请求级超时和取消。
// Value 统一为 string 类型,具体序列化/反序列化由 Pass 层负责。
//
// # 原子操作
//
// StateStore 提供三种原子操作方法,用于并发场景下保证数据一致性:
//
// - CompareAndSwap: 比较并交换,适用于乐观锁、状态机转换等场景
// - IncrBy: 原子增量,适用于计数器(积分、调用次数等)
// - SetIfNotExists: 仅当键不存在时设置,适用于分布式锁、幂等性保障等场景
//
// 这三种操作在底层存储中必须是原子的。对于 Redis 等后端,直接映射到
// 对应的原子命令(如 INCRBY、SETNX);对于内存实现,通过互斥锁保证原子性。
type StateStore interface {
// Get 获取指定键的值。
// 如果键不存在,应返回空字符串和 nil error(不返回 error)。
Get(ctx context.Context, key string) (string, error)
// Set 设置键值对,ttl 为过期时间。
// ttl == 0 表示永不过期。
Set(ctx context.Context, key string, value string, ttl time.Duration) error
// Delete 删除指定键。
// 如果键不存在,不应返回错误。
Delete(ctx context.Context, key string) error
// Exists 检查指定键是否存在。
Exists(ctx context.Context, key string) (bool, error)
// CompareAndSwap 原子地比较并交换指定键的值(CAS 操作)。
// 仅当键的当前值等于 oldValue 时,将值更新为 newValue,返回 true。
// 如果键不存在或已过期,且 oldValue 为空字符串,视为匹配成功(可借此实现"不存在则创建")。
// 如果键的当前值不等于 oldValue,不做任何修改,返回 false。
//
// 注意:由于 Get 对"键不存在"和"值为空字符串"均返回空字符串,
// 当 oldValue 为空字符串时,"键不存在"和"键存在且值为空"都会匹配成功。
// 如果需要仅当键不存在时创建,请使用 SetIfNotExists。
//
// 典型用法:
//
// // 乐观锁:仅当状态为 "idle" 时切换为 "running"
// swapped, err := store.CompareAndSwap(ctx, "task:status", "idle", "running", 0)
// if !swapped {
// // 状态已被其他协程修改,重试或放弃
// }
//
// // 键不存在时创建(oldValue 为空)
// swapped, err := store.CompareAndSwap(ctx, "lock:resource", "", "locked", 10*time.Second)
CompareAndSwap(ctx context.Context, key string, oldValue, newValue string, ttl time.Duration) (bool, error)
// IncrBy 原子地将指定键的值增加 delta,返回增加后的值。
// 如果键不存在,视为当前值为 0 后再执行增加。
// 如果键的当前值不是合法的十进制整数,返回错误。
//
// delta 为负数时实现递减功能。
//
// 典型用法:
//
// // 用户积分 +10
// newScore, err := store.IncrBy(ctx, "user:score:u123", 10)
//
// // 剩余次数 -1
// remaining, err := store.IncrBy(ctx, "rate:limit:u123", -1)
IncrBy(ctx context.Context, key string, delta int64) (int64, error)
// SetIfNotExists 仅当键不存在时设置值(SETNX 语义)。
// 如果键已存在且未过期,不做任何修改,返回 false。
// 如果键不存在或已过期,设置成功,返回 true。
//
// 典型用法:
//
// // 分布式锁:获取锁
// acquired, err := store.SetIfNotExists(ctx, "lock:resource1", ownerID, 30*time.Second)
// if acquired {
// // 获得锁,执行临界区逻辑
// defer store.Delete(ctx, "lock:resource1")
// }
//
// // 幂等性:防止重复处理
// isNew, err := store.SetIfNotExists(ctx, "msg:processed:msg123", "1", 24*time.Hour)
// if !isNew {
// // 消息已处理,跳过
// }
SetIfNotExists(ctx context.Context, key string, value string, ttl time.Duration) (bool, error)
// Close 关闭存储连接(如 Redis 连接池释放)。
// 引擎停止时会调用此方法。
Close() error
}
// ============================================================
// 键命名空间辅助函数
// ============================================================
// MakeStoreKey 使用命名空间拼接状态存储的键,避免跨业务键冲突。
// 建议所有 Pass 在操作 StateStore 时使用此函数生成键。
//
// 格式:namespace:identifier(如 "chat:history:user123")
//
// 示例:
//
// key := conduit.MakeStoreKey("chat", "history", ctx.UserID)
// // key == "chat:history:user123"
// store.Set(ctx.Ctx, key, data, 0)
func MakeStoreKey(parts ...string) string {
return strings.Join(parts, ":")
}