redis stack 사용
This commit is contained in:
210
core/rejson_handler.go
Normal file
210
core/rejson_handler.go
Normal file
@ -0,0 +1,210 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/go-redis/redis/v8"
|
||||
"github.com/nitishm/go-rejson/v4"
|
||||
"github.com/nitishm/go-rejson/v4/rjs"
|
||||
)
|
||||
|
||||
// gocommon으로 옮길 거
|
||||
type RejsonHandler struct {
|
||||
*redis.Client
|
||||
inner *rejson.Handler
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
func NewReJSONHandler(ctx context.Context, redisClient *redis.Client) *RejsonHandler {
|
||||
inner := rejson.NewReJSONHandler()
|
||||
inner.SetGoRedisClientWithContext(ctx, redisClient)
|
||||
return &RejsonHandler{
|
||||
Client: redisClient,
|
||||
inner: inner,
|
||||
ctx: ctx,
|
||||
}
|
||||
}
|
||||
|
||||
func respToArray[T any](resp any) []T {
|
||||
resArr := resp.([]any)
|
||||
v := make([]T, len(resArr))
|
||||
for i, e := range resArr {
|
||||
v[i] = e.(T)
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
func (rh *RejsonHandler) JSONMSet(key string, kv map[string]any) error {
|
||||
pl := rh.Pipeline()
|
||||
for path, obj := range kv {
|
||||
b, err := json.Marshal(obj)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
pl.Do(rh.ctx, []any{"JSON.SET"}, key, path, b)
|
||||
}
|
||||
|
||||
cmders, err := pl.Exec(rh.ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, cmder := range cmders {
|
||||
if cmder.Err() != nil {
|
||||
return cmder.Err()
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (rh *RejsonHandler) JSONSet(key, path string, obj any, opts ...rjs.SetOption) (res interface{}, err error) {
|
||||
b, err := json.Marshal(obj)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
args := []any{
|
||||
[]any{"JSON.SET"},
|
||||
key,
|
||||
path,
|
||||
b,
|
||||
}
|
||||
if len(opts) > 0 {
|
||||
args = append(args, opts[0])
|
||||
}
|
||||
|
||||
return rh.Do(rh.ctx, args...).Result()
|
||||
}
|
||||
|
||||
func (rh *RejsonHandler) JSONGet(key, path string, opts ...rjs.GetOption) (res interface{}, err error) {
|
||||
return rh.inner.JSONGet(key, path, opts...)
|
||||
}
|
||||
|
||||
func (rh *RejsonHandler) JSONGetString(key, path string) ([]string, error) {
|
||||
res, err := rh.inner.JSONResp(key, path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return respToArray[string](res), nil
|
||||
}
|
||||
|
||||
func (rh *RejsonHandler) JSONGetInt64(key, path string) ([]int64, error) {
|
||||
res, err := rh.inner.JSONResp(key, path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return respToArray[int64](res), nil
|
||||
}
|
||||
|
||||
func (rh *RejsonHandler) JSONMGet(path string, keys ...string) (res interface{}, err error) {
|
||||
return rh.inner.JSONMGet(path, keys...)
|
||||
}
|
||||
|
||||
func (rh *RejsonHandler) JSONMDel(key string, paths []string) error {
|
||||
pl := rh.Pipeline()
|
||||
for _, path := range paths {
|
||||
name, args, err := rjs.CommandBuilder(rjs.ReJSONCommandDEL, key, path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
args = append([]interface{}{name}, args...)
|
||||
pl.Do(rh.ctx, args...)
|
||||
}
|
||||
_, err := pl.Exec(rh.ctx)
|
||||
return err
|
||||
}
|
||||
|
||||
func (rh *RejsonHandler) JSONDel(key, path string) (res interface{}, err error) {
|
||||
return rh.inner.JSONDel(key, path)
|
||||
}
|
||||
|
||||
func (rh *RejsonHandler) JSONType(key, path string) (res interface{}, err error) {
|
||||
return rh.inner.JSONType(key, path)
|
||||
}
|
||||
|
||||
func (rh *RejsonHandler) JSONNumIncrBy(key, path string, number int) (res interface{}, err error) {
|
||||
return rh.inner.JSONNumIncrBy(key, path, number)
|
||||
}
|
||||
|
||||
func (rh *RejsonHandler) JSONNumMultBy(key, path string, number int) (res interface{}, err error) {
|
||||
return rh.inner.JSONNumMultBy(key, path, number)
|
||||
}
|
||||
|
||||
func (rh *RejsonHandler) JSONStrAppend(key, path string, jsonstring string) (res interface{}, err error) {
|
||||
return rh.inner.JSONStrAppend(key, path, jsonstring)
|
||||
}
|
||||
|
||||
func (rh *RejsonHandler) JSONStrLen(key, path string) (res interface{}, err error) {
|
||||
return rh.inner.JSONStrLen(key, path)
|
||||
}
|
||||
|
||||
func (rh *RejsonHandler) JSONArrAppend(key, path string, values ...interface{}) (res interface{}, err error) {
|
||||
return rh.inner.JSONArrAppend(key, path, values...)
|
||||
}
|
||||
|
||||
func (rh *RejsonHandler) JSONArrLen(key, path string) (res interface{}, err error) {
|
||||
return rh.inner.JSONArrLen(key, path)
|
||||
}
|
||||
|
||||
func (rh *RejsonHandler) JSONArrPop(key, path string, index int) (res interface{}, err error) {
|
||||
return rh.inner.JSONArrPop(key, path, index)
|
||||
}
|
||||
|
||||
func (rh *RejsonHandler) JSONArrIndex(key, path string, jsonValue interface{}, optionalRange ...int) (res interface{}, err error) {
|
||||
return rh.inner.JSONArrIndex(key, path, jsonValue, optionalRange...)
|
||||
}
|
||||
|
||||
func (rh *RejsonHandler) JSONArrTrim(key, path string, start, end int) (res interface{}, err error) {
|
||||
return rh.inner.JSONArrTrim(key, path, start, end)
|
||||
}
|
||||
|
||||
func (rh *RejsonHandler) JSONArrInsert(key, path string, index int, values ...interface{}) (res interface{}, err error) {
|
||||
return rh.inner.JSONArrInsert(key, path, index, values...)
|
||||
}
|
||||
|
||||
func (rh *RejsonHandler) JSONObjKeys(key, path string) ([]string, error) {
|
||||
name, args, err := rjs.CommandBuilder(rjs.ReJSONCommandOBJKEYS, key, path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
args = append([]interface{}{name}, args...)
|
||||
res, err := rh.Do(rh.ctx, args...).Result()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resArr := res.([]any)
|
||||
resArr = resArr[0].([]any)
|
||||
slc := make([]string, len(resArr))
|
||||
|
||||
for i, r := range resArr {
|
||||
slc[i] = r.(string)
|
||||
}
|
||||
|
||||
return slc, nil
|
||||
}
|
||||
|
||||
func (rh *RejsonHandler) JSONObjLen(key, path string) (res interface{}, err error) {
|
||||
return rh.inner.JSONObjLen(key, path)
|
||||
}
|
||||
|
||||
func (rh *RejsonHandler) JSONObjLenInt64(key, path string) (int64, error) {
|
||||
res, err := rh.inner.JSONObjLen(key, path)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return res.([]any)[0].(int64), nil
|
||||
}
|
||||
|
||||
func (rh *RejsonHandler) JSONDebug(subCmd rjs.DebugSubCommand, key, path string) (res interface{}, err error) {
|
||||
return rh.inner.JSONDebug(subCmd, key, path)
|
||||
}
|
||||
|
||||
func (rh *RejsonHandler) JSONForget(key, path string) (res interface{}, err error) {
|
||||
return rh.inner.JSONForget(key, path)
|
||||
}
|
||||
|
||||
func (rh *RejsonHandler) JSONResp(key, path string) (res interface{}, err error) {
|
||||
return rh.inner.JSONResp(key, path)
|
||||
}
|
||||
Reference in New Issue
Block a user