From eb92470865b56986698da06438647949ff2828b1 Mon Sep 17 00:00:00 2001 From: lifeiyi <736926938@qq.com> Date: Tue, 21 Apr 2020 18:25:59 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0redis=20scan=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/GateService/GateService.go | 3 ++- sysmodule/RedisModule.go | 28 +++++++++++++++++++++++----- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/example/GateService/GateService.go b/example/GateService/GateService.go index e92d9eb..35f6d5c 100644 --- a/example/GateService/GateService.go +++ b/example/GateService/GateService.go @@ -73,8 +73,9 @@ func (slf *GateService) Test(){ } func (slf *GateService) TestRedis() { - slf.redisModule.GetHashValueByHashKeyList("BITGET_2160_LastSetLevelInfo", "SBTC_USD_1", "BTC_SUSDT_1", "SBTC_USD_3") + //slf.redisModule.GetHashValueByHashKeyList("BITGET_2160_LastSetLevelInfo", "SBTC_USD_1", "BTC_SUSDT_1", "SBTC_USD_3") //slf.redisModule.GetHashValueByKey("BITGET_2160_LastSetLevelInfo", "SBTC_USD_1") + slf.redisModule.ScanMatchKeys(192, "ZC*", 100) } func (slf *GateService) HttpTest(session *sysservice.HttpSession) { diff --git a/sysmodule/RedisModule.go b/sysmodule/RedisModule.go index a9cb69f..6ee60ad 100644 --- a/sysmodule/RedisModule.go +++ b/sysmodule/RedisModule.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" "github.com/duanhf2012/origin/log" + "strconv" "time" "github.com/duanhf2012/origin/service" @@ -760,21 +761,38 @@ func (slf *RedisModule) GetHashValueByHashKeyList(fieldKey ...interface{}) ([]st return retList, nil } -/*func (slf *RedisModule) ScanMatchKeys(cursorValue int, redisKey string, count int) ([]string, error) { +func (slf *RedisModule) ScanMatchKeys(cursorValue int, redisKey string, count int) (int, []string, error) { retKeys := []string{} + nextCursorValue := 0 if redisKey == "" { log.Error("ScanMatchKeys key is empty!") - return nil, errors.New("Key Is Empty") + return nextCursorValue, nil, errors.New("Key Is Empty") } conn, err := slf.getConn() if err != nil { - return nil, err + return nextCursorValue, nil, err } defer conn.Close() - return retKeys, nil -}*/ + value, err := conn.Do("SCAN", cursorValue, "match", redisKey, "count", count) + if err != nil { + log.Error("GetHashValueByKey fail,reason:%v", err) + return nextCursorValue, nil, err + } + if value == nil { + return nextCursorValue, nil, errors.New("Reids Get Hash nil") + } + + valueList := value.([]interface{}) + nextCursorValue, _ = strconv.Atoi(string(valueList[0].([]byte))) + keysList := valueList[1].([]interface{}) + for _, keysItem := range keysList { + retKeys = append(retKeys, string(keysItem.([]byte))) + } + + return nextCursorValue, retKeys, nil +} //SetRedisHashJSON ... func (slf *RedisModule) SetHashJSON(redisKey, hsahKey string, value interface{}) error {