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.

127 lines
2.1 KiB

3 years ago
  1. package redis
  2. import (
  3. // "errors"
  4. redisdb "github.com/gomodule/redigo/redis"
  5. )
  6. //hash取值, 返回interface{}
  7. func HGet(key string, field interface{}) (interface{}, error) {
  8. c := pool.Get()
  9. defer c.Close()
  10. return c.Do("HGET", key, field)
  11. }
  12. //hash取值, 返回字符串
  13. func HGetString(key string, field interface{}) (string, error) {
  14. c := pool.Get()
  15. defer c.Close()
  16. ret, err := c.Do("HGET", key, field)
  17. if err != nil {
  18. return "", err
  19. }
  20. return redisdb.String(ret, err)
  21. }
  22. //hash取值,返回接口类型
  23. func HGetStringMap(key string, field interface{}) (map[string]string, error) {
  24. c := pool.Get()
  25. defer c.Close()
  26. ret, err := c.Do("HGET", key, field)
  27. if err != nil {
  28. return ret.(map[string]string), err
  29. }
  30. return redisdb.StringMap(ret, err)
  31. }
  32. //hash取值,返回[]byte
  33. func HGetBytes(key string, field interface{}) ([]byte, error) {
  34. c := pool.Get()
  35. defer c.Close()
  36. ret, err := c.Do("HGET", key, field)
  37. if err != nil {
  38. return nil, err
  39. }
  40. return redisdb.Bytes(ret, err)
  41. }
  42. //hash取所有值
  43. func HGetAll(key string) ([][]byte, error) {
  44. c := pool.Get()
  45. defer c.Close()
  46. ret, err := c.Do("HGETAll", key)
  47. if err != nil {
  48. return nil, err
  49. }
  50. // return ret, err
  51. // return redisdb.MultiBulk(ret, err)
  52. return redisdb.ByteSlices(ret, err)
  53. }
  54. /*
  55. * hash存值,
  56. * key
  57. * field
  58. * value
  59. * 2020/06/06
  60. */
  61. func HSet(key string, field, value interface{}) (interface{}, error) {
  62. c := pool.Get()
  63. defer c.Close()
  64. reply, err := c.Do("HSET", key, field, value)
  65. return reply, err
  66. }
  67. /*
  68. * 删除hash值
  69. */
  70. func HDel(key,field string) error{
  71. c := pool.Get()
  72. defer c.Close()
  73. _, err := c.Do("HDEL", key, field)
  74. return err
  75. }
  76. /*
  77. * hash批量存值,
  78. * args (key,field,value,field,value,field,value...) ,,, ...
  79. * 2020/06/06
  80. */
  81. func HMSet(args ...interface{}) (interface{}, error) {
  82. c := pool.Get()
  83. defer c.Close()
  84. reply, err := c.Do("HMSET", args...)
  85. return reply, err
  86. }
  87. /*
  88. * hash存值,
  89. * key
  90. * expire 过期时长
  91. * 2020/06/06
  92. */
  93. func HSetExpire(key string, expire int) (interface{}, error) {
  94. c := pool.Get()
  95. defer c.Close()
  96. return c.Do("expire", key, expire)
  97. }