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取所有值
|
|
|
|
func HGetList(key 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 []string
|
|
|
|
if len(reply) > 0 {
|
|
for key, item := range reply {
|
|
if (key+1)%2 == 0 { //只处理偶数位
|
|
all = append(all, string(item))
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
CloseConn(c)
|
|
return all, err
|
|
|
|
}
|
|
|
|
//hash取所有值
|
|
|
|
func HGetAllItem(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 = make(map[string]string)
|
|
|
|
if len(reply) > 0 {
|
|
for key, item := range reply {
|
|
if key%2 == 0 { //只处理奇数位
|
|
info[string(item)] = string(reply[key+1])
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
CloseConn(c)
|
|
return info, 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批量查询,
|
|
* args (key,field,field,field...) 域,名,名,名 ...
|
|
* 2020/06/06
|
|
*/
|
|
func HMGet(args ...interface{}) (interface{}, error) {
|
|
c := GetConn()
|
|
|
|
reply, err := c.Do("HMGET", args...)
|
|
CloseConn(c)
|
|
return reply, err
|
|
}
|
|
|
|
/*
|
|
* hash批量查询,
|
|
* args (key,field,field,field...) 域,名,名,名 ...
|
|
* 2020/06/06
|
|
*/
|
|
func HMGetString(args ...interface{}) ([]string, error) {
|
|
c := GetConn()
|
|
|
|
reply, err := c.Do("HMGET", args...)
|
|
CloseConn(c)
|
|
|
|
list := make([]string, 0)
|
|
|
|
if err == nil {
|
|
list, err = redisdb.Strings(reply, err)
|
|
}
|
|
|
|
return list, 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)
|
|
}
|
|
}
|
|
|
|
/**
|
|
* hash 键名
|
|
*/
|
|
func HKeys(key string) ([]string, error) {
|
|
c := GetConn()
|
|
|
|
ret, err := c.Do("HKEYS", key)
|
|
|
|
reply := make([]string, 0)
|
|
|
|
if err == nil {
|
|
reply, err = redisdb.Strings(ret, err)
|
|
}
|
|
CloseConn(c)
|
|
return reply, err
|
|
|
|
}
|
|
|
|
/**
|
|
* hash 键值
|
|
*/
|
|
func HVals(key string) ([]string, error) {
|
|
c := GetConn()
|
|
|
|
ret, err := c.Do("HVALS", key)
|
|
|
|
reply := make([]string, 0)
|
|
|
|
if err == nil {
|
|
reply, err = redisdb.Strings(ret, err)
|
|
}
|
|
CloseConn(c)
|
|
return reply, err
|
|
|
|
}
|