2026-05-30 19:26:27 +08:00
|
|
|
package redis
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"errors"
|
2026-06-10 12:04:34 +08:00
|
|
|
"strings"
|
2026-05-30 19:26:27 +08:00
|
|
|
|
2026-05-30 19:44:31 +08:00
|
|
|
"apigo.cc/go/id"
|
2026-05-30 19:26:27 +08:00
|
|
|
"apigo.cc/go/jsmod"
|
2026-06-20 22:08:56 +08:00
|
|
|
"apigo.cc/go/log"
|
2026-05-30 19:26:27 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
jsmod.Register("redis", map[string]any{
|
2026-06-10 12:04:34 +08:00
|
|
|
// 入口:支持别名获取,不传则默认 "default"
|
|
|
|
|
"Get": func(ctx context.Context, name *string) (*jsRedis, error) {
|
|
|
|
|
target := "default"
|
|
|
|
|
if name != nil {
|
|
|
|
|
target = *name
|
|
|
|
|
}
|
|
|
|
|
rd := GetRedis(target, nil)
|
2026-05-30 19:26:27 +08:00
|
|
|
if rd.Error != nil {
|
|
|
|
|
return nil, rd.Error
|
|
|
|
|
}
|
|
|
|
|
return &jsRedis{rd: rd, ctx: ctx}, nil
|
|
|
|
|
},
|
2026-06-10 12:04:34 +08:00
|
|
|
|
|
|
|
|
// 默认快捷调用 (面向 "default" 实例)
|
|
|
|
|
"Do": func(ctx context.Context, cmd string, args ...any) (*Result, error) {
|
2026-06-20 22:08:56 +08:00
|
|
|
jr := getDefaultRedisForJS(ctx)
|
2026-06-10 12:04:34 +08:00
|
|
|
if jr.rd.Error != nil {
|
|
|
|
|
return nil, jr.rd.Error
|
|
|
|
|
}
|
|
|
|
|
res := jr.Do(cmd, args...)
|
|
|
|
|
return res, res.Error
|
|
|
|
|
},
|
|
|
|
|
|
2026-06-20 22:08:56 +08:00
|
|
|
"MakeID": func(ctx context.Context, size int, forDB *string) string {
|
|
|
|
|
jr := getDefaultRedisForJS(ctx)
|
|
|
|
|
if jr.rd.Error != nil {
|
|
|
|
|
return id.MakeID(size)
|
|
|
|
|
}
|
|
|
|
|
return jr.MakeID(size, forDB)
|
2026-06-10 12:04:34 +08:00
|
|
|
},
|
2026-05-30 19:26:27 +08:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type jsRedis struct {
|
2026-05-30 19:44:31 +08:00
|
|
|
rd *Redis
|
|
|
|
|
ctx context.Context
|
|
|
|
|
idMaker *id.IDMaker
|
2026-05-30 19:26:27 +08:00
|
|
|
}
|
|
|
|
|
|
2026-06-20 22:08:56 +08:00
|
|
|
var defaultRedisForJS *jsRedis
|
|
|
|
|
|
|
|
|
|
func getDefaultRedisForJS(ctx context.Context) *jsRedis {
|
|
|
|
|
if defaultRedisForJS == nil {
|
|
|
|
|
defaultRedisForJS = &jsRedis{rd: GetRedis("default", ctx.Value("Logger").(*log.Logger)), ctx: ctx}
|
|
|
|
|
}
|
|
|
|
|
return defaultRedisForJS
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-30 19:44:31 +08:00
|
|
|
var errSafeMode = errors.New("redis operation is restricted in safe mode")
|
2026-05-30 19:26:27 +08:00
|
|
|
|
2026-06-10 12:04:34 +08:00
|
|
|
// 核心写操作指令集
|
|
|
|
|
var writeCommands = map[string]bool{
|
|
|
|
|
"SET": true, "SETEX": true, "SETNX": true, "MSET": true, "MSETNX": true,
|
|
|
|
|
"DEL": true, "EXPIRE": true, "EXPIREAT": true, "PEXPIRE": true, "PEXPIREAT": true,
|
|
|
|
|
"HSET": true, "HSETNX": true, "HDEL": true, "HMSET": true,
|
|
|
|
|
"LPUSH": true, "RPUSH": true, "LPOP": true, "RPOP": true, "LREM": true, "LTRIM": true,
|
|
|
|
|
"SADD": true, "SREM": true, "SPOP": true, "SMOVE": true,
|
|
|
|
|
"ZADD": true, "ZREM": true, "ZREMRANGEBYRANK": true, "ZREMRANGEBYSCORE": true,
|
|
|
|
|
"PUBLISH": true, "FLUSHDB": true, "FLUSHALL": true,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (jr *jsRedis) checkSafe(cmd string) error {
|
2026-05-30 19:26:27 +08:00
|
|
|
if jsmod.IsSafeMode(jr.ctx) {
|
2026-06-10 12:04:34 +08:00
|
|
|
cmd = strings.ToUpper(cmd)
|
|
|
|
|
if writeCommands[cmd] || !strings.Contains(" GET EXISTS ZRANGE HGET HGETALL SMEMBERS SISMEMBER LINDEX LLEN ", " "+cmd+" ") {
|
|
|
|
|
// 严格模式:不在白名单内的或在黑名单内的都禁止
|
|
|
|
|
return errSafeMode
|
|
|
|
|
}
|
2026-05-30 19:26:27 +08:00
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-30 19:44:31 +08:00
|
|
|
func (jr *jsRedis) Do(cmd string, args ...any) *Result {
|
2026-06-10 12:04:34 +08:00
|
|
|
if err := jr.checkSafe(cmd); err != nil {
|
|
|
|
|
return &Result{Error: err}
|
2026-05-30 19:26:27 +08:00
|
|
|
}
|
2026-05-30 19:44:31 +08:00
|
|
|
return jr.rd.Do(cmd, args...)
|
2026-05-30 19:26:27 +08:00
|
|
|
}
|
2026-05-30 19:44:31 +08:00
|
|
|
|
2026-06-10 12:04:34 +08:00
|
|
|
// ID Generation
|
|
|
|
|
func (jr *jsRedis) MakeID(size int, forDB *string) string {
|
2026-05-30 19:44:31 +08:00
|
|
|
if jr.idMaker == nil {
|
|
|
|
|
jr.idMaker = NewIDMaker(jr.rd)
|
2026-05-30 19:26:27 +08:00
|
|
|
}
|
2026-06-10 12:04:34 +08:00
|
|
|
dbType := ""
|
|
|
|
|
if forDB != nil {
|
|
|
|
|
dbType = strings.ToLower(*forDB)
|
|
|
|
|
}
|
|
|
|
|
switch dbType {
|
|
|
|
|
case "mysql":
|
|
|
|
|
return jr.idMaker.GetForMysql(size)
|
|
|
|
|
case "postgres", "pg", "pgsql":
|
|
|
|
|
return jr.idMaker.GetForPostgreSQL(size)
|
|
|
|
|
default:
|
|
|
|
|
return jr.idMaker.Get(size)
|
|
|
|
|
}
|
2026-05-30 19:26:27 +08:00
|
|
|
}
|