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.

93 lines
1.5 KiB

2 years ago
2 years ago
  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. //移除列表的最后一个元素,返回值为移除的元素。
  16. func Rpop(key string) ([]byte, error) {
  17. c := GetConn()
  18. ret, err := c.Do("RPOP", key)
  19. CloseConn(c)
  20. if err != nil {
  21. return nil, err
  22. }
  23. reply, err := redisdb.Bytes(ret, err)
  24. return reply, err
  25. }
  26. //list 头部增加值
  27. func Lpush(key string, field interface{}) (int64, error) {
  28. c := GetConn()
  29. reply, err := c.Do("LPUSH", key, field)
  30. CloseConn(c)
  31. if err != nil {
  32. return 0, err
  33. }
  34. return redisdb.Int64(reply, nil)
  35. }
  36. //list 长度
  37. func Llen(key string) (int64, error) {
  38. c := GetConn()
  39. reply, err := c.Do("LLEN", key)
  40. CloseConn(c)
  41. if err != nil {
  42. return 0, err
  43. }
  44. return redisdb.Int64(reply, nil)
  45. }
  46. //list 通过索引设置列表元素的值 LSET key index value
  47. func Lset(key string, index int, value interface{}) (interface{}, error) {
  48. c := GetConn()
  49. reply, err := c.Do("LSET", key, index, value)
  50. CloseConn(c)
  51. return reply, err
  52. }
  53. /*
  54. LRANGE key start stop
  55. 获取列表指定范围内的元素
  56. */
  57. func Lrange(key string, start, stop int64) ([][]byte, error) {
  58. c := GetConn()
  59. ret, err := c.Do("LRANGE", key, start, stop)
  60. reply := make([][]byte, 0)
  61. if err == nil {
  62. reply, err = redisdb.ByteSlices(ret, err)
  63. }
  64. CloseConn(c)
  65. return reply, err
  66. }