2025-03-21 16:21:33 +08:00
|
|
|
package diagram
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
|
2025-03-24 16:37:43 +08:00
|
|
|
distributed_lock "modelRT/distributedlock"
|
2025-03-21 16:21:33 +08:00
|
|
|
locker "modelRT/distributedlock"
|
2025-03-24 16:37:43 +08:00
|
|
|
"modelRT/logger"
|
2025-03-21 16:21:33 +08:00
|
|
|
|
|
|
|
|
"github.com/redis/go-redis/v9"
|
|
|
|
|
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// RedisHash defines the encapsulation struct of redis hash type
|
|
|
|
|
type RedisHash struct {
|
|
|
|
|
ctx context.Context
|
|
|
|
|
rwLocker *locker.RedissionRWLocker
|
|
|
|
|
storageClient *redis.Client
|
|
|
|
|
logger *zap.Logger
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-24 16:37:43 +08:00
|
|
|
// NewRedisHash define func of new redis hash instance
|
|
|
|
|
func NewRedisHash(ctx context.Context, hashKey string, token string, lockLeaseTime uint64, needRefresh bool) *RedisHash {
|
|
|
|
|
return &RedisHash{
|
|
|
|
|
ctx: ctx,
|
|
|
|
|
rwLocker: distributed_lock.InitRWLocker(hashKey, token, lockLeaseTime, needRefresh),
|
|
|
|
|
storageClient: GetRedisClientInstance(),
|
|
|
|
|
logger: logger.GetLoggerInstance(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-21 16:21:33 +08:00
|
|
|
// SetRedisHashByMap define func of set redis hash by map struct
|
|
|
|
|
func (rh *RedisHash) SetRedisHashByMap(hashKey string, fields map[string]interface{}) error {
|
2025-03-24 16:37:43 +08:00
|
|
|
err := rh.rwLocker.WLock(rh.ctx)
|
2025-03-21 16:21:33 +08:00
|
|
|
if err != nil {
|
|
|
|
|
rh.logger.Error("lock wLock by hashKey failed", zap.String("hashKey", hashKey), zap.Error(err))
|
|
|
|
|
return err
|
|
|
|
|
}
|
2025-03-24 16:37:43 +08:00
|
|
|
defer rh.rwLocker.UnWLock(rh.ctx)
|
2025-03-21 16:21:33 +08:00
|
|
|
|
|
|
|
|
err = rh.storageClient.HSet(rh.ctx, hashKey, fields).Err()
|
|
|
|
|
if err != nil {
|
|
|
|
|
rh.logger.Error("set hash by map failed", zap.String("hashKey", hashKey), zap.Any("fields", fields), zap.Error(err))
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SetRedisHashByKV define func of set redis hash by kv struct
|
|
|
|
|
func (rh *RedisHash) SetRedisHashByKV(hashKey string, field string, value interface{}) error {
|
2025-03-24 16:37:43 +08:00
|
|
|
err := rh.rwLocker.WLock(rh.ctx)
|
2025-03-21 16:21:33 +08:00
|
|
|
if err != nil {
|
|
|
|
|
rh.logger.Error("lock wLock by hashKey failed", zap.String("hashKey", hashKey), zap.Error(err))
|
|
|
|
|
return err
|
|
|
|
|
}
|
2025-03-24 16:37:43 +08:00
|
|
|
defer rh.rwLocker.UnWLock(rh.ctx)
|
2025-03-21 16:21:33 +08:00
|
|
|
|
|
|
|
|
err = rh.storageClient.HSet(rh.ctx, hashKey, field, value).Err()
|
|
|
|
|
if err != nil {
|
|
|
|
|
rh.logger.Error("set hash by kv failed", zap.String("hashKey", hashKey), zap.String("field", field), zap.Any("value", value), zap.Error(err))
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// HGet define func of get specified field value from redis hash by key and field name
|
|
|
|
|
func (rh *RedisHash) HGet(hashKey string, field string) (string, error) {
|
2025-03-24 16:37:43 +08:00
|
|
|
err := rh.rwLocker.RLock(rh.ctx)
|
2025-03-21 16:21:33 +08:00
|
|
|
if err != nil {
|
|
|
|
|
rh.logger.Error("lock rLock by hashKey failed", zap.String("hashKey", hashKey), zap.Error(err))
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
2025-03-24 16:37:43 +08:00
|
|
|
defer rh.rwLocker.UnRLock(rh.ctx)
|
2025-03-21 16:21:33 +08:00
|
|
|
|
|
|
|
|
result, err := rh.storageClient.HGet(rh.ctx, hashKey, field).Result()
|
|
|
|
|
if err != nil {
|
|
|
|
|
rh.logger.Error("set hash by kv failed", zap.String("hashKey", hashKey), zap.String("field", field), zap.Error(err))
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
return result, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// HGetAll define func of get all filelds from redis hash by key
|
|
|
|
|
func (rh *RedisHash) HGetAll(hashKey string) (map[string]string, error) {
|
2025-03-24 16:37:43 +08:00
|
|
|
err := rh.rwLocker.RLock(rh.ctx)
|
2025-03-21 16:21:33 +08:00
|
|
|
if err != nil {
|
|
|
|
|
rh.logger.Error("lock rLock by hashKey failed", zap.String("hashKey", hashKey), zap.Error(err))
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2025-03-24 16:37:43 +08:00
|
|
|
defer rh.rwLocker.UnRLock(rh.ctx)
|
2025-03-21 16:21:33 +08:00
|
|
|
|
|
|
|
|
result, err := rh.storageClient.HGetAll(rh.ctx, hashKey).Result()
|
|
|
|
|
if err != nil {
|
|
|
|
|
rh.logger.Error("get all hash field by hash key failed", zap.String("hashKey", hashKey), zap.Error(err))
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return result, nil
|
|
|
|
|
}
|