redis hset方法
redis hset方法:initimport ("falcon/middleware/contrib/cache""io/ioutil"//"jobs/contrib/cache""os""strings""time"// "falcon/middleware/contrib/cache"// "github.com/garyburd/redigo/redis"_ "github.com/jin
·
redis hset方法:
init
import (
"falcon/middleware/contrib/cache"
"io/ioutil"
//"jobs/contrib/cache"
"os"
"strings"
"time"
// "falcon/middleware/contrib/cache"
// "github.com/garyburd/redigo/redis"
_ "github.com/jinzhu/gorm/dialects/mysql"
_ "github.com/astaxie/beego"
"github.com/jinzhu/gorm"
log "github.com/wonderivan/logger"
"gopkg.in/yaml.v2"
)
var redisClient *cache.RedisStore
type Conf struct {
Mode string `yaml:"runmode"`
Host string `yaml:"host"`
Maxkeep int `yaml:"maxkeep"`
Maxnew int `yaml:"maxnew"`
Maxevent int `yaml:"maxevent"`
Client string `yaml:"client"`
Launch string `yaml:"launch"`
Event string `yaml:"event"`
ClientRuntimes int `yaml:"clientruntimes"`
Clearredis bool `yaml:"clearredis"`
AccessKey string `yaml:"accesskey"`
SecretKey string `yaml:"secretkey"`
Initdevicefrom string `yaml:"initdevicefrom"`
Totaldevices int `yaml:"totaldevices"`
// DbHost string `yaml:"dbHost"`
// DbUser string `yaml:"dbUser"`
// DbPass string `yaml:"dbPass"`
Appversions []string `yaml:"appversions"`
Models []string `yaml:"models"`
Channels []string `yaml:"channels"`
Mysql MysqlConf
Redis RedisConf
Api string `yaml:"api"`
App string `yaml:"app"`
Reset string `yaml:"reset"`
Start string `yaml:"start"`
}
type RedisConf struct {
Host string
Password string
Namespace string
Expires string
MaxIdle int
MaxActive int
Wait bool
Db string
PoolSize int
RedisDefaultExpires string
}
var defaultExpire time.Duration
var redisStore *cache.RedisStore
func InitRedis(conf *RedisConf) {
expires, _ := time.ParseDuration(conf.RedisDefaultExpires)
defaultExpire = expires
log.Debug("[Redis Starting]...:host: ", conf.Host)
redisClient := cache.NewRedisCache(conf.Host, conf.Password, expires, conf.Namespace, conf.MaxIdle, conf.MaxActive, conf.Wait)
if redisClient == nil {
log.Info("[Redis Starting Failed] ")
}
redisStore = redisClient
// log.Info(redisStore)
return
}
func GetRedis() *cache.RedisStore {
// log.Info(redisClient)
return redisStore
}
方法:
package cache
import (
"fmt"
"github.com/garyburd/redigo/redis"
"time"
log "github.com/wonderivan/logger"
)
/*
*返回给定字段的值。如果给定的字段或 key 不存在时,返回 nil 。
*/
func (c *RedisStore) HGet(key string, field string) (interface{}, error) {
conn := c.pool.Get()
defer conn.Close()
hkey := c.GetKey(key, REDIS_EXPIRE_HASH)
v, err := conn.Do("HGET", hkey, field)
// log.Info(hkey)
// log.Info(field)
// log.Info(v)
if err != nil {
fmt.Println("HGET:", err)
}
conn.Close()
return v, err
}
/*
Hmget
*/
func (c *RedisStore) HMGet(key string, fields []string) ([]interface{}, error) {
conn := c.pool.Get()
defer conn.Close()
hkey := c.GetKey(key, REDIS_EXPIRE_HASH)
var values []interface{}
for _, k := range fields {
value, err := conn.Do("HGET", hkey, k)
values = append(values, value)
if err != nil {
fmt.Println("HGET:", err)
}
}
conn.Close()
return values, nil
}
func (c *RedisStore) HGetAll(key string) (interface{}, error) {
conn := c.pool.Get()
defer conn.Close()
rerr := conn.Err()
if rerr != nil {
return nil, rerr
}
hkey := c.GetKey(key, REDIS_EXPIRE_HASH)
//fmt.Println(hkey)
v, err := conn.Do("HGETALL", hkey)
if err != nil {
fmt.Println("HGETALL:", err)
}
conn.Close()
return v, err
}
func (c *RedisStore) HGetAllNoNS(key string) (interface{}, error) {
conn := c.pool.Get()
defer conn.Close()
// log.Info(key)
v, err := conn.Do("HGETALL", key)
if err != nil {
fmt.Println("HGETALL:", err)
}
return v, err
}
// 交集
func Slice_Intersect(slice1, slice2 []string) []string { // 取两个切片的交集
m := make(map[string]int)
n := make([]string, 0)
for _, v := range slice1 {
m[v]++
}
for _, v := range slice2 {
times, _ := m[v]
if times == 1 {
n = append(n, v)
}
}
return n
}
//Redis HVals 命令用于返回哈希表中,所有的字段的值。
func (c *RedisStore) HVals(key string) (interface{}, error) {
conn := c.pool.Get()
defer conn.Close()
hkey := c.GetKey(key, REDIS_EXPIRE_HASH)
v, err := conn.Do("HVALS", hkey)
if err != nil {
fmt.Println("HVALS:", err)
}
return v, err
}
func (c *RedisStore) HInter(key1 string, key2 string) ([]string,int) {
conn := c.pool.Get()
defer conn.Close()
log.Info(key1)
log.Info(key2)
arg1,error1 := redis.Values(c.HGetAllNoNS(key1))
arg2,_error2:= redis.Values(c.HGetAllNoNS(key2))
if error1 !=nil || _error2 !=nil{
log.Info("HInter func error#v:", error1,_error2)
}
var err1,err2 error
cacheKeys1, _ := redis.Strings(arg1, err1)
cacheKeys2, _ := redis.Strings(arg2, err2)
if err1 !=nil || err2 !=nil{
log.Info("HInter func error#v:" ,err1,err2)
}
var sss1 []string
var sss2 []string
// log.Info(len(cacheKeys1))
for k:=0;k<len(cacheKeys1);k+=2{
ss1:=cacheKeys1[k]
// log.Info(ss1)
sss1=append(sss1,string(ss1))
}
for k:=0;k<len(cacheKeys2);k+=2{
ss2:=cacheKeys2[k]
sss2=append(sss2,string(ss2))
}
Intrdevice:=Slice_Intersect(sss1,sss2)
return Intrdevice,len(sss2)
}
func (c *RedisStore) HInter11(key1 string, key2 string) ([]string,int) {
conn := c.pool.Get()
defer conn.Close()
log.Info(key1)
log.Info(key2)
arg1,error1 := redis.Values(c.HGetAllNoNS(key1))
arg2,_error2:= redis.Values(c.SMembers11(key2))
if error1 !=nil || _error2 !=nil{
log.Info("HInter func error#v:", error1,_error2)
}
var err1,err2 error
cacheKeys1, _ := redis.Strings(arg1, err1)
cacheKeys2, _ := redis.Strings(arg2, err2)
log.Info(len(cacheKeys1)/2)
log.Info(len(cacheKeys2))
if err1 !=nil || err2 !=nil{
log.Info("HInter func error#v:" ,err1,err2)
}
var sss1 []string
var sss2 []string
// log.Info(len(cacheKeys1))
for k:=0;k<len(cacheKeys1);k+=2{
ss1:=cacheKeys1[k]
// log.Info(ss1)
sss1=append(sss1,string(ss1))
}
// for k:=0;k<len(cacheKeys2);k+=2{
// ss2:=cacheKeys2[k]
// sss2=append(sss2,string(ss2))
// }
Intrdevice:=Slice_Intersect(sss1,cacheKeys2)
return Intrdevice,len(sss2)
}
//Redis HValsStr 命令用于返回哈希表中,所有的字段的值, 值类型为string。
func (c *RedisStore) HValsStr(key string) (interface{}, error) {
v, err := c.HVals(key)
if err != nil {
fmt.Println("HValsStr:", err)
}
var ret []interface{}
for _, it := range v.([]interface{}) {
ret = append(ret, uint8ToStr(it.([]uint8)))
}
return ret, err
}
/*
Redis Hmset 命令用于同时将多个 field-value (字段-值)对设置到哈希表中。
此命令会覆盖哈希表中已存在的字段。
如果哈希表不存在,会创建一个空哈希表,并执行 HMSET 操作。
*/
func (c *RedisStore) HMSet(key string, values map[string]string) error {
conn := c.pool.Get()
defer conn.Close()
rerr := conn.Err()
if rerr != nil {
return rerr
}
hkey := c.GetKey(key, REDIS_EXPIRE_HASH)
for k, v := range values {
_, err := conn.Do("HSET", hkey, k, v)
if err != nil {
fmt.Println("HMSET:", err)
}
}
conn.Close()
return nil
}
func (c *RedisStore) HSet(key string, field string, value string) (interface{}, error) {
conn := c.pool.Get()
defer conn.Close()
rerr := conn.Err()
if rerr != nil {
return nil, rerr
}
hkey := c.GetKey(key, REDIS_EXPIRE_HASH)
v, err := conn.Do("HSET", hkey, field, value)
// fmt.Println("HSET:", field)
conn.Close()
if err != nil {
fmt.Println("HSET:", err)
}
return v, err
}
// Hash迭代
func (c *RedisStore) HScan(key string, cursor string, count string) (interface{}, error) {
conn := c.pool.Get()
defer conn.Close()
hkey := c.GetKey(key, REDIS_EXPIRE_HASH)
v, err := conn.Do("HSCAN", hkey, cursor, "COUNT", count)
// fmt.Println("SCAN:err:", hkey,cursor,count)
if err != nil {
fmt.Println("SSCAN:err:", err)
}
return v, err
}
//Redis Hlen 命令用于获取哈希表中字段的数量。
func (c *RedisStore) HLen(key string) (interface{}, error) {
conn := c.pool.Get()
defer conn.Close()
hkey := c.GetKey(key, REDIS_EXPIRE_HASH)
v, err := conn.Do("HLEN", hkey)
// fmt.Println("HLEN:", hkey)
if err != nil {
fmt.Println("HLEN:err:", err)
}
// fmt.Println("HLEN:", v)
return v, err
}
//设置hash key 过期时间
func (c *RedisStore) HExpire(key string, expires time.Duration) error {
err := c._Expire(key, REDIS_EXPIRE_HASH, expires)
return err
}
//删除hash key
func (c *RedisStore) HDelete(key string) error {
err := c._Delete(key, REDIS_EXPIRE_HASH)
return err
}
//用于删除哈希表 key 中的一个或多个指定字段,不存在的字段将被忽略。被成功删除字段的数量,不包括被忽略的字段。
func (c *RedisStore) HDel(key string, field []string) error {
conn := c.pool.Get()
defer conn.Close()
hkey := c.GetKey(key, REDIS_EXPIRE_HASH)
// fmt.Println("HDEL:", key)
for _, v := range field {
re, err := conn.Do("HDEL", hkey, v)
// fmt.Println("HDEL:", hkey)
fmt.Println("删除新增重复设备:",re)
if err != nil {
fmt.Println("HDEL:", err)
}
}
return nil
}
func (c *RedisStore) HDelfield(key string, field string) error {
conn := c.pool.Get()
defer conn.Close()
hkey := c.GetKey(key, REDIS_EXPIRE_HASH)
// fmt.Println("HDEL:", key)
re, err := conn.Do("HDEL", hkey, field)
fmt.Println("HDEL:", field)
fmt.Println("删除已存在field:",re)
if err != nil {
fmt.Println("HDEL:", err)
}
return nil
}
//Redis HIncrby增加field的增量
func (c *RedisStore) HIncrby(key string, field string, increment int) (interface{}, error) {
conn := c.pool.Get()
defer conn.Close()
hkey := c.GetKey(key, REDIS_EXPIRE_HASH)
v, err := conn.Do("HINCRBY", hkey, field, increment)
if err != nil {
fmt.Println("HINCRBY:", err)
}
// fmt.Println("HINCRBY:", hkey)
// fmt.Println("HINCRBY:", field)
return v, err
}
更多推荐
已为社区贡献1条内容
所有评论(0)