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.

80 lines
1.3 KiB

  1. package redis
  2. import (
  3. redisdb "github.com/gomodule/redigo/redis"
  4. )
  5. //list 尾部增加值
  6. func Rpush(key string, field interface{}) (int64, error) {
  7. c := GetConn()
  8. reply, err := c.Do("RPUSH", key, field)
  9. CloseConn(c)
  10. if err != nil {
  11. return 0, err
  12. }
  13. return redisdb.Int64(reply, nil)
  14. }
  15. //list 头部增加值
  16. func Lpush(key string, field interface{}) (int64, error) {
  17. c := GetConn()
  18. reply, err := c.Do("LPUSH", key, field)
  19. CloseConn(c)
  20. if err != nil {
  21. return 0, err
  22. }
  23. return redisdb.Int64(reply, nil)
  24. }
  25. //list 长度
  26. func Llen(key string) (int64, error) {
  27. c := GetConn()
  28. reply, err := c.Do("LLEN", key)
  29. CloseConn(c)
  30. if err != nil {
  31. return 0, err
  32. }
  33. return redisdb.Int64(reply, nil)
  34. }
  35. //list 通过索引设置列表元素的值 LSET key index value
  36. func Lset(key string, index int, value interface{}) (interface{}, error) {
  37. c := GetConn()
  38. reply, err := c.Do("LSET", key, index, value)
  39. CloseConn(c)
  40. return reply, err
  41. }
  42. /*
  43. LRANGE key start stop
  44. 获取列表指定范围内的元素
  45. */
  46. func Lrange(key string, start, stop int64) ([][]byte, error) {
  47. c := GetConn()
  48. ret, err := c.Do("LRANGE", key, start, stop)
  49. reply := make([][]byte, 0)
  50. if err == nil {
  51. reply, err = redisdb.ByteSlices(ret, err)
  52. }
  53. CloseConn(c)
  54. return reply, err
  55. }