Squashed commit of the following:
commit29b2f25850Author: mountain <mountain@action2quare.com> Date: Wed Jul 19 09:33:37 2023 +0900 타입 이름 변경 commit256bfd030cAuthor: mountain <mountain@action2quare.com> Date: Wed Jul 19 09:31:01 2023 +0900 redison 추가 commit72a683fed2Author: mountain <mountain@action2quare.com> Date: Tue Jul 18 19:51:24 2023 +0900 gob에 []any 추가 commit89fa9e4ac5Author: mountain <mountain@action2quare.com> Date: Tue Jul 18 17:45:12 2023 +0900 write control 수정 commitd724cc84faAuthor: mountain <mountain@action2quare.com> Date: Tue Jul 18 17:38:04 2023 +0900 redis pubsub 채널 이름에 디비 인덱스 추가 commit8df248fa54Author: mountain <mountain@action2quare.com> Date: Tue Jul 18 17:20:47 2023 +0900 close를 writecontrol로 변경 commit40a603522dAuthor: mountain <mountain@action2quare.com> Date: Tue Jul 18 12:21:06 2023 +0900 conn에 msg를 쓰는 쓰레드 단일화 commitc21017d2cdAuthor: mountain <mountain@action2quare.com> Date: Tue Jul 18 11:08:38 2023 +0900 redis call이 문제가 아니었음 commit82abcddb49Author: mountain <mountain@action2quare.com> Date: Tue Jul 18 11:04:15 2023 +0900 잦은 redis call 회피 commit289af24a8fAuthor: mountain <mountain@action2quare.com> Date: Tue Jul 18 09:55:18 2023 +0900 room create 메시지 전송 commit4b35e0e638Author: mountain <mountain@action2quare.com> Date: Tue Jul 18 09:45:27 2023 +0900 EventReceiver 인터페이스 추가 commit29843802ffAuthor: mountain <mountain@action2quare.com> Date: Mon Jul 17 17:45:40 2023 +0900 gob 등록 commit66aea48fb7Author: mountain <mountain@action2quare.com> Date: Sun Jul 16 18:39:11 2023 +0900 채널간 publish marshalling을 gob으로 변경 commit8f6c87a8aeAuthor: mountain <mountain@action2quare.com> Date: Sun Jul 16 16:37:02 2023 +0900 redis option을 copy로 변경 commitf0f459332dAuthor: mountain <mountain@action2quare.com> Date: Sat Jul 15 17:08:33 2023 +0900 wshandler에서 authcache제거하고 config 포맷 변경
This commit is contained in:
412
redis.go
412
redis.go
@ -2,9 +2,11 @@ package gocommon
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/url"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/go-redis/redis/v8"
|
||||
)
|
||||
@ -19,23 +21,30 @@ func newRedisClient(uri string, dbidxoffset int) *redis.Client {
|
||||
return redis.NewClient(option)
|
||||
}
|
||||
|
||||
func NewRedisClient(uri string, dbidx int) (*redis.Client, error) {
|
||||
func NewRedisClient(uri string) (*redis.Client, error) {
|
||||
if !*devflag {
|
||||
return newRedisClient(uri, dbidx), nil
|
||||
return newRedisClient(uri, 0), nil
|
||||
}
|
||||
|
||||
rdb := newRedisClient(uri, 0)
|
||||
devUrl, _ := url.Parse(uri)
|
||||
option, err := redis.ParseURL(uri)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
zero := *option
|
||||
zero.DB = 0
|
||||
rdb := redis.NewClient(&zero)
|
||||
defer rdb.Close()
|
||||
|
||||
hostname, _ := os.Hostname()
|
||||
myidx, _ := rdb.HGet(context.Background(), "private_db", hostname).Result()
|
||||
if len(myidx) > 0 {
|
||||
devUrl.Path = "/" + myidx
|
||||
return newRedisClient(devUrl.String(), dbidx), nil
|
||||
offset, _ := strconv.Atoi(myidx)
|
||||
option.DB += offset
|
||||
return redis.NewClient(option), nil
|
||||
}
|
||||
|
||||
alldbs, err := rdb.HGetAll(context.Background(), "private_db").Result()
|
||||
if err != nil {
|
||||
rdb.Close()
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@ -53,6 +62,389 @@ func NewRedisClient(uri string, dbidx int) (*redis.Client, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
devUrl.Path = "/" + strconv.Itoa(newidx)
|
||||
return newRedisClient(devUrl.String(), dbidx), nil
|
||||
option.DB += newidx
|
||||
return redis.NewClient(option), nil
|
||||
}
|
||||
|
||||
type RedisonSetOption = string
|
||||
type RedisonGetOption = [2]any
|
||||
|
||||
const (
|
||||
// JSONSET command Options
|
||||
RedisonSetOptionNX RedisonSetOption = "NX"
|
||||
RedisonSetOptionXX RedisonSetOption = "XX"
|
||||
)
|
||||
|
||||
var (
|
||||
RedisonGetOptionSPACE = RedisonGetOption{"SPACE", " "}
|
||||
RedisonGetOptionINDENT = RedisonGetOption{"INDENT", "\t"}
|
||||
RedisonGetOptionNEWLINE = RedisonGetOption{"NEWLINE", "\n"}
|
||||
RedisonGetOptionNOESCAPE = RedisonGetOption{"NOESCAPE", ""}
|
||||
)
|
||||
|
||||
// gocommon으로 옮길 거
|
||||
type RedisonHandler struct {
|
||||
*redis.Client
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
func NewRedisonHandler(ctx context.Context, redisClient *redis.Client) *RedisonHandler {
|
||||
return &RedisonHandler{
|
||||
Client: redisClient,
|
||||
ctx: ctx,
|
||||
}
|
||||
}
|
||||
|
||||
func respToArray[T any](resp any, err error) ([]T, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resArr := resp.([]any)
|
||||
v := make([]T, len(resArr))
|
||||
for i, e := range resArr {
|
||||
v[i] = e.(T)
|
||||
}
|
||||
return v, nil
|
||||
}
|
||||
|
||||
func appendArgs[T any](args []any, ext ...T) []any {
|
||||
for _, e := range ext {
|
||||
args = append(args, e)
|
||||
}
|
||||
return args
|
||||
}
|
||||
|
||||
func (rh *RedisonHandler) JSONMSetRel(key string, prefixPath string, kv map[string]any) error {
|
||||
if len(prefixPath) > 0 && !strings.HasSuffix(prefixPath, ".") {
|
||||
prefixPath += "."
|
||||
}
|
||||
|
||||
pl := rh.Pipeline()
|
||||
for path, obj := range kv {
|
||||
b, err := json.Marshal(obj)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
pl.Do(rh.ctx, "JSON.SET", key, prefixPath+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 *RedisonHandler) JSONMSet(key string, kv map[string]any) error {
|
||||
return rh.JSONMSetRel(key, "", kv)
|
||||
}
|
||||
|
||||
func (rh *RedisonHandler) jsonSetMergeJSONSet(cmd, key, path string, obj any, opts ...RedisonSetOption) (bool, error) {
|
||||
b, err := json.Marshal(obj)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
args := []any{
|
||||
"JSON.SET",
|
||||
key,
|
||||
path,
|
||||
b,
|
||||
}
|
||||
if len(opts) > 0 {
|
||||
args = append(args, opts[0])
|
||||
}
|
||||
|
||||
res, err := rh.Do(rh.ctx, args...).Result()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return res.(string) == "OK", nil
|
||||
}
|
||||
|
||||
func (rh *RedisonHandler) JSONSet(key, path string, obj any, opts ...RedisonSetOption) (bool, error) {
|
||||
return rh.jsonSetMergeJSONSet("JSON.SET", key, path, obj, opts...)
|
||||
}
|
||||
|
||||
func (rh *RedisonHandler) JSONMerge(key, path string, obj any, opts ...RedisonSetOption) (bool, error) {
|
||||
return rh.jsonSetMergeJSONSet("JSON.MERGE", key, path, obj, opts...)
|
||||
}
|
||||
|
||||
func (rh *RedisonHandler) JSONGet(key, path string, opts ...RedisonGetOption) (res any, err error) {
|
||||
args := appendArgs[string]([]any{
|
||||
"JSON.GET",
|
||||
key,
|
||||
}, strings.Split(path, " ")...)
|
||||
|
||||
for _, opt := range opts {
|
||||
args = append(args, opt[:]...)
|
||||
}
|
||||
|
||||
return rh.Do(rh.ctx, args...).Result()
|
||||
}
|
||||
|
||||
func (rh *RedisonHandler) JSONGetString(key, path string) ([]string, error) {
|
||||
return respToArray[string](rh.JSONResp(key, path))
|
||||
}
|
||||
|
||||
func (rh *RedisonHandler) JSONGetInt64(key, path string) ([]int64, error) {
|
||||
return respToArray[int64](rh.JSONResp(key, path))
|
||||
}
|
||||
|
||||
func (rh *RedisonHandler) JSONMGet(path string, keys ...string) (res any, err error) {
|
||||
args := appendArgs[string]([]any{
|
||||
"JSON.MGET",
|
||||
path,
|
||||
}, keys...)
|
||||
return rh.Do(rh.ctx, args...).Result()
|
||||
}
|
||||
|
||||
func (rh *RedisonHandler) JSONMDel(key string, paths []string) error {
|
||||
pl := rh.Pipeline()
|
||||
for _, path := range paths {
|
||||
args := []any{
|
||||
"JSON.DEL",
|
||||
key,
|
||||
path,
|
||||
}
|
||||
pl.Do(rh.ctx, args...)
|
||||
}
|
||||
_, err := pl.Exec(rh.ctx)
|
||||
return err
|
||||
}
|
||||
|
||||
func (rh *RedisonHandler) JSONDel(key, path string) (int64, error) {
|
||||
args := []any{
|
||||
"JSON.DEL",
|
||||
key,
|
||||
path,
|
||||
}
|
||||
resp, err := rh.Do(rh.ctx, args...).Result()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return resp.(int64), nil
|
||||
}
|
||||
|
||||
func (rh *RedisonHandler) JSONType(key, path string) ([]string, error) {
|
||||
args := []any{
|
||||
"JSON.TYPE",
|
||||
key,
|
||||
path,
|
||||
}
|
||||
return respToArray[string](rh.Do(rh.ctx, args...).Result())
|
||||
}
|
||||
|
||||
func (rh *RedisonHandler) JSONNumIncrBy(key, path string, number int) ([]any, error) {
|
||||
args := []any{
|
||||
"JSON.NUMINCRBY",
|
||||
key,
|
||||
path,
|
||||
number,
|
||||
}
|
||||
resp, err := rh.Do(rh.ctx, args...).Result()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return resp.([]any), nil
|
||||
}
|
||||
|
||||
func (rh *RedisonHandler) JSONNumMultBy(key, path string, number int) (res any, err error) {
|
||||
args := []any{
|
||||
"JSON.NUMMULTBY",
|
||||
key,
|
||||
path,
|
||||
number,
|
||||
}
|
||||
resp, err := rh.Do(rh.ctx, args...).Result()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return resp.([]any), nil
|
||||
}
|
||||
|
||||
func (rh *RedisonHandler) JSONStrAppend(key, path string, jsonstring string) ([]int64, error) {
|
||||
args := []any{
|
||||
"JSON.STRAPPEND",
|
||||
key,
|
||||
path,
|
||||
fmt.Sprintf(`'"%s"'`, jsonstring),
|
||||
}
|
||||
return respToArray[int64](rh.Do(rh.ctx, args...).Result())
|
||||
}
|
||||
|
||||
func (rh *RedisonHandler) JSONStrLen(key, path string) (res []int64, err error) {
|
||||
args := []any{
|
||||
"JSON.STRLEN",
|
||||
key,
|
||||
path,
|
||||
}
|
||||
return respToArray[int64](rh.Do(rh.ctx, args...).Result())
|
||||
}
|
||||
|
||||
func (rh *RedisonHandler) JSONArrAppend(key, path string, values ...any) (int64, error) {
|
||||
args := []any{
|
||||
"JSON.ARRAPPEND",
|
||||
key,
|
||||
path,
|
||||
}
|
||||
resp, err := rh.Do(rh.ctx, args...).Result()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return resp.(int64), nil
|
||||
}
|
||||
|
||||
func (rh *RedisonHandler) JSONArrLen(key, path string) ([]int64, error) {
|
||||
args := []any{
|
||||
"JSON.ARRLEN",
|
||||
key,
|
||||
path,
|
||||
}
|
||||
return respToArray[int64](rh.Do(rh.ctx, args...).Result())
|
||||
}
|
||||
|
||||
func (rh *RedisonHandler) JSONArrPop(key, path string, index int) (res any, err error) {
|
||||
args := []any{
|
||||
"JSON.ARRPOP",
|
||||
key,
|
||||
path,
|
||||
index,
|
||||
}
|
||||
resp, err := rh.Do(rh.ctx, args...).Result()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resp.([]any)[0], nil
|
||||
}
|
||||
|
||||
func appendValues(args []any, values ...any) []any {
|
||||
for _, jsonValue := range values {
|
||||
switch jsonValue := jsonValue.(type) {
|
||||
case string:
|
||||
args = append(args, fmt.Sprintf(`'"%s"'`, jsonValue))
|
||||
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
|
||||
args = append(args, jsonValue)
|
||||
default:
|
||||
bt, _ := json.Marshal(jsonValue)
|
||||
args = append(args, bt)
|
||||
}
|
||||
}
|
||||
return args
|
||||
}
|
||||
|
||||
func (rh *RedisonHandler) JSONArrIndex(key, path string, jsonValue any, optionalRange ...int) ([]int64, error) {
|
||||
args := appendValues([]any{
|
||||
"JSON.ARRINDEX",
|
||||
key,
|
||||
path,
|
||||
}, jsonValue)
|
||||
|
||||
return respToArray[int64](rh.Do(rh.ctx, args...).Result())
|
||||
}
|
||||
|
||||
func (rh *RedisonHandler) JSONArrTrim(key, path string, start, end int) (res any, err error) {
|
||||
args := []any{
|
||||
"JSON.ARRTRIM",
|
||||
key,
|
||||
path,
|
||||
start,
|
||||
end,
|
||||
}
|
||||
return respToArray[int64](rh.Do(rh.ctx, args...).Result())
|
||||
}
|
||||
|
||||
func (rh *RedisonHandler) JSONArrInsert(key, path string, index int, values ...any) (res any, err error) {
|
||||
args := appendValues([]any{
|
||||
"JSON.ARRINSERT",
|
||||
key,
|
||||
path,
|
||||
index,
|
||||
}, values...)
|
||||
|
||||
return respToArray[int64](rh.Do(rh.ctx, args...).Result())
|
||||
}
|
||||
|
||||
func (rh *RedisonHandler) JSONObjKeys(key, path string) ([]string, error) {
|
||||
args := []any{
|
||||
"JSON.OBJKEYS",
|
||||
key,
|
||||
path,
|
||||
}
|
||||
|
||||
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 *RedisonHandler) JSONObjLen(key, path string) ([]int64, error) {
|
||||
args := []any{
|
||||
"JSON.OBJLEN",
|
||||
key,
|
||||
}
|
||||
|
||||
if path != "$" {
|
||||
args = append(args, path)
|
||||
}
|
||||
|
||||
resp, err := rh.Do(rh.ctx, args...).Result()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
switch resp := resp.(type) {
|
||||
case []any:
|
||||
return respToArray[int64](resp, nil)
|
||||
|
||||
case int64:
|
||||
return []int64{resp}, nil
|
||||
}
|
||||
|
||||
return []int64{0}, nil
|
||||
}
|
||||
|
||||
func (rh *RedisonHandler) JSONDebug(key, path string) (res any, err error) {
|
||||
args := []any{
|
||||
"JSON.DEBUG",
|
||||
"MEMORY",
|
||||
key,
|
||||
path,
|
||||
}
|
||||
return rh.Do(rh.ctx, args...).Result()
|
||||
}
|
||||
|
||||
func (rh *RedisonHandler) JSONForget(key, path string) (int64, error) {
|
||||
return rh.JSONDel(key, path)
|
||||
}
|
||||
|
||||
func (rh *RedisonHandler) JSONResp(key, path string) (res any, err error) {
|
||||
args := []any{
|
||||
"JSON.RESP",
|
||||
key,
|
||||
path,
|
||||
}
|
||||
return rh.Do(rh.ctx, args...).Result()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user