chore: 并入 iotlcm-app 历史到 app/(原提交 f40bda9)

This commit is contained in:
MiFaZhan
2026-11-02 13:28:00 +08:00
1107 changed files with 147143 additions and 0 deletions
+11
View File
@@ -0,0 +1,11 @@
package gfredis
import (
"context"
"github.com/redis/go-redis/v9"
)
func (r *Redis) Conn(ctx context.Context) (*redis.Conn, error) {
return r.rdb.Conn(), nil
}
+150
View File
@@ -0,0 +1,150 @@
package gfredis
import (
"context"
"crypto/tls"
"sync"
"time"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/util/gconv"
"github.com/redis/go-redis/v9"
)
const (
DefaultGroupName = "default" // Default configuration group name.
)
var (
lock sync.RWMutex
instances map[string]*Redis
)
func init() {
instances = make(map[string]*Redis)
}
type Redis struct {
config *Config
rdb *redis.Client
}
// Config is redis configuration.
type Config struct {
// Address It supports single and cluster redis server. Multiple addresses joined with char ','. Eg: 192.0.2.10:6379, 192.0.2.10:6379.
Address string `json:"address"`
Db int `json:"db"` // Redis db.
User string `json:"user"` // Username for AUTH.
Pass string `json:"pass"` // Password for AUTH.
SentinelUser string `json:"sentinel_user"` // Username for sentinel AUTH.
SentinelPass string `json:"sentinel_pass"` // Password for sentinel AUTH.
MinIdle int `json:"minIdle"` // Minimum number of connections allowed to be idle (default is 0)
MaxIdle int `json:"maxIdle"` // Maximum number of connections allowed to be idle (default is 10)
MaxActive int `json:"maxActive"` // Maximum number of connections limit (default is 0 means no limit).
MaxConnLifetime time.Duration `json:"maxConnLifetime"` // Maximum lifetime of the connection (default is 30 seconds, not allowed to be set to 0)
IdleTimeout time.Duration `json:"idleTimeout"` // Maximum idle time for connection (default is 10 seconds, not allowed to be set to 0)
WaitTimeout time.Duration `json:"waitTimeout"` // Timed out duration waiting to get a connection from the connection pool.
DialTimeout time.Duration `json:"dialTimeout"` // Dial connection timeout for TCP.
ReadTimeout time.Duration `json:"readTimeout"` // Read timeout for TCP. DO NOT set it if not necessary.
WriteTimeout time.Duration `json:"writeTimeout"` // Write timeout for TCP.
MasterName string `json:"masterName"` // Used in Redis Sentinel mode.
TLS bool `json:"tls"` // Specifies whether TLS should be used when connecting to the server.
TLSSkipVerify bool `json:"tlsSkipVerify"` // Disables server name verification when connecting over TLS.
TLSConfig *tls.Config `json:"-"` // TLS Config to use. When set TLS will be negotiated.
SlaveOnly bool `json:"slaveOnly"` // Route all commands to slave read-only nodes.
Cluster bool `json:"cluster"` // Specifies whether cluster mode be used.
Protocol int `json:"protocol"` // Specifies the RESP version (Protocol 2 or 3.)
}
func New(groupName string, config *Config) *Redis {
instance := &Redis{
config: config,
}
if config == nil {
instance.rdb = redis.NewClient(&redis.Options{})
return instance
}
poolSize := config.MaxActive
if poolSize == 0 {
poolSize = 10
config.MaxActive = 10
}
if config.IdleTimeout < 1*time.Minute {
config.IdleTimeout = 30 * time.Minute
}
if config.MaxConnLifetime <= 1 {
config.MaxConnLifetime = 24 * 365 * 10 * time.Hour
}
if config.WaitTimeout < 1*time.Second {
config.WaitTimeout = 6 * time.Second
}
if config.DialTimeout < 1*time.Second {
config.DialTimeout = 5 * time.Second
}
if config.ReadTimeout < 1*time.Second {
config.ReadTimeout = 5 * time.Second
}
if config.WriteTimeout < 1*time.Second {
config.WriteTimeout = 5 * time.Second
}
opt := &redis.Options{
Addr: config.Address,
Password: config.Pass,
DB: config.Db,
MinIdleConns: config.MinIdle,
MaxIdleConns: config.MaxIdle,
MaxActiveConns: config.MaxActive,
ConnMaxIdleTime: config.IdleTimeout,
PoolSize: poolSize,
PoolTimeout: config.WaitTimeout,
DialTimeout: config.DialTimeout,
ReadTimeout: config.ReadTimeout,
WriteTimeout: config.WriteTimeout,
TLSConfig: config.TLSConfig,
// IdentitySuffix: config.IdentitySuffix,
// ConnMaxLifetime: config.MaxConnLifetime,
}
instance.rdb = redis.NewClient(opt)
g.Log().Noticef(context.Background(), "groupName: %s, opt: %+v", groupName, opt)
return instance
}
func GetConfig(name string) *Config {
cfg, err := g.Cfg().Get(context.Background(), "redis."+name)
if err != nil {
panic(err)
}
if cfg == nil || cfg.IsNil() {
panic("redis config not found: " + name)
}
config := &Config{}
if err = gconv.Scan(cfg.Map(), config); err != nil {
panic(`invalid redis configuration: ` + cfg.String())
}
return config
}
func Instance(name ...string) *Redis {
groupName := DefaultGroupName
if len(name) > 0 {
groupName = name[0]
if groupName == "" {
groupName = DefaultGroupName
}
}
lock.RLock()
instance, ok := instances[groupName]
lock.RUnlock()
if !ok {
lock.Lock()
instance, ok = instances[groupName]
if !ok {
instance = New(groupName, GetConfig(groupName))
instances[groupName] = instance
}
lock.Unlock()
return instance
}
return instance
}
+51
View File
@@ -0,0 +1,51 @@
package gfredis
import (
"context"
"github.com/gogf/gf/v2/util/gconv"
"github.com/redis/go-redis/v9"
)
func (r *Redis) HMSet(ctx context.Context, key string, mp map[string]interface{}) error {
mp2 := make(map[string]string)
for k, v := range mp {
mp2[k] = gconv.String(v)
}
cmd := r.rdb.HMSet(ctx, key, mp2)
return cmd.Err()
}
func (r *Redis) HGetAll(ctx context.Context, key string) (map[string]string, error) {
cmd := r.rdb.HGetAll(ctx, key)
return cmd.Val(), cmd.Err()
}
func (r *Redis) HGet(ctx context.Context, key, field string) (string, error) {
cmd := r.rdb.HGet(ctx, key, field)
if cmd.Err() == redis.Nil {
return "", nil
}
return cmd.Val(), cmd.Err()
}
func (r *Redis) HSet(ctx context.Context, key string, mp map[string]interface{}) error {
mp2 := make(map[string]string)
for k, v := range mp {
mp2[k] = gconv.String(v)
}
cmd := r.rdb.HSet(ctx, key, mp2)
return cmd.Err()
}
func (r *Redis) HSetNX(ctx context.Context, key, field string, value interface{}) (bool, error) {
cmd := r.rdb.HSetNX(ctx, key, field, gconv.String(value))
return cmd.Val(), cmd.Err()
}
func (r *Redis) HDel(ctx context.Context, key string, field string) (int64, error) {
cmd := r.rdb.HDel(ctx, key, field)
return cmd.Val(), cmd.Err()
}
func (r *Redis) HLen(ctx context.Context, key string) (int64, error) {
cmd := r.rdb.HLen(ctx, key)
return cmd.Val(), cmd.Err()
}
+16
View File
@@ -0,0 +1,16 @@
package gfredis
import (
"context"
"github.com/gogf/gf/v2/frame/g"
"github.com/redis/go-redis/v9"
)
func (r *Redis) Do(ctx context.Context, args ...interface{}) (*g.Var, error) {
cmd := r.rdb.Do(ctx, args...)
if cmd.Err() == redis.Nil {
return g.NewVar(nil), nil
}
return g.NewVar(cmd.Val()), cmd.Err()
}
+37
View File
@@ -0,0 +1,37 @@
package gfredis
import (
"context"
"time"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/util/gconv"
"github.com/redis/go-redis/v9"
)
func (r *Redis) LRange(ctx context.Context, key string, start, stop int64) (*g.Var, error) {
cmd := r.rdb.LRange(ctx, key, start, stop)
if cmd.Err() == redis.Nil {
return g.NewVar(nil), nil
}
return g.NewVar(cmd.Val()), cmd.Err()
}
func (r *Redis) RPush(ctx context.Context, key string, value interface{}) (int64, error) {
cmd := r.rdb.RPush(ctx, key, gconv.String(value))
return cmd.Val(), cmd.Err()
}
func (r *Redis) LLen(ctx context.Context, key string) (int64, error) {
cmd := r.rdb.LLen(ctx, key)
return cmd.Val(), cmd.Err()
}
func (r *Redis) LTrim(ctx context.Context, key string, start, stop int64) error {
cmd := r.rdb.LTrim(ctx, key, start, stop)
return cmd.Err()
}
func (r *Redis) BLPop(ctx context.Context, timeout time.Duration, keys ...string) (*g.Var, error) {
cmd := r.rdb.BLPop(ctx, timeout, keys...)
if cmd.Err() == redis.Nil {
return g.NewVar(nil), nil
}
return g.NewVar(cmd.Val()), cmd.Err()
}
+42
View File
@@ -0,0 +1,42 @@
package gfredis
import (
"context"
"time"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/util/gconv"
"github.com/redis/go-redis/v9"
)
func (r *Redis) Set(ctx context.Context, key string, value interface{}, expiration time.Duration) error {
cmd := r.rdb.Set(ctx, key, gconv.String(value), expiration)
return cmd.Err()
}
func (r *Redis) SetNX(ctx context.Context, key string, value interface{}, expiration time.Duration) (bool, error) {
cmd := r.rdb.SetNX(ctx, key, gconv.String(value), expiration)
return cmd.Val(), cmd.Err()
}
func (r *Redis) Get(ctx context.Context, key string) (*g.Var, error) {
cmd := r.rdb.Get(ctx, key)
if cmd.Err() == redis.Nil {
return g.NewVar(nil), nil
}
return g.NewVar(cmd.Val()), cmd.Err()
}
func (r *Redis) UpdateExpire(ctx context.Context, key string, expiration time.Duration) (bool, error) {
cmd := r.rdb.Expire(ctx, key, expiration)
return cmd.Val(), cmd.Err()
}
func (r *Redis) Remove(ctx context.Context, key string) (bool, error) {
cmd := r.rdb.Del(ctx, key)
return cmd.Val() > 0, cmd.Err()
}
func (r *Redis) Del(ctx context.Context, key string) (int64, error) {
cmd := r.rdb.Del(ctx, key)
return cmd.Val(), cmd.Err()
}
+18
View File
@@ -0,0 +1,18 @@
package gfredis
import (
"context"
"github.com/gogf/gf/v2/util/gconv"
"github.com/redis/go-redis/v9"
)
func (r *Redis) Subscribe(ctx context.Context, channels ...string) (*redis.PubSub, error) {
cmd := r.rdb.Subscribe(ctx, channels...)
return cmd, nil
}
func (r *Redis) Publish(ctx context.Context, channel string, message interface{}) (int64, error) {
cmd := r.rdb.Publish(ctx, channel, gconv.String(message))
return cmd.Val(), cmd.Err()
}
+21
View File
@@ -0,0 +1,21 @@
package gfredis
import (
"context"
)
func (r *Redis) ZPopByScore(ctx context.Context, key string, min, max string, limit int64) (res []string, err error) {
var luaScript = `
local members = redis.call('ZRANGEBYSCORE', KEYS[1], ARGV[1], ARGV[2], 'LIMIT', 0, ARGV[3])
if #members > 0 then
redis.call('ZREM', KEYS[1], unpack(members))
end
return members
`
cmd := r.rdb.Eval(ctx, luaScript, []string{key}, min, max, limit)
if err = cmd.Err(); err != nil {
return nil, err
}
res, err = cmd.StringSlice()
return
}