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.

77 lines
1.4 KiB

package redis
import (
"errors"
redisdb "github.com/gomodule/redigo/redis"
)
//取值
func Get(key string) (string, error) {
c := pool.Get()
defer c.Close()
return redisdb.String(c.Do("get", key))
}
/*
* 存值,
* args,范围(key, value)或(key, value,expire)
* 2020/06/06
*/
func Set(args ...interface{}) (interface{}, error) {
c := pool.Get()
defer c.Close()
if len(args) < 2 {
return nil, errors.New("参数错误,至少需两个参数")
}
var err error
var reply interface{}
if len(args) > 2 {
reply, err = c.Do("SET", args[0], args[1], "EX", args[2]) //设置过期时间
} else {
reply, err = c.Do("SET", args[0], args[1])
}
return reply, err
}
/**
* 删除key
*/
func Del(key string) (int,error){
c := pool.Get()
defer c.Close()
var err error
var reply interface{}
reply, err = c.Do("DEL", key)
return redisdb.Int(reply, err)
}
/**
* 设锁
* 2020/01/15
*/
func SetLock(args ...interface{}) (bool,error){
c := pool.Get()
defer c.Close()
if len(args) < 2 {
return false, errors.New("参数错误,至少需两个参数")
}
var err error
var result interface{}
if len(args) > 2 {
result, err = c.Do("SETNX", args[0], args[1], "EX", args[2]) //设置过期时间
} else {
result, err = c.Do("SETNX", args[0], args[1])
}
if err != nil{
return false,err
}
if result.(int) == 1{
return true, nil
} else{
return false, nil
}
}