|
|
- package redis
-
- import (
- redisdb "github.com/gomodule/redigo/redis"
- )
-
- //hash取值, 返回interface{}
-
- func HGet(key string, field interface{}) (interface{}, error) {
- c := GetConn()
-
- reply, err := c.Do("HGET", key, field)
- CloseConn(c)
-
- return reply, err
- }
-
- //hash取值, 返回字符串
-
- func HGetString(key string, field interface{}) (string, error) {
- c := GetConn()
-
- ret, err := c.Do("HGET", key, field)
-
- reply := ""
- if err == nil {
- reply, err = redisdb.String(ret, err)
- }
- CloseConn(c)
- return reply, err
- }
-
- //hash取值,返回接口类型
-
- func HGetStringMap(key string, field interface{}) (map[string]string, error) {
- c := GetConn()
-
- ret, err := c.Do("HGET", key, field)
-
- reply := make(map[string]string)
-
- if err == nil {
- reply, err = redisdb.StringMap(ret, err)
- }
- CloseConn(c)
- return reply, err
-
- }
-
- //hash取值,返回[]byte
-
- func HGetBytes(key string, field interface{}) ([]byte, error) {
- c := GetConn()
-
- ret, err := c.Do("HGET", key, field)
-
- reply := make([]byte, 0)
-
- if err == nil {
- reply, err = redisdb.Bytes(ret, err)
- }
- CloseConn(c)
- return reply, err
-
- }
-
- //hash取所有值
-
- func HGetAll(key string) ([]map[string]string, error) {
- c := GetConn()
-
- ret, err := c.Do("HGETAll", key)
-
- reply := make([][]byte, 0)
-
- if err == nil {
- reply, err = redisdb.ByteSlices(ret, err)
- }
-
- var info map[string]string
- var all []map[string]string
-
- if len(reply) > 0 {
- for key, item := range reply {
- if key%2 == 0 { //只处理奇数位
- info = make(map[string]string)
- info[string(item)] = string(reply[key+1])
- all = append(all, info)
- }
-
- }
- }
-
- CloseConn(c)
- return all, err
-
- }
-
- /*
- * hash存值,
- * key 域
- * field 名
- * value 值
- * 2020/06/06
- */
- func HSet(key string, field, value interface{}) (int64, error) {
- c := GetConn()
-
- reply, err := c.Do("HSET", key, field, value)
- CloseConn(c)
-
- if err != nil {
- return 0, err
- } else {
- return redisdb.Int64(reply, nil)
- }
-
- }
-
- /*
- * hash存值,
- * key 域
- * field 名
- * value 值
- * 2020/06/06
- */
- func HIncrby(key string, field, value interface{}) (int64, error) {
- c := GetConn()
-
- reply, err := c.Do("hincrby", key, field, value)
- CloseConn(c)
-
- if err != nil {
- return 0, err
- } else {
- return redisdb.Int64(reply, nil)
- }
-
- }
-
- /*
- * 删除hash值
- */
- func HDel(key, field string) (int64, error) {
- c := GetConn()
-
- reply, err := c.Do("HDEL", key, field)
-
- CloseConn(c)
-
- if err != nil {
- return 0, err
- } else {
- return redisdb.Int64(reply, nil)
- }
- }
-
- /*
- * hash批量存值,
- * args (key,field,value,field,value,field,value...) 域,名,值,名,值,名,值 ...
- * 2020/06/06
- */
- func HMSet(args ...interface{}) (interface{}, error) {
- c := GetConn()
-
- reply, err := c.Do("HMSET", args...)
- CloseConn(c)
- return reply, err
- }
-
- /*
- * hash存值,
- * key 域
- * expire 过期时长
- * 2020/06/06
- */
- func HSetExpire(key string, expire int) (interface{}, error) {
- c := GetConn()
- reply, err := c.Do("expire", key, expire)
-
- CloseConn(c)
- return reply, err
- }
-
- /**
- * hash键是否存在
- * key 域
- * field 名
- */
- func HExists(key, field string) (int64, error) {
- c := GetConn()
-
- reply, err := c.Do("HEXISTS", key, field)
- CloseConn(c)
-
- if err != nil {
- return 0, err
- } else {
- return redisdb.Int64(reply, nil)
- }
- }
|