From 9a1da461eadb2a5fd734b22b25746406bcd6edc3 Mon Sep 17 00:00:00 2001 From: listen <494177603@qq.com> Date: Tue, 7 Dec 2021 13:58:33 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=9B=86=E5=90=88=E6=88=90?= =?UTF-8?q?=E5=91=98=E6=95=B0=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 1 + go.sum | 2 ++ redis.proto | 1 + string.go | 24 ++++++++++++++++++++++++ string_test.go | 2 +- 5 files changed, 29 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 82ea8b2..84a00d7 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.14 require ( git.tetele.net/tgo/conf v0.35.3 + git.tetele.net/tgo/helper v0.2.6 github.com/chai2010/protorpc v1.0.0 github.com/golang/protobuf v1.5.2 ) diff --git a/go.sum b/go.sum index ef593ba..f0c63f1 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,8 @@ git.tetele.net/tgo/conf v0.25.0 h1:fopDch45xw/di5fLvvzwltFiGiXrilMobZwQNO678Wo= git.tetele.net/tgo/conf v0.25.0/go.mod h1:DogEBvxG2fGdukpoobTVFE2b4Fd5OTE9FJ3Xetyn47E= git.tetele.net/tgo/conf v0.35.3 h1:OQEa87qN5bAbscjMhaoTRinLnv8xZg1WErl5JXgFZco= git.tetele.net/tgo/conf v0.35.3/go.mod h1:AWVIBEDE5dtotthUgR0SWaR2Qa6/f+O5WQ3s7Tj8q7A= +git.tetele.net/tgo/helper v0.2.6 h1:JC+N+If/bGvcwuUSklva17YDNtKBV2Qpvl5RQpMySOY= +git.tetele.net/tgo/helper v0.2.6/go.mod h1:89mQwyfqZ+t8YXiVwzSxA70gLlUNqoZGDEUxvV46jXk= github.com/chai2010/protorpc v1.0.0 h1:aJ45G9sl1utSKo35EqnBSTs5jqTpdJDJAuZMMYPAtFo= github.com/chai2010/protorpc v1.0.0/go.mod h1:woR3WwjaQDqFjlzdVsFEKiK5Ur12QL8mYxVPjfr5z54= github.com/golang/protobuf v1.0.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= diff --git a/redis.proto b/redis.proto index 2c02d1c..eb287c5 100644 --- a/redis.proto +++ b/redis.proto @@ -121,6 +121,7 @@ service RedisService { rpc SAdd(SSetRequest) returns (SSetResponse); //添加进入集合 rpc SIsmember(SSetRequest) returns (SSetResponse); //集合中是否存在值 rpc SRem(SSetRequest) returns (SSetResponse); //集合中删除值 + rpc SCard(SSetRequest) returns (SSetResponse); //获取集合的成员数 rpc LLpush(LSetRequest) returns (LSetResponse); //列表头部增加值 rpc LRpush(LSetRequest) returns (LSetResponse); //列表尾部增加值 rpc LRange(LRangeRequest) returns (HGetListResponse); //列表尾部增加值 diff --git a/string.go b/string.go index fad0325..75d012f 100644 --- a/string.go +++ b/string.go @@ -95,3 +95,27 @@ func Exists(key string, url ...string)(int64, error){ return res.GetRet(), nil } + + +//设置如果不存在 +func SetNx(key, value string, ttl int64, url ...string) (int64, error) { + + conn, _, err := Conn(url...) + + if err != nil { + return 0, err + } + defer conn.Close() + + req := &SetRequest{proto.String(key), proto.String(value), proto.Int64(ttl), nil} + + res := &DelResponse{} + + err = conn.SetNx(req, res) + + if err != nil { + return 0, err + } + + return res.GetRet(), nil +} \ No newline at end of file diff --git a/string_test.go b/string_test.go index 604f3f4..a48c7f6 100644 --- a/string_test.go +++ b/string_test.go @@ -27,4 +27,4 @@ func Test_Del(t *testing.T) { t.Log(c) t.Log(err) -} +} \ No newline at end of file