package redis
|
|
|
|
import (
|
|
// "errors"
|
|
|
|
redisdb "github.com/gomodule/redigo/redis"
|
|
)
|
|
|
|
//hash取值, 返回interface{}
|
|
|
|
func HGet(key string, field interface{}) (interface{}, error) {
|
|
c := pool.Get()
|
|
defer c.Close()
|
|
|
|
return c.Do("HGET", key, field)
|
|
}
|
|
|
|
//hash取值, 返回字符串
|
|
|
|
func HGetString(key string, field interface{}) (string, error) {
|
|
c := pool.Get()
|
|
defer c.Close()
|
|
|
|
ret, err := c.Do("HGET", key, field)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return redisdb.String(ret, err)
|
|
}
|
|
|
|
//hash取值,返回接口类型
|
|
|
|
func HGetStringMap(key string, field interface{}) (map[string]string, error) {
|
|
c := pool.Get()
|
|
defer c.Close()
|
|
|
|
ret, err := c.Do("HGET", key, field)
|
|
if err != nil {
|
|
return ret.(map[string]string), err
|
|
}
|
|
return redisdb.StringMap(ret, err)
|
|
|
|
}
|
|
|
|
//hash取值,返回[]byte
|
|
|
|
func HGetBytes(key string, field interface{}) ([]byte, error) {
|
|
c := pool.Get()
|
|
defer c.Close()
|
|
|
|
ret, err := c.Do("HGET", key, field)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return redisdb.Bytes(ret, err)
|
|
|
|
}
|
|
|
|
//hash取所有值
|
|
|
|
func HGetAll(key string) ([][]byte, error) {
|
|
c := pool.Get()
|
|
defer c.Close()
|
|
|
|
ret, err := c.Do("HGETAll", key)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
// return ret, err
|
|
// return redisdb.MultiBulk(ret, err)
|
|
return redisdb.ByteSlices(ret, err)
|
|
|
|
}
|
|
|
|
/*
|
|
* hash存值,
|
|
* key 域
|
|
* field 名
|
|
* value 值
|
|
* 2020/06/06
|
|
*/
|
|
func HSet(key string, field, value interface{}) (interface{}, error) {
|
|
c := pool.Get()
|
|
defer c.Close()
|
|
|
|
reply, err := c.Do("HSET", key, field, value)
|
|
|
|
return reply, err
|
|
}
|
|
|
|
/*
|
|
* 删除hash值
|
|
*/
|
|
func HDel(key,field string) error{
|
|
c := pool.Get()
|
|
defer c.Close()
|
|
|
|
_, err := c.Do("HDEL", key, field)
|
|
|
|
return err
|
|
}
|
|
|
|
/*
|
|
* hash批量存值,
|
|
* args (key,field,value,field,value,field,value...) 域,名,值,名,值,名,值 ...
|
|
* 2020/06/06
|
|
*/
|
|
func HMSet(args ...interface{}) (interface{}, error) {
|
|
c := pool.Get()
|
|
defer c.Close()
|
|
|
|
reply, err := c.Do("HMSET", args...)
|
|
|
|
return reply, err
|
|
}
|
|
|
|
/*
|
|
* hash存值,
|
|
* key 域
|
|
* expire 过期时长
|
|
* 2020/06/06
|
|
*/
|
|
func HSetExpire(key string, expire int) (interface{}, error) {
|
|
c := pool.Get()
|
|
defer c.Close()
|
|
return c.Do("expire", key, expire)
|
|
}
|