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