Browse Source

增加watch操作

master v0.2.0
guzeng 3 years ago
parent
commit
1ddbd44a2d
8 changed files with 191 additions and 4 deletions
  1. +5
    -0
      go.mod
  2. +3
    -0
      go.sum
  3. +8
    -4
      hash_test.go
  4. +17
    -0
      int.go
  5. +13
    -0
      int_test.go
  6. +2
    -0
      string_test.go
  7. +72
    -0
      watch.go
  8. +71
    -0
      watch_test.go

+ 5
- 0
go.mod View File

@ -0,0 +1,5 @@
module git.tetele.net/tgo/redis
go 1.14
require github.com/gomodule/redigo v2.0.0+incompatible // indirect

+ 3
- 0
go.sum View File

@ -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=

+ 8
- 4
hash_test.go View File

@ -18,15 +18,19 @@ func Test_HSet(t *testing.T) {
// ret, err := HGetString("testing2", "test1") // ret, err := HGetString("testing2", "test1")
// t.Log(ret) // t.Log(ret)
// t.Log(err)
// if err == nil {
// t.Log("nil", err)
// } else {
// t.Log("error:", err)
// }
// ret2, err := HSetExpire("testing2", 200) // ret2, err := HSetExpire("testing2", 200)
// t.Log(ret2) // t.Log(ret2)
// t.Log(err) // 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") ret4, err := HGetAll("1000064_product")
// ret := ret4.([]interface{}) // ret := ret4.([]interface{})


+ 17
- 0
int.go View File

@ -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)
}

+ 13
- 0
int_test.go View File

@ -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)
}

+ 2
- 0
string_test.go View File

@ -18,4 +18,6 @@ func Test_Set(t *testing.T) {
c, err := Set("test", 1111, 7200) c, err := Set("test", 1111, 7200)
t.Log(c) t.Log(c)
t.Log(err) t.Log(err)
p,err := pool.
} }

+ 72
- 0
watch.go View File

@ -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()
}

+ 71
- 0
watch_test.go View File

@ -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)
}

Loading…
Cancel
Save