From 1ddbd44a2d5f5f37ee98ed7256a1a3943dc44507 Mon Sep 17 00:00:00 2001 From: guzeng Date: Tue, 27 Apr 2021 01:20:04 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0watch=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 5 ++++ go.sum | 3 +++ hash_test.go | 12 ++++++--- int.go | 17 ++++++++++++ int_test.go | 13 +++++++++ string_test.go | 2 ++ watch.go | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++ watch_test.go | 71 +++++++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 191 insertions(+), 4 deletions(-) create mode 100644 go.mod create mode 100644 go.sum create mode 100644 int.go create mode 100644 int_test.go create mode 100644 watch.go create mode 100644 watch_test.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..0ca04ce --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module git.tetele.net/tgo/redis + +go 1.14 + +require github.com/gomodule/redigo v2.0.0+incompatible // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..4c1e172 --- /dev/null +++ b/go.sum @@ -0,0 +1,3 @@ +github.com/gomodule/redigo v1.7.0 h1:ZKld1VOtsGhAe37E7wMxEDgAlGM5dvFY+DiOhSkhP9Y= +github.com/gomodule/redigo v2.0.0+incompatible h1:K/R+8tc58AaqLkqG2Ol3Qk+DR/TlNuhuh457pBFPtt0= +github.com/gomodule/redigo v2.0.0+incompatible/go.mod h1:B4C85qUVwatsJoIUNIfCRsp7qO0iAmpGFZ4EELWSbC4= diff --git a/hash_test.go b/hash_test.go index 41ad2b4..1b074d1 100644 --- a/hash_test.go +++ b/hash_test.go @@ -18,15 +18,19 @@ func Test_HSet(t *testing.T) { // ret, err := HGetString("testing2", "test1") // t.Log(ret) - // t.Log(err) + // if err == nil { + // t.Log("nil", err) + // } else { + // t.Log("error:", err) + // } // ret2, err := HSetExpire("testing2", 200) // t.Log(ret2) // t.Log(err) - // ret3, err := HMSet("testing2", "test1", "test1", "test2", "test2", "test3", "test3") - // t.Log(ret3) - // t.Log(err) + ret3, err := HMSet("testing2", "test1", "test1") + t.Log(ret3) + t.Log(err) ret4, err := HGetAll("1000064_product") // ret := ret4.([]interface{}) diff --git a/int.go b/int.go new file mode 100644 index 0000000..782563a --- /dev/null +++ b/int.go @@ -0,0 +1,17 @@ +package redis + +func Incr(key string) (interface{}, error) { + + c := pool.Get() + defer c.Close() + + return c.Do("INCR", key) +} + +func Decr(key string) (interface{}, error) { + + c := pool.Get() + defer c.Close() + + return c.Do("DECR", key) +} diff --git a/int_test.go b/int_test.go new file mode 100644 index 0000000..49aa2f4 --- /dev/null +++ b/int_test.go @@ -0,0 +1,13 @@ +package redis + +import ( + "testing" +) + +func Test_Incr(t *testing.T) { + reply, err := Incr("xxx") + t.Log(reply, err) + + reply, err := Decr("xxx") + t.Log(reply, err) +} diff --git a/string_test.go b/string_test.go index 877fe81..79aa341 100644 --- a/string_test.go +++ b/string_test.go @@ -18,4 +18,6 @@ func Test_Set(t *testing.T) { c, err := Set("test", 1111, 7200) t.Log(c) t.Log(err) + + p,err := pool. } diff --git a/watch.go b/watch.go new file mode 100644 index 0000000..793b601 --- /dev/null +++ b/watch.go @@ -0,0 +1,72 @@ +package redis + +import ( + "errors" + "time" + + redisdb "github.com/gomodule/redigo/redis" +) + +type Tx struct { + conn redisdb.Conn +} + +func NewTx() (*Tx, error) { + var conn redisdb.Conn + var i int = 0 + var err error + for { + if i > 100 { + break + } + conn = pool.Get() + if conn != nil { + break + } else { + err = errors.New("no conn") + } + i++ + time.Sleep(time.Microsecond * 1000) + } + if conn != nil { + n := &Tx{conn: pool.Get()} + return n, nil + } else { + return nil, err + } +} + +func (tx *Tx) GetInt(key string) (int, error) { + + return redisdb.Int(tx.conn.Do("GET", key)) +} + +func (tx *Tx) Watch(key string) (interface{}, error) { + + return tx.conn.Do("WATCH", key) +} + +func (tx *Tx) Multi() (interface{}, error) { + + return tx.conn.Do("MULTI") +} + +func (tx *Tx) Exec() (interface{}, error) { + + return tx.conn.Do("Exec") +} + +func (tx *Tx) Incr(key string) (interface{}, error) { + + return tx.conn.Do("INCR", key) +} + +func (tx *Tx) Decr(key string) (interface{}, error) { + + return tx.conn.Do("DECR", key) +} + +func (tx *Tx) Close() error { + + return tx.conn.Close() +} diff --git a/watch_test.go b/watch_test.go new file mode 100644 index 0000000..f131dc3 --- /dev/null +++ b/watch_test.go @@ -0,0 +1,71 @@ +package redis + +import ( + "strconv" + "testing" + // "time" +) + +var succ []int + +func Test_Watch(t *testing.T) { + + var key string = "tttttt" + + // var tx *Tx + var count int + + for i := 0; i < 100000; i++ { + + // t.Log(i) + + go func(i int) { + + // t.Log(i) + tx, err := NewTx() + // t.Log(tx, err) + + if err == nil { + + reply, err := tx.Watch(key) + + // t.Log("watch:", reply, err) + + count_str, err := Get(key) + + count, _ = strconv.Atoi(count_str) + + // t.Log("count:", count, err) + + reply, err = tx.Multi() + + // t.Log("Multi:", reply, err) + + if count > 0 && err == nil { + + // t.Log("start decr,", i) + + reply, err = tx.Decr(key) + + // t.Log("Decr:", reply, err) + + reply, err = tx.Exec() + + // t.Log("Exec:", exec_reply, err, i) + + if err == nil && reply != nil { + // reply, err = HSet("succ", i, i) + succ = append(succ, i) + t.Log("succ:", i, succ) + } + + } + + err = tx.Close() + // t.Log("Close:", err) + } + }(i) + } + + t.Log(succ) +}