添加redis方法

This commit is contained in:
lifeiyi
2020-04-21 18:09:42 +08:00
parent a189c2c80b
commit c06af0ab4f
3 changed files with 83 additions and 7 deletions

View File

@@ -6,9 +6,10 @@ import (
"github.com/duanhf2012/origin/network/processor"
"github.com/duanhf2012/origin/node"
"github.com/duanhf2012/origin/service"
"github.com/duanhf2012/origin/sysmodule"
"github.com/duanhf2012/origin/sysservice"
"github.com/duanhf2012/origin/util/timer"
"net/http"
"time"
)
type GateService struct {
@@ -16,6 +17,8 @@ type GateService struct {
processor *processor.PBProcessor
processor2 *processor.PBProcessor
httpRouter sysservice.IHttpRouter
redisModule *sysmodule.RedisModule
}
func (slf *GateService) OnInit() error{
@@ -42,8 +45,25 @@ func (slf *GateService) OnInit() error{
slf.httpRouter.POST("/post/query", slf.HttpTestPost)
slf.httpRouter.SetServeFile(sysservice.METHOD_GET,"/img/head/","d:/img")
pCronExpr,_ := timer.NewCronExpr("0 * * * * *")
slf.CronFunc(pCronExpr,slf.Test)
//pCronExpr,_ := timer.NewCronExpr("0 * * * * *")
//slf.CronFunc(pCronExpr,slf.Test)
redisCfg := sysmodule.ConfigRedis{
IP :"192.168.0.5",
Port :"6379",
Password :"",
DbIndex :0,
MaxIdle :50, //最大的空闲连接数表示即使没有redis连接时依然可以保持N个空闲的连接而不被清除随时处于待命状态。
MaxActive :50, //最大的激活连接数表示同时最多有N个连接
IdleTimeout :30, //最大的空闲连接等待时间,超过此时间后,空闲连接将被关闭
SyncRouterNum :10, //异步执行Router数量
}
slf.redisModule = &sysmodule.RedisModule{}
slf.redisModule.Init(&redisCfg)
slf.redisModule.OnInit()
slf.AddModule(slf.redisModule)
slf.AfterFunc(time.Second, slf.TestRedis)
return nil
}
@@ -52,6 +72,11 @@ func (slf *GateService) Test(){
fmt.Print("xxxxx\n")
}
func (slf *GateService) TestRedis() {
slf.redisModule.GetHashValueByHashKeyList("BITGET_2160_LastSetLevelInfo", "SBTC_USD_1", "BTC_SUSDT_1", "SBTC_USD_3")
//slf.redisModule.GetHashValueByKey("BITGET_2160_LastSetLevelInfo", "SBTC_USD_1")
}
func (slf *GateService) HttpTest(session *sysservice.HttpSession) {
session.SetHeader("a","b")
session.Write([]byte("this is a test"))

View File

@@ -4,13 +4,13 @@ import (
"fmt"
"github.com/duanhf2012/origin/event"
"github.com/duanhf2012/origin/example/GateService"
"github.com/duanhf2012/origin/example/msgpb"
//"github.com/duanhf2012/origin/example/msgpb"
"github.com/duanhf2012/origin/log"
"github.com/duanhf2012/origin/node"
"github.com/duanhf2012/origin/service"
"github.com/duanhf2012/origin/sysmodule"
"github.com/duanhf2012/origin/sysservice"
"github.com/golang/protobuf/proto"
//"github.com/golang/protobuf/proto"
"time"
)
@@ -249,12 +249,12 @@ func (slf *TestService1) RPC_Test(a *Param,b *Param) error {
return nil
}
func (slf *TestService1) RPC_TestPB(a *msgpb.InputRpc,b *msgpb.OutputRpc) error {
/*func (slf *TestService1) RPC_TestPB(a *msgpb.InputRpc,b *msgpb.OutputRpc) error {
b.Msg = proto.String(a.GetMsg())
b.Tag = proto.Int32(a.GetTag())
return nil
}
}*/
func (slf *TestService1) OnInit() error {

View File

@@ -725,6 +725,57 @@ func (slf *RedisModule) GetHashValueByKey(redisKey string, fieldKey string) (str
return string(str), nil
}
//GetHashValueByHashKeyList ...
func (slf *RedisModule) GetHashValueByHashKeyList(fieldKey ...interface{}) ([]string, error) {
if len(fieldKey) < 2 {
log.Error("GetHashValueByHashKeyList key len less than two!")
return nil, errors.New("Key Is Empty")
}
conn, err := slf.getConn()
if err != nil {
return nil, err
}
defer conn.Close()
value, err := conn.Do("HMGET", fieldKey...)
if err != nil {
log.Error("GetHashValueByKey fail,reason:%v", err)
return nil, err
}
if value == nil {
return nil, errors.New("Reids Get Hash nil")
}
valueList := value.([]interface{})
retList := []string{}
for _, valueItem := range valueList{
valueByte, ok := valueItem.([]byte)
if !ok {
retList = append(retList, "")
} else {
retList = append(retList, string(valueByte))
}
}
return retList, nil
}
/*func (slf *RedisModule) ScanMatchKeys(cursorValue int, redisKey string, count int) ([]string, error) {
retKeys := []string{}
if redisKey == "" {
log.Error("ScanMatchKeys key is empty!")
return nil, errors.New("Key Is Empty")
}
conn, err := slf.getConn()
if err != nil {
return nil, err
}
defer conn.Close()
return retKeys, nil
}*/
//SetRedisHashJSON ...
func (slf *RedisModule) SetHashJSON(redisKey, hsahKey string, value interface{}) error {
temp, err := json.Marshal(value)