@gordon.gan/specflow 1.4.0-beta → 1.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/package.json +1 -1
- package/prompts/approval/database-guidance.md +79 -0
- package/prompts/approval/generate.md +569 -290
- package/skills/database/LICENSE +405 -0
- package/skills/database/ORIGIN.md +6 -0
- package/skills/database/README.md +30 -0
- package/skills/database/elasticsearch/LICENSE.txt +202 -0
- package/skills/database/elasticsearch/SKILL.md +199 -0
- package/skills/database/elasticsearch/examples/01-fulltext-search.md +215 -0
- package/skills/database/elasticsearch/examples/02-aggregation-report.md +206 -0
- package/skills/database/elasticsearch/examples/03-reindex-zero-downtime.md +200 -0
- package/skills/database/elasticsearch/examples/04-cluster-monitoring.md +204 -0
- package/skills/database/elasticsearch/references/01-query-dsl-fulltext.md +162 -0
- package/skills/database/elasticsearch/references/02-query-dsl-term.md +210 -0
- package/skills/database/elasticsearch/references/03-aggregations-metric.md +161 -0
- package/skills/database/elasticsearch/references/04-aggregations-bucket.md +236 -0
- package/skills/database/elasticsearch/references/05-mapping-types.md +134 -0
- package/skills/database/elasticsearch/references/06-analyzers.md +187 -0
- package/skills/database/elasticsearch/references/07-cluster-ops.md +225 -0
- package/skills/database/elasticsearch/references/08-elk-integration.md +170 -0
- package/skills/database/mysql/SKILL.md +195 -0
- package/skills/database/mysql/examples/01-connection-pool.md +75 -0
- package/skills/database/mysql/examples/02-slow-query-optimization.md +98 -0
- package/skills/database/mysql/examples/03-master-slave-setup.md +144 -0
- package/skills/database/mysql/examples/04-backup-strategy.md +212 -0
- package/skills/database/mysql/references/01-functions-string.md +103 -0
- package/skills/database/mysql/references/02-functions-date.md +152 -0
- package/skills/database/mysql/references/03-functions-aggregate-window.md +167 -0
- package/skills/database/mysql/references/04-functions-json.md +129 -0
- package/skills/database/mysql/references/05-sql-ddl-types.md +235 -0
- package/skills/database/mysql/references/06-index-optimization.md +232 -0
- package/skills/database/mysql/references/07-replication-ha.md +213 -0
- package/skills/database/mysql/references/08-backup-restore.md +207 -0
- package/skills/database/mysql/references/09-advanced-features.md +345 -0
- package/skills/database/oracle/LICENSE.txt +202 -0
- package/skills/database/oracle/SKILL.md +238 -0
- package/skills/database/oracle/examples/01-plsql-procedure.md +90 -0
- package/skills/database/oracle/examples/02-awr-analysis.md +99 -0
- package/skills/database/oracle/examples/03-rman-backup.md +108 -0
- package/skills/database/oracle/examples/04-dataguard-setup.md +146 -0
- package/skills/database/oracle/references/01-functions-string.md +91 -0
- package/skills/database/oracle/references/02-functions-date.md +71 -0
- package/skills/database/oracle/references/03-analytic-functions.md +103 -0
- package/skills/database/oracle/references/04-plsql-guide.md +303 -0
- package/skills/database/oracle/references/05-performance-tuning.md +164 -0
- package/skills/database/oracle/references/06-backup-recovery.md +115 -0
- package/skills/database/oracle/references/07-dataguard-rac.md +76 -0
- package/skills/database/oracle/references/08-security.md +170 -0
- package/skills/database/oracle/references/09-sql-syntax.md +152 -0
- package/skills/database/oracle/references/10-features.md +174 -0
- package/skills/database/postgresql/LICENSE.txt +202 -0
- package/skills/database/postgresql/SKILL.md +182 -0
- package/skills/database/postgresql/examples/.gitkeep +0 -0
- package/skills/database/postgresql/examples/01-jsonb-query.md +72 -0
- package/skills/database/postgresql/examples/02-cte-recursive.md +110 -0
- package/skills/database/postgresql/examples/03-performance-tuning.md +114 -0
- package/skills/database/postgresql/examples/04-streaming-replication.md +113 -0
- package/skills/database/postgresql/references/.gitkeep +0 -0
- package/skills/database/postgresql/references/01-functions-string.md +174 -0
- package/skills/database/postgresql/references/02-functions-datetime.md +54 -0
- package/skills/database/postgresql/references/03-functions-aggregate-window.md +142 -0
- package/skills/database/postgresql/references/04-functions-jsonb.md +117 -0
- package/skills/database/postgresql/references/05-fulltext-search.md +109 -0
- package/skills/database/postgresql/references/06-index-types.md +95 -0
- package/skills/database/postgresql/references/07-partition-fdw.md +133 -0
- package/skills/database/postgresql/references/08-replication-backup.md +215 -0
- package/skills/database/redis/LICENSE.txt +202 -0
- package/skills/database/redis/SKILL.md +922 -0
- package/skills/database/redis/examples/01-cache-usage.md +104 -0
- package/skills/database/redis/examples/02-session-storage.md +72 -0
- package/skills/database/redis/examples/03-leaderboard.md +63 -0
- package/skills/database/redis/examples/04-redis-cluster-setup.md +70 -0
- package/skills/database/redis/examples/05-stream-queue.md +65 -0
- package/skills/database/redis/references/command-quick-ref.md +180 -0
- package/skills/database/redis/references/commands-admin-key.md +413 -0
- package/skills/database/redis/references/commands-set-sorted-advanced.md +539 -0
- package/skills/database/redis/references/commands-string-hash-list.md +458 -0
- package/skills/database/redis/references/memory-optimization.md +150 -0
- package/skills/database/redis/references/redis-conf-production.md +139 -0
- package/skills/specflow-approval/SKILL.md +100 -181
- package/templates/approval.md +295 -221
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
# Redis 缓存使用示例
|
|
2
|
+
|
|
3
|
+
## 1. Cache-Aside 模式 (Java + Spring Boot)
|
|
4
|
+
|
|
5
|
+
```java
|
|
6
|
+
@Service
|
|
7
|
+
public class UserService {
|
|
8
|
+
@Autowired
|
|
9
|
+
private RedisTemplate<String, Object> redisTemplate;
|
|
10
|
+
@Autowired
|
|
11
|
+
private UserMapper userMapper;
|
|
12
|
+
|
|
13
|
+
public User getUserById(Long id) {
|
|
14
|
+
String key = "user:" + id;
|
|
15
|
+
// 1. 尝试从缓存获取
|
|
16
|
+
User user = (User) redisTemplate.opsForValue().get(key);
|
|
17
|
+
if (user != null) {
|
|
18
|
+
return user;
|
|
19
|
+
}
|
|
20
|
+
// 2. 缓存未命中,查数据库
|
|
21
|
+
user = userMapper.selectById(id);
|
|
22
|
+
if (user != null) {
|
|
23
|
+
// 3. 写入缓存,设置 TTL
|
|
24
|
+
redisTemplate.opsForValue().set(key, user, 1, TimeUnit.HOURS);
|
|
25
|
+
}
|
|
26
|
+
return user;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
public void updateUser(User user) {
|
|
30
|
+
// 1. 更新数据库
|
|
31
|
+
userMapper.updateById(user);
|
|
32
|
+
// 2. 删除缓存 (下次读取时重建)
|
|
33
|
+
redisTemplate.delete("user:" + user.getId());
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## 2. 分布式锁 (Python + redis-py)
|
|
39
|
+
|
|
40
|
+
```python
|
|
41
|
+
import redis
|
|
42
|
+
import uuid
|
|
43
|
+
import time
|
|
44
|
+
|
|
45
|
+
r = redis.Redis(host='localhost', port=6379, decode_responses=True)
|
|
46
|
+
|
|
47
|
+
def acquire_lock(lock_name, acquire_timeout=10, lock_timeout=30):
|
|
48
|
+
"""获取分布式锁"""
|
|
49
|
+
identifier = str(uuid.uuid4())
|
|
50
|
+
lock_key = f"lock:{lock_name}"
|
|
51
|
+
end = time.time() + acquire_timeout
|
|
52
|
+
|
|
53
|
+
while time.time() < end:
|
|
54
|
+
if r.set(lock_key, identifier, nx=True, ex=lock_timeout):
|
|
55
|
+
return identifier # 成功获取锁
|
|
56
|
+
time.sleep(0.01) # 短暂休眠后重试
|
|
57
|
+
return None # 超时未获取
|
|
58
|
+
|
|
59
|
+
def release_lock(lock_name, identifier):
|
|
60
|
+
"""释放分布式锁 (Lua 脚本保证原子性)"""
|
|
61
|
+
lock_key = f"lock:{lock_name}"
|
|
62
|
+
lua_script = """
|
|
63
|
+
if redis.call("get",KEYS[1]) == ARGV[1] then
|
|
64
|
+
return redis.call("del",KEYS[1])
|
|
65
|
+
else
|
|
66
|
+
return 0
|
|
67
|
+
end
|
|
68
|
+
"""
|
|
69
|
+
return r.eval(lua_script, 1, lock_key, identifier)
|
|
70
|
+
|
|
71
|
+
# 使用示例
|
|
72
|
+
lock_id = acquire_lock("order:1001")
|
|
73
|
+
if lock_id:
|
|
74
|
+
try:
|
|
75
|
+
print("处理订单 1001...")
|
|
76
|
+
# 执行业务逻辑
|
|
77
|
+
finally:
|
|
78
|
+
release_lock("order:1001", lock_id)
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## 3. 限流器 (Node.js)
|
|
82
|
+
|
|
83
|
+
```javascript
|
|
84
|
+
const Redis = require('ioredis');
|
|
85
|
+
const redis = new Redis();
|
|
86
|
+
|
|
87
|
+
async function rateLimit(ip, limit = 100, window = 60) {
|
|
88
|
+
const key = `ratelimit:${ip}`;
|
|
89
|
+
const current = await redis.incr(key);
|
|
90
|
+
if (current === 1) {
|
|
91
|
+
await redis.expire(key, window);
|
|
92
|
+
}
|
|
93
|
+
return current <= limit;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// 使用: 每个 IP 每分钟最多 100 次请求
|
|
97
|
+
app.use(async (req, res, next) => {
|
|
98
|
+
const allowed = await rateLimit(req.ip);
|
|
99
|
+
if (!allowed) {
|
|
100
|
+
return res.status(429).json({ error: '请求过于频繁' });
|
|
101
|
+
}
|
|
102
|
+
next();
|
|
103
|
+
});
|
|
104
|
+
```
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# Redis Session 存储示例
|
|
2
|
+
|
|
3
|
+
## 1. Spring Session + Redis
|
|
4
|
+
|
|
5
|
+
```xml
|
|
6
|
+
<!-- pom.xml 依赖 -->
|
|
7
|
+
<dependency>
|
|
8
|
+
<groupId>org.springframework.boot</groupId>
|
|
9
|
+
<artifactId>spring-boot-starter-data-redis</artifactId>
|
|
10
|
+
</dependency>
|
|
11
|
+
<dependency>
|
|
12
|
+
<groupId>org.springframework.session</groupId>
|
|
13
|
+
<artifactId>spring-session-data-redis</artifactId>
|
|
14
|
+
</dependency>
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
```yaml
|
|
18
|
+
# application.yml
|
|
19
|
+
spring:
|
|
20
|
+
session:
|
|
21
|
+
store-type: redis
|
|
22
|
+
timeout: 1800 # Session 过期时间 (秒)
|
|
23
|
+
redis:
|
|
24
|
+
host: localhost
|
|
25
|
+
port: 6379
|
|
26
|
+
password: your_password
|
|
27
|
+
timeout: 2000ms
|
|
28
|
+
lettuce:
|
|
29
|
+
pool:
|
|
30
|
+
max-active: 50
|
|
31
|
+
max-idle: 20
|
|
32
|
+
min-idle: 5
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
```java
|
|
36
|
+
@Configuration
|
|
37
|
+
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 1800)
|
|
38
|
+
public class RedisSessionConfig {
|
|
39
|
+
// 自动生效,无需额外代码
|
|
40
|
+
}
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## 2. Flask Session (Python)
|
|
44
|
+
|
|
45
|
+
```python
|
|
46
|
+
from flask import Flask, session
|
|
47
|
+
from flask_session import Session
|
|
48
|
+
import redis
|
|
49
|
+
|
|
50
|
+
app = Flask(__name__)
|
|
51
|
+
app.config['SECRET_KEY'] = 'your-secret-key'
|
|
52
|
+
app.config['SESSION_TYPE'] = 'redis'
|
|
53
|
+
app.config['SESSION_REDIS'] = redis.from_url('redis://localhost:6379/0')
|
|
54
|
+
app.config['SESSION_PERMANENT'] = False
|
|
55
|
+
app.config['SESSION_USE_SIGNER'] = True
|
|
56
|
+
app.config['SESSION_KEY_PREFIX'] = 'session:'
|
|
57
|
+
app.config['SESSION_PERMANENT_TIMEOUT'] = 1800
|
|
58
|
+
|
|
59
|
+
Session(app)
|
|
60
|
+
|
|
61
|
+
@app.route('/login')
|
|
62
|
+
def login():
|
|
63
|
+
session['user_id'] = 1001
|
|
64
|
+
session['role'] = 'admin'
|
|
65
|
+
return 'Session stored in Redis'
|
|
66
|
+
|
|
67
|
+
@app.route('/profile')
|
|
68
|
+
def profile():
|
|
69
|
+
user_id = session.get('user_id')
|
|
70
|
+
role = session.get('role')
|
|
71
|
+
return f'User {user_id} with role {role}'
|
|
72
|
+
```
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# Redis ZSet 排行榜示例
|
|
2
|
+
|
|
3
|
+
## 游戏排行榜 (Go)
|
|
4
|
+
|
|
5
|
+
```go
|
|
6
|
+
package redis
|
|
7
|
+
|
|
8
|
+
import (
|
|
9
|
+
"context"
|
|
10
|
+
"github.com/redis/go-redis/v9"
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
type Leaderboard struct {
|
|
14
|
+
rdb *redis.Client
|
|
15
|
+
key string
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
func NewLeaderboard(rdb *redis.Client, gameID string) *Leaderboard {
|
|
19
|
+
return &Leaderboard{rdb: rdb, key: "leaderboard:" + gameID}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// AddScore 增加玩家分数 (原子操作)
|
|
23
|
+
func (lb *Leaderboard) AddScore(ctx context.Context, player string, score float64) error {
|
|
24
|
+
return lb.rdb.ZIncrBy(ctx, lb.key, score, player).Err()
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// GetTopN 获取前 N 名
|
|
28
|
+
func (lb *Leaderboard) GetTopN(ctx context.Context, n int64) ([]redis.Z, error) {
|
|
29
|
+
return lb.rdb.ZRevRangeWithScores(ctx, lb.key, 0, n-1).Result()
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// GetRank 获取玩家排名 (从 0 开始)
|
|
33
|
+
func (lb *Leaderboard) GetRank(ctx context.Context, player string) (int64, error) {
|
|
34
|
+
rank, err := lb.rdb.ZRevRank(ctx, lb.key, player).Result()
|
|
35
|
+
if err != nil {
|
|
36
|
+
return -1, err
|
|
37
|
+
}
|
|
38
|
+
return rank + 1, nil // 转为 1-based
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// GetScore 获取玩家分数
|
|
42
|
+
func (lb *Leaderboard) GetScore(ctx context.Context, player string) (float64, error) {
|
|
43
|
+
return lb.rdb.ZScore(ctx, lb.key, player).Result()
|
|
44
|
+
}
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## 周榜 + 日榜 + 总榜设计
|
|
48
|
+
|
|
49
|
+
```redis
|
|
50
|
+
# 命名规范
|
|
51
|
+
leaderboard:total # 总榜 (长周期)
|
|
52
|
+
leaderboard:2024:W01 # 周榜
|
|
53
|
+
leaderboard:2024:01:15 # 日榜
|
|
54
|
+
|
|
55
|
+
# 每日零点:创建新日榜
|
|
56
|
+
# 每周一零点:创建新周榜
|
|
57
|
+
|
|
58
|
+
# 合并多期分数 (ZUNIONSTORE)
|
|
59
|
+
ZUNIONSTORE leaderboard:month 3
|
|
60
|
+
leaderboard:2024:01:01 leaderboard:2024:01:02 leaderboard:2024:01:03
|
|
61
|
+
WEIGHTS 1 1 1
|
|
62
|
+
AGGREGATE SUM
|
|
63
|
+
```
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# Redis Cluster 搭建与运维示例
|
|
2
|
+
|
|
3
|
+
## 1. 最小生产集群 (3主3从)
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
# 6 个节点目录
|
|
7
|
+
mkdir -p /data/redis/{7000,7001,7002,7003,7004,7005}
|
|
8
|
+
|
|
9
|
+
# 每个节点 redis.conf
|
|
10
|
+
cat > /data/redis/7000/redis.conf << 'EOF'
|
|
11
|
+
port 7000
|
|
12
|
+
cluster-enabled yes
|
|
13
|
+
cluster-config-file nodes-7000.conf
|
|
14
|
+
cluster-node-timeout 5000
|
|
15
|
+
appendonly yes
|
|
16
|
+
appendfsync everysec
|
|
17
|
+
protected-mode no
|
|
18
|
+
bind 0.0.0.0
|
|
19
|
+
daemonize yes
|
|
20
|
+
logfile /var/log/redis/7000.log
|
|
21
|
+
EOF
|
|
22
|
+
|
|
23
|
+
# 启动 6 个节点
|
|
24
|
+
redis-server /data/redis/7000/redis.conf
|
|
25
|
+
redis-server /data/redis/7001/redis.conf
|
|
26
|
+
redis-server /data/redis/7002/redis.conf
|
|
27
|
+
redis-server /data/redis/7003/redis.conf
|
|
28
|
+
redis-server /data/redis/7004/redis.conf
|
|
29
|
+
redis-server /data/redis/7005/redis.conf
|
|
30
|
+
|
|
31
|
+
# 创建集群 (Redis 5+)
|
|
32
|
+
redis-cli --cluster create \
|
|
33
|
+
192.168.1.100:7000 192.168.1.100:7001 192.168.1.100:7002 \
|
|
34
|
+
192.168.1.100:7003 192.168.1.100:7004 192.168.1.100:7005 \
|
|
35
|
+
--cluster-replicas 1
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## 2. 集群运维命令
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
# 检查集群状态
|
|
42
|
+
redis-cli -c -h 192.168.1.100 -p 7000 cluster info
|
|
43
|
+
redis-cli -c -h 192.168.1.100 -p 7000 cluster nodes
|
|
44
|
+
|
|
45
|
+
# 重新平衡 slot
|
|
46
|
+
redis-cli --cluster rebalance 192.168.1.100:7000 \
|
|
47
|
+
--cluster-weight node1=1 node2=2 node3=1 \
|
|
48
|
+
--cluster-use-empty-masters
|
|
49
|
+
|
|
50
|
+
# 修复集群
|
|
51
|
+
redis-cli --cluster fix 192.168.1.100:7000
|
|
52
|
+
|
|
53
|
+
# 动态扩容:添加节点
|
|
54
|
+
redis-cli --cluster add-node 新节点:7006 已有节点:7000 --cluster-slave
|
|
55
|
+
redis-cli --cluster reshard 已有节点:7000 --cluster-from all \
|
|
56
|
+
--cluster-to 新节点id --cluster-slots 1000 --cluster-yes
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## 3. 数据迁移方案
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
# 使用 redis-shake 跨集群迁移
|
|
63
|
+
redis-shake.linux -type sync -conf redis-shake.conf
|
|
64
|
+
|
|
65
|
+
# redis-shake.conf 示例
|
|
66
|
+
source.type = cluster
|
|
67
|
+
source.address = 192.168.1.100:7000
|
|
68
|
+
target.type = cluster
|
|
69
|
+
target.address = 192.168.1.200:7000
|
|
70
|
+
```
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# Redis Stream 消息队列示例
|
|
2
|
+
|
|
3
|
+
## 1. 生产-消费模式
|
|
4
|
+
|
|
5
|
+
```redis
|
|
6
|
+
# 生产者: 发送消息
|
|
7
|
+
XADD orders * order_id 1001 user_id 42 amount 99.99 status pending
|
|
8
|
+
XADD orders * order_id 1002 user_id 55 amount 199.00 status pending
|
|
9
|
+
|
|
10
|
+
# 消费者: 读取消息 (非阻塞)
|
|
11
|
+
XRANGE orders - + COUNT 10
|
|
12
|
+
|
|
13
|
+
# 消费者: 阻塞读取新消息
|
|
14
|
+
XREAD COUNT 1 BLOCK 5000 STREAMS orders $
|
|
15
|
+
|
|
16
|
+
# 消息长度
|
|
17
|
+
XLEN orders
|
|
18
|
+
|
|
19
|
+
# 删除消息
|
|
20
|
+
XDEL orders 1700000000000-0
|
|
21
|
+
|
|
22
|
+
# 修剪 (保留最近的 1000 条)
|
|
23
|
+
XTRIM orders MAXLEN ~ 1000
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## 2. 消费组模式
|
|
27
|
+
|
|
28
|
+
```redis
|
|
29
|
+
# 创建消费组 (从最新消息开始消费)
|
|
30
|
+
XGROUP CREATE orders payment-group $ MKSTREAM
|
|
31
|
+
XGROUP CREATE orders inventory-group $
|
|
32
|
+
XGROUP CREATE orders notification-group $
|
|
33
|
+
|
|
34
|
+
# 消费组成员: 读取未确认消息
|
|
35
|
+
# payment-service
|
|
36
|
+
XREADGROUP GROUP payment-group worker-1 COUNT 1 BLOCK 2000 STREAMS orders >
|
|
37
|
+
# inventory-service
|
|
38
|
+
XREADGROUP GROUP inventory-group worker-1 COUNT 1 BLOCK 2000 STREAMS orders >
|
|
39
|
+
# notification-service
|
|
40
|
+
XREADGROUP GROUP notification-group worker-1 COUNT 1 BLOCK 2000 STREAMS orders >
|
|
41
|
+
|
|
42
|
+
# 确认消费 (ACK)
|
|
43
|
+
XACK orders payment-group 1700000000000-0
|
|
44
|
+
|
|
45
|
+
# 查看待确认消息
|
|
46
|
+
XPENDING orders payment-group
|
|
47
|
+
|
|
48
|
+
# 查看消费组信息
|
|
49
|
+
XINFO GROUPS orders
|
|
50
|
+
XINFO CONSUMERS orders payment-group
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## 3. CAP 对比: Stream vs Kafka
|
|
54
|
+
|
|
55
|
+
| 特性 | Redis Stream | Apache Kafka |
|
|
56
|
+
|------|-------------|-------------|
|
|
57
|
+
| 延迟 | <1ms | <10ms |
|
|
58
|
+
| 消息持久化 | RDB/AOF | 磁盘日志 |
|
|
59
|
+
| 消息回溯 | 支持 | 支持 |
|
|
60
|
+
| 消费组 | 支持 | 支持 |
|
|
61
|
+
| 分区顺序 | 单分区有序 | 单分区有序 |
|
|
62
|
+
| 数据保留 | 内存+磁盘 (可控) | 磁盘 (可配置) |
|
|
63
|
+
| 吞吐量 | 10万+/s | 百万+/s |
|
|
64
|
+
| 运维复杂度 | 低 (Redis 原生) | 高 (需 ZK) |
|
|
65
|
+
| 适用场景 | 微服务异步、任务队列 | 大数据流、日志采集 |
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
# Redis 命令速查
|
|
2
|
+
|
|
3
|
+
## 操作分类速查
|
|
4
|
+
|
|
5
|
+
```
|
|
6
|
+
内存操作: SET / GET / DEL / EXISTS / TYPE / TTL / EXPIRE / PERSIST
|
|
7
|
+
计数器: INCR / DECR / INCRBY / INCRBYFLOAT
|
|
8
|
+
散列: HSET / HGET / HGETALL / HDEL / HEXISTS / HINCRBY
|
|
9
|
+
列表: LPUSH / RPUSH / LPOP / RPOP / LRANGE / LLEN / LTRIM / BLPOP
|
|
10
|
+
集合: SADD / SREM / SMEMBERS / SISMEMBER / SCARD / SINTER / SUNION
|
|
11
|
+
有序集合: ZADD / ZREM / ZRANGE / ZRANK / ZSCORE / ZINCRBY / ZINTERSTORE
|
|
12
|
+
位图: SETBIT / GETBIT / BITCOUNT / BITOP
|
|
13
|
+
地理: GEOADD / GEOPOS / GEODIST / GEORADIUS
|
|
14
|
+
流: XADD / XREAD / XREADGROUP / XACK / XRANGE / XLEN / XTRIM
|
|
15
|
+
超日志: PFADD / PFCOUNT / PFMERGE
|
|
16
|
+
事务: MULTI / EXEC / WATCH / DISCARD
|
|
17
|
+
脚本: EVAL / EVALSHA / SCRIPT LOAD / SCRIPT KILL
|
|
18
|
+
连接: PING / AUTH / SELECT / CLIENT LIST / CLIENT KILL
|
|
19
|
+
服务器: INFO / CONFIG GET/SET / SLOWLOG / MONITOR / DBSIZE / FLUSHALL
|
|
20
|
+
发布订阅: PUBLISH / SUBSCRIBE / PSUBSCRIBE / PUBSUB
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## 字符串命令速查
|
|
24
|
+
|
|
25
|
+
```
|
|
26
|
+
SET key value [EX|PX] [NX|XX] # 设置值,支持过期和条件
|
|
27
|
+
GET key # 返回字符串值或 nil
|
|
28
|
+
MGET key1 key2 # 批量获取
|
|
29
|
+
MSET k1 v1 k2 v2 # 批量设置
|
|
30
|
+
INCR key # 整数 +1 (原子)
|
|
31
|
+
INCRBY key n # 整数 +n
|
|
32
|
+
DECR key # 整数 -1
|
|
33
|
+
DECRBY key n # 整数 -n
|
|
34
|
+
APPEND key value # 追加到末尾
|
|
35
|
+
GETRANGE key start end # 获取子串
|
|
36
|
+
SETRANGE key offset value # 从 offset 覆写
|
|
37
|
+
STRLEN key # 获取长度
|
|
38
|
+
GETSET key new_value # 返回旧值设新值
|
|
39
|
+
SETEX key sec value # 设置+过期
|
|
40
|
+
SETNX key value # 不存在才设置
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## 哈希命令速查
|
|
44
|
+
|
|
45
|
+
```
|
|
46
|
+
HSET key field value # 设置字段
|
|
47
|
+
HSETNX key field value # 不存在才设置
|
|
48
|
+
HGET key field # 获取字段
|
|
49
|
+
HGETALL key # 获取所有 (慎用)
|
|
50
|
+
HMGET key f1 f2 # 批量获取
|
|
51
|
+
HMSET key f1 v1 f2 v2 # 批量设置
|
|
52
|
+
HDEL key field # 删除字段
|
|
53
|
+
HEXISTS key field # 判断存在
|
|
54
|
+
HLEN key # 字段数量
|
|
55
|
+
HKEYS key # 所有字段名
|
|
56
|
+
HVALS key # 所有字段值
|
|
57
|
+
HINCRBY key field n # 字段值 +n
|
|
58
|
+
HINCRBYFLOAT key field n # 浮点增加
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## 列表命令速查
|
|
62
|
+
|
|
63
|
+
```
|
|
64
|
+
LPUSH key v1 v2 # 左推
|
|
65
|
+
RPUSH key v1 v2 # 右推
|
|
66
|
+
LPOP key # 左弹
|
|
67
|
+
RPOP key # 右弹
|
|
68
|
+
LRANGE key start stop # 范围获取
|
|
69
|
+
LINDEX key index # 索引获取
|
|
70
|
+
LLEN key # 长度
|
|
71
|
+
LREM key count value # 移除元素
|
|
72
|
+
LTRIM key start stop # 修剪
|
|
73
|
+
LSET key index value # 设置索引值
|
|
74
|
+
BLPOP key timeout # 阻塞左弹
|
|
75
|
+
BRPOP key timeout # 阻塞右弹
|
|
76
|
+
RPOPLPUSH src dst # 转存
|
|
77
|
+
BRPOPLPUSH src dst timeout # 阻塞转存
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## 集合命令速查
|
|
81
|
+
|
|
82
|
+
```
|
|
83
|
+
SADD key m1 m2 # 添加
|
|
84
|
+
SREM key m1 # 移除
|
|
85
|
+
SMEMBERS key # 所有成员
|
|
86
|
+
SISMEMBER key m # 是否在集合中
|
|
87
|
+
SCARD key # 数量
|
|
88
|
+
SPOP key # 随机弹出
|
|
89
|
+
SRANDMEMBER key count # 随机取样
|
|
90
|
+
SINTER k1 k2 # 交集
|
|
91
|
+
SUNION k1 k2 # 并集
|
|
92
|
+
SDIFF k1 k2 # 差集
|
|
93
|
+
SINTERSTORE dest k1 k2 # 交集存
|
|
94
|
+
SUNIONSTORE dest k1 k2 # 并集存
|
|
95
|
+
SDIFFSTORE dest k1 k2 # 差集存
|
|
96
|
+
SSCAN key cursor # 渐进迭代
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## 有序集合命令速查
|
|
100
|
+
|
|
101
|
+
```
|
|
102
|
+
ZADD key score member # 添加
|
|
103
|
+
ZREM key member # 移除
|
|
104
|
+
ZRANGE key start stop [WITHSCORES] # 按排名取 (小到大)
|
|
105
|
+
ZREVRANGE key start stop [WS] # 按排名取 (大到小)
|
|
106
|
+
ZRANGEBYSCORE key min max # 按分数取
|
|
107
|
+
ZRANK key member # 排名 (小到大)
|
|
108
|
+
ZREVRANK key member # 排名 (大到小)
|
|
109
|
+
ZSCORE key member # 分数
|
|
110
|
+
ZCARD key # 数量
|
|
111
|
+
ZINCRBY key n member # 分数 +n
|
|
112
|
+
ZCOUNT key min max # 分数区间内数量
|
|
113
|
+
ZREM key member # 移除
|
|
114
|
+
ZREMRANGEBYRANK key start stop # 移除排名区间
|
|
115
|
+
ZREMRANGEBYSCORE key min max # 移除分数区间
|
|
116
|
+
ZINTERSTORE dest n keys [WEIGHTS] # 交集
|
|
117
|
+
ZUNIONSTORE dest n keys [WEIGHTS] # 并集
|
|
118
|
+
ZSCAN key cursor # 渐进迭代
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
## 发布订阅命令速查
|
|
122
|
+
|
|
123
|
+
```
|
|
124
|
+
SUBSCRIBE channel # 订阅
|
|
125
|
+
UNSUBSCRIBE channel # 退订
|
|
126
|
+
PUBLISH channel message # 发布
|
|
127
|
+
PSUBSCRIBE pattern # 模式订阅
|
|
128
|
+
PUNSUBSCRIBE pattern # 模式退订
|
|
129
|
+
PUBSUB channels # 查看活跃频道
|
|
130
|
+
PUBSUB numsub channel # 查看频道订阅数
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
## 脚本命令速查
|
|
134
|
+
|
|
135
|
+
```
|
|
136
|
+
EVAL script numkeys key [key] arg [arg] # 执行 Lua
|
|
137
|
+
EVALSHA sha1 numkeys key [key] arg [arg] # 通过 SHA1 执行缓存脚本
|
|
138
|
+
SCRIPT LOAD script # 加载脚本到缓存
|
|
139
|
+
SCRIPT EXISTS sha1 # 检查脚本是否在缓存
|
|
140
|
+
SCRIPT FLUSH # 清除脚本缓存
|
|
141
|
+
SCRIPT KILL # 终止正在运行的脚本
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
## 连接命令速查
|
|
145
|
+
|
|
146
|
+
```
|
|
147
|
+
PING # 检查是否 alive
|
|
148
|
+
ECHO message # 回显
|
|
149
|
+
AUTH password # 认证
|
|
150
|
+
SELECT index # 选择数据库
|
|
151
|
+
QUIT # 关闭连接
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
## 服务器命令速查
|
|
155
|
+
|
|
156
|
+
```
|
|
157
|
+
INFO [section] # 服务器信息
|
|
158
|
+
CONFIG GET parameter # 获取配置
|
|
159
|
+
CONFIG SET parameter value # 修改配置 (运行时)
|
|
160
|
+
CONFIG REWRITE # 写入配置文件
|
|
161
|
+
DBSIZE # 当前数据库 key 数量
|
|
162
|
+
FLUSHDB # 清空当前库
|
|
163
|
+
FLUSHALL # 清空所有库
|
|
164
|
+
CLIENT LIST # 客户端列表
|
|
165
|
+
CLIENT KILL ip:port # 杀死客户端连接
|
|
166
|
+
SLOWLOG GET n # 获取慢查询日志
|
|
167
|
+
MONITOR # 实时监控 (生产慎用)
|
|
168
|
+
SAVE # 同步保存 RDB
|
|
169
|
+
BGSAVE # 后台保存 RDB
|
|
170
|
+
BGREWRITEAOF # 重写 AOF
|
|
171
|
+
LASTSAVE # 最后保存时间
|
|
172
|
+
SHUTDOWN # 关闭服务器
|
|
173
|
+
SLAVEOF host port # 设置主从
|
|
174
|
+
ROLE # 查看角色
|
|
175
|
+
TIME # 服务器时间
|
|
176
|
+
DEBUG OBJECT key # key 调试信息
|
|
177
|
+
MEMORY USAGE key # 精确内存
|
|
178
|
+
MEMORY PURGE # 整理碎片
|
|
179
|
+
COMMAND # 命令统计
|
|
180
|
+
```
|