feat(redis-string-class): 1. data token parse 2. redis string get 3. redis string set 4. redis string incr
This commit is contained in:
parent
3e833909d1
commit
426409ed91
|
|
@ -4,40 +4,52 @@ package database
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"modelRT/diagram"
|
||||||
"modelRT/model"
|
"modelRT/model"
|
||||||
|
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ParseAttrToken define return the attribute model interface based on the input attribute token
|
// ParseAttrToken define return the attribute model interface based on the input attribute token. doc addr http://server.baseware.net:6875/books/product-design-docs/page/d6baf
|
||||||
func ParseAttrToken(ctx context.Context, tx *gorm.DB, attrToken string) (model.AttrModelInterface, error) {
|
func ParseAttrToken(ctx context.Context, tx *gorm.DB, attrToken string) (model.AttrModelInterface, error) {
|
||||||
|
rs := diagram.NewRedisString(ctx, attrToken, "", 10, true)
|
||||||
|
|
||||||
attrSlice := strings.Split(attrToken, ".")
|
attrSlice := strings.Split(attrToken, ".")
|
||||||
attrLen := len(attrSlice)
|
attrLen := len(attrSlice)
|
||||||
if attrLen == 4 {
|
if attrLen == 4 {
|
||||||
short := &model.ShortAttrInfo{
|
short := &model.ShortAttrInfo{
|
||||||
AttrGroupName: attrSlice[2],
|
AttrGroupName: attrSlice[2],
|
||||||
AttrKey: attrSlice[3],
|
AttrKey: attrSlice[3],
|
||||||
// TODO use redis query
|
|
||||||
// AttrValue: attrSlice[2],
|
|
||||||
}
|
}
|
||||||
err := FillingShortAttrModel(ctx, tx, attrSlice, short)
|
err := FillingShortAttrModel(ctx, tx, attrSlice, short)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
attrValue, err := rs.Get(attrToken)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
short.AttrValue = attrValue
|
||||||
return short, nil
|
return short, nil
|
||||||
} else if attrLen == 7 {
|
} else if attrLen == 7 {
|
||||||
long := &model.LongAttrInfo{
|
long := &model.LongAttrInfo{
|
||||||
AttrGroupName: attrSlice[5],
|
AttrGroupName: attrSlice[5],
|
||||||
AttrKey: attrSlice[6],
|
AttrKey: attrSlice[6],
|
||||||
// TODO use redis query
|
|
||||||
// AttrValue: attrSlice[5],
|
|
||||||
}
|
}
|
||||||
err := FillingLongAttrModel(ctx, tx, attrSlice, long)
|
err := FillingLongAttrModel(ctx, tx, attrSlice, long)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
attrValue, err := rs.Get(attrToken)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
long.AttrValue = attrValue
|
||||||
return long, nil
|
return long, nil
|
||||||
}
|
}
|
||||||
return nil, errors.New("invalid attribute token format")
|
return nil, errors.New("invalid attribute token format")
|
||||||
|
|
@ -77,3 +89,9 @@ func FillingLongAttrModel(ctx context.Context, tx *gorm.DB, attrItems []string,
|
||||||
attrModel.ComponentInfo = &component
|
attrModel.ComponentInfo = &component
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// QueryAttrValueFromRedis define query attribute value from redis by attrKey
|
||||||
|
func QueryAttrValueFromRedis(attrKey string) string {
|
||||||
|
fmt.Println(attrKey)
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,97 @@
|
||||||
|
package diagram
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
locker "modelRT/distributedlock"
|
||||||
|
"modelRT/logger"
|
||||||
|
|
||||||
|
"github.com/redis/go-redis/v9"
|
||||||
|
"go.uber.org/zap"
|
||||||
|
)
|
||||||
|
|
||||||
|
// RedisString defines the encapsulation struct of redis string type
|
||||||
|
type RedisString struct {
|
||||||
|
ctx context.Context
|
||||||
|
rwLocker *locker.RedissionRWLocker
|
||||||
|
storageClient *redis.Client
|
||||||
|
logger *zap.Logger
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewRedisString define func of new redis string instance
|
||||||
|
func NewRedisString(ctx context.Context, stringKey string, token string, lockLeaseTime uint64, needRefresh bool) *RedisString {
|
||||||
|
return &RedisString{
|
||||||
|
ctx: ctx,
|
||||||
|
rwLocker: locker.InitRWLocker(stringKey, token, lockLeaseTime, needRefresh),
|
||||||
|
storageClient: GetRedisClientInstance(),
|
||||||
|
logger: logger.GetLoggerInstance(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get define func of get the value of key
|
||||||
|
func (rs *RedisString) Get(stringKey string) (string, error) {
|
||||||
|
err := rs.rwLocker.RLock(rs.ctx)
|
||||||
|
if err != nil {
|
||||||
|
logger.Error(rs.ctx, "lock rLock by stringKey failed", "string_key", stringKey, "error", err)
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
defer rs.rwLocker.UnRLock(rs.ctx)
|
||||||
|
|
||||||
|
value, err := rs.storageClient.Get(rs.ctx, stringKey).Result()
|
||||||
|
if err != nil {
|
||||||
|
logger.Error(rs.ctx, "get string value by key failed", "string_key", stringKey, "error", err)
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return value, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set define func of set the value of key
|
||||||
|
func (rs *RedisString) Set(stringKey string, value interface{}) error {
|
||||||
|
err := rs.rwLocker.WLock(rs.ctx)
|
||||||
|
if err != nil {
|
||||||
|
logger.Error(rs.ctx, "lock wLock by stringKey failed", "string_key", stringKey, "error", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer rs.rwLocker.UnWLock(rs.ctx)
|
||||||
|
|
||||||
|
err = rs.storageClient.Set(rs.ctx, stringKey, value, redis.KeepTTL).Err()
|
||||||
|
if err != nil {
|
||||||
|
logger.Error(rs.ctx, "get string value by key failed", "string_key", stringKey, "error", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Incr define func of increments the number stored at key by one
|
||||||
|
func (rs *RedisString) Incr(stringKey string) error {
|
||||||
|
err := rs.rwLocker.WLock(rs.ctx)
|
||||||
|
if err != nil {
|
||||||
|
logger.Error(rs.ctx, "lock wLock by stringKey failed", "string_key", stringKey, "error", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer rs.rwLocker.UnWLock(rs.ctx)
|
||||||
|
|
||||||
|
err = rs.storageClient.Incr(rs.ctx, stringKey).Err()
|
||||||
|
if err != nil {
|
||||||
|
logger.Error(rs.ctx, "incr the number stored at key by one failed", "string_key", stringKey, "error", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// IncrBy define func of increments the number stored at key by increment
|
||||||
|
func (rs *RedisString) IncrBy(stringKey string, value int64) error {
|
||||||
|
err := rs.rwLocker.WLock(rs.ctx)
|
||||||
|
if err != nil {
|
||||||
|
logger.Error(rs.ctx, "lock wLock by stringKey failed", "string_key", stringKey, "error", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer rs.rwLocker.UnWLock(rs.ctx)
|
||||||
|
|
||||||
|
err = rs.storageClient.IncrBy(rs.ctx, stringKey, value).Err()
|
||||||
|
if err != nil {
|
||||||
|
logger.Error(rs.ctx, "incr the number stored at key by increment", "string_key", stringKey, "error", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue