123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- package redis
- import (
- "context"
- "encoding/json"
- "errors"
- "fmt"
- "github.com/go-redis/redis/v8"
- "strconv"
- "time"
- )
- type StandaloneRedisClient struct {
- redisClient *redis.Client
- }
- func InitStandaloneRedis(config string) (standaloneRedis *StandaloneRedisClient, err error) {
- var cf map[string]string
- err = json.Unmarshal([]byte(config), &cf)
- if err != nil {
- return
- }
- if _, ok := cf["conn"]; !ok {
- err = errors.New("config has no conn key")
- return
- }
- dbNum := 0
- if _, ok := cf["dbNum"]; ok {
- dbNum, err = strconv.Atoi(cf["dbNum"])
- if err != nil {
- return
- }
- }
- if _, ok := cf["password"]; !ok {
- cf["password"] = ""
- }
- client := redis.NewClient(&redis.Options{
- Addr: cf["conn"],
- Password: cf["password"],
- DB: dbNum,
- })
- _, err = client.Ping(context.TODO()).Result()
- if err != nil {
- err = errors.New("redis 链接失败:" + err.Error())
- return
- }
- standaloneRedis = &StandaloneRedisClient{redisClient: client}
- return
- }
- func (rc *StandaloneRedisClient) Get(key string) interface{} {
- data, err := rc.redisClient.Get(context.TODO(), key).Bytes()
- if err != nil {
- return nil
- }
- return data
- }
- func (rc *StandaloneRedisClient) RedisBytes(key string) (data []byte, err error) {
- data, err = rc.redisClient.Get(context.TODO(), key).Bytes()
- return
- }
- func (rc *StandaloneRedisClient) RedisString(key string) (data string, err error) {
- data, err = rc.redisClient.Get(context.TODO(), key).Result()
- return
- }
- func (rc *StandaloneRedisClient) RedisInt(key string) (data int, err error) {
- data, err = rc.redisClient.Get(context.TODO(), key).Int()
- return
- }
- func (rc *StandaloneRedisClient) Put(key string, val interface{}, timeout time.Duration) error {
- var err error
- err = rc.redisClient.SetEX(context.TODO(), key, val, timeout).Err()
- if err != nil {
- return err
- }
- err = rc.redisClient.HSet(context.TODO(), DefaultKey, key, true).Err()
- return err
- }
- func (rc *StandaloneRedisClient) SetNX(key string, val interface{}, timeout time.Duration) bool {
- result, err := rc.redisClient.SetNX(context.TODO(), key, val, timeout).Result()
- if err != nil {
- return false
- }
- return result
- }
- func (rc *StandaloneRedisClient) Delete(key string) error {
- var err error
- err = rc.redisClient.Del(context.TODO(), key).Err()
- if err != nil {
- return err
- }
- err = rc.redisClient.HDel(context.TODO(), DefaultKey, key).Err()
- return err
- }
- func (rc *StandaloneRedisClient) IsExist(key string) bool {
- result, err := rc.redisClient.Exists(context.TODO(), key).Result()
- if err != nil {
- return false
- }
- if result == 0 {
- _ = rc.redisClient.HDel(context.TODO(), DefaultKey, key).Err()
- return false
- }
- return true
- }
- func (rc *StandaloneRedisClient) LPush(key string, val interface{}) error {
- data, _ := json.Marshal(val)
- err := rc.redisClient.LPush(context.TODO(), key, data).Err()
- return err
- }
- func (rc *StandaloneRedisClient) Brpop(key string, callback func([]byte)) {
- values, err := rc.redisClient.BRPop(context.TODO(), 1*time.Second, key).Result()
- if err != nil {
- return
- }
- if len(values) < 2 {
- fmt.Println("assert is wrong")
- return
- }
- callback([]byte(values[1]))
- }
- func (rc *StandaloneRedisClient) GetRedisTTL(key string) time.Duration {
- value, err := rc.redisClient.TTL(context.TODO(), key).Result()
- if err != nil {
- return 0
- }
- return value
- }
- func (rc *StandaloneRedisClient) Incrby(key string, num int) (interface{}, error) {
- return rc.redisClient.IncrBy(context.TODO(), key, int64(num)).Result()
- }
- func (rc *StandaloneRedisClient) Do(commandName string, args ...interface{}) (reply interface{}, err error) {
- newArgs := []interface{}{commandName}
- newArgs = append(newArgs, args...)
- return rc.redisClient.Do(context.TODO(), newArgs...).Result()
- }
|