Redis 罕用的数据类型:字符串 哈希 链表 汇合 有序汇合 bitmap hyperlog geo
应用 Lua 脚本实现多个命令原子性操作 分布式锁
package redis
import (
"fmt"
"github.com/go-redis/redis"
"time"
)
type clientRedis struct {*redis.Client}
/*
获取一个 *redis.Client
*/
func NewRds(addr, password string, DB, poolSize int) (client *clientRedis, err error) {
client = &clientRedis{redis.NewClient(&redis.Options{
Addr: addr,
Password: password,
DB: DB,
PoolSize: poolSize,
})}
pong, err := client.Ping().Result()
if err != nil {fmt.Println(err)
return
}
fmt.Println(pong)
return
}
/*
string set 操作 对照命令 set key value
*/
func (cr *clientRedis) StringSet(expire time.Duration, key string, value string) (err error) {err = cr.Client.Append(key, value).Err()
if err != nil {
// todo error info
return
}
cr.Expire(key, expire)
return
}
/*
string get 操作 对照命令 get key
*/
func (cr *clientRedis) StringGet(key string) (res string, err error) {res, err = cr.Get(key).Result()
if err != nil {
//todo error info
return
}
return
}
/*
list lpush 操作 对照命令 lpush key value
*/
func (cr *clientRedis) ListLPush(expire time.Duration, key string, value ...string) (err error) {err = cr.LPush(key, value).Err()
if err != nil {
// todo error info
return
}
cr.Expire(key, expire)
return
}
/*
list rpush 操作 对照命令 rpush key value
*/
func (cr *clientRedis) ListRPush(expire time.Duration, key string, value ...string) (err error) {err = cr.RPush(key, value).Err()
if err != nil {
// todo error info
return
}
cr.Expire(key, expire)
return
}
/*
list lpop 操作 对照命令 lpop key
*/
func (cr *clientRedis) ListLPop(key string) (res string, err error) {res, err = cr.LPop(key).Result()
if err != nil {
//todo error info
return
}
return
}
/*
list rpop 操作 对照命令 rpop key
*/
func (cr *clientRedis) ListRPop(key string) (res string, err error) {res, err = cr.RPop(key).Result()
if err != nil {
//todo error info
return
}
return
}
/*
set add 操作 对照命令 sadd key value
*/
func (cr *clientRedis) SetAdd(expire time.Duration, key string, value ...string) (err error) {err = cr.SAdd(key, value).Err()
if err != nil {
// todo error info
return
}
cr.Expire(key, expire)
return
}
/*
set members 操作 对照命令 smembers key
*/
func (cr *clientRedis) SetMembers(key string) (res []string, err error) {res, err = cr.SMembers(key).Result()
if err != nil {
//todo error info
return
}
return
}