redis操作
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
1.0 KiB

  1. package redis
  2. import (
  3. "errors"
  4. "time"
  5. redisdb "github.com/gomodule/redigo/redis"
  6. )
  7. type Tx struct {
  8. conn redisdb.Conn
  9. }
  10. func NewTx() (*Tx, error) {
  11. var conn redisdb.Conn
  12. var i int = 0
  13. var err error
  14. for {
  15. if i > 100 {
  16. break
  17. }
  18. conn = pool.Get()
  19. if conn != nil {
  20. break
  21. } else {
  22. err = errors.New("no conn")
  23. }
  24. i++
  25. time.Sleep(time.Microsecond * 1000)
  26. }
  27. if conn != nil {
  28. n := &Tx{conn: pool.Get()}
  29. return n, nil
  30. } else {
  31. return nil, err
  32. }
  33. }
  34. func (tx *Tx) GetInt(key string) (int, error) {
  35. return redisdb.Int(tx.conn.Do("GET", key))
  36. }
  37. func (tx *Tx) Watch(key string) (interface{}, error) {
  38. return tx.conn.Do("WATCH", key)
  39. }
  40. func (tx *Tx) Multi() (interface{}, error) {
  41. return tx.conn.Do("MULTI")
  42. }
  43. func (tx *Tx) Exec() (interface{}, error) {
  44. return tx.conn.Do("Exec")
  45. }
  46. func (tx *Tx) Incr(key string) (interface{}, error) {
  47. return tx.conn.Do("INCR", key)
  48. }
  49. func (tx *Tx) Decr(key string) (interface{}, error) {
  50. return tx.conn.Do("DECR", key)
  51. }
  52. func (tx *Tx) Close() error {
  53. return tx.conn.Close()
  54. }