redis

package
v0.0.0-...-76471f1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 19, 2021 License: MIT Imports: 9 Imported by: 0

README

Redis配置

  • default: 缺省的Redis客户端
  • items: 额外的Redis客户端列表
[sdk.redis]
   [sdk.redis.default]
       host = "127.0.0.1"   <--- redis的服务器
       port = 6379          <--- redis的服务端口
       password = ""        <--- redis的连接密码
       db = 0               <--- redis的连接db
   [[sdk.redis.items]]
       name = "extra1"      <--- 需要通过指定name来区分使用的redis连接
       host = "127.0.0.1"
       port = 6379
       password = ""
       db = 0
   [[sdk.redis.items]]
       name = "extra2"
       host = "127.0.0.1"
       port = 6379
       password = ""
       db = 0

在配置其他Redis连接的时候需要定义在[[sdk.redis.items]]中,同时必须指定name

Redis使用指南

获取初始化的Redis客户端

  • 获取缺省Redis客户端: sdk.Redis.My()
  • 获取指定名字的Redis客户端: sdk.Redis.By(string)

支持的Redis接口

常规接口
  • 删除某个key

    func Del(keys []string) error

  • 删除多个key

    func Dels(keys []string) error

  • 检查某个key是否存在

    func Exists(key string) (bool, error)

  • 让某个key过期

    func Expire(key string, expire int) error

  • 在某个key上加1

    func Incr(key string) error

  • 批量发送命令到Redis并一次性执行

    func Pipeline(commands []*RedisCommand) (reply interface{}, err error)

  • 检查redis是否存活

    func Ping() error

string类型
  • 设置指定key的值为value

    func Set(key string, value interface{}) error

  • 设置指定key的值为value,并同时设置时间为expire(单位为秒)

    func SetEx(key string, value interface{}, expire int) error

  • 获取指定key的值

    func Get(key string) ([]byte, error)

  • 获取指定key的int值, 如果该key的值不为int或者不能转换成int会返回错误

    func GetInt(key string) (int, error)

  • 获取指定key的int64值,如果该key的值不为int或者不能转换成int会返回错误

    func GetInt64(key string) (int64, error)

  • 获取指定key的float64值,如果该key的值不为int64或者不能转换成int64会返回错误

    func GetFloat64(key string) (float64, error)

  • 获取指定key的string值,如果该key的值不为string或者不能转换成string会返回错误

    func GetFloat64(key string) (float64, error)

HashMap类型
  • 获取在哈希表中指定key的所有字段和值

    func HGetAll(key string) (map[string]string, error)

  • 获取存储在哈希表中指定字段的值,返回为[]byte

    func HGet(key string, field string) ([]byte, error)

  • 获取存储在哈希表中指定字段的int值,如果该key的值不为int或者不能转换成int会返回错误

    func HGetInt(key string, field string) (int, error)

  • 获取存储在哈希表中指定字段的int64值,如果该key的值不为int64或者不能转换成int64会返回错误

    func HGetInt64(key string, field string) (int64, error)

  • 获取存储在哈希表中指定字段的float64值,如果该key的值不为int64或者不能转换成float64会返回错误

    func HGetFloat64(key string, field string) (float64, error)

  • 获取存储在哈希表中指定字段的string值,如果该key的值不为string或者不能转换成string会返回错误

    func HGetString(key string, field string) (string, error)

  • 获取所有给定字段的值, 返回值为[]byte数组

    func HMGet(key string, fields []string) ([][]byte, error)

  • 将哈希表key中的字段field的值设为value

    func HSet(key string, field interface{}, value interface{}) (int, error)

  • 同时将多个field-value(域-值)对设置到哈希表key中, field-value以map[string]interface{}的形式组装

    func HMSet(key string, args map[string]interface{}) error

  • 删除一个哈希表字段

    func HDel(key string, field interface{}) (int, error)

  • 删除多个哈希表字段

    HDels(key string, fields []interface{}) (int, error)

集合类型
  • 判断 member 元素是否是集合 key 的成员

    func SIsMember(key string, member interface{}) (bool, error)

  • 向集合添加一个或多个成员, members可以为一个slice

    func SAdd(key string, members interface{}) error

  • 移除集合中一个或多个成员

    func SRem(key string, members interface{}) error

  • 返回给定所有集合的交集, 其中每个key对应的存储数据必须为set数据类型

    func SInter(keys []string) ([]string, error)

  • 返回所有给定集合的并集, 其中每个key对应的存储数据必须为set数据类型

    func SUnion(keys []string) ([]string, error)

  • 返回第一个集合与其他集合之间的差异

    func SDiff(keys []string) ([]string, error)

  • 返回集合中的所有成员

    func SMembers(key string) ([]string, error)

zset有序集合
  • 向有序集合添加一个或多个成员,或者更新已存在成员的分数

    func ZAdd(key string, score int64, member interface{}) error

  • 获取有序集合的成员数

    func ZCard(key string) (int, error)

  • 通过索引区间返回有序集合指定区间内的成员

    func ZRange(key string, min, max int64) (map[string]string, error)

  • 通过分数返回有序集合指定区间内的成员

    ZRangeByScore(key string, min, max interface{}) ([]string, error)

  • 移除有序集合中给定的分数区间的所有成员

    func ZRemRangeByScore(key string, min, max interface{}) error

  • 返回有序集中,成员的分数值

    func ZScore(key string, member interface{}) (int64, error)

  • 计算给定的一个或多个有序集的交集并将结果集存储在新的有序集合destination key中

    func ZInterstore(destKey string, keys ...interface{}) (int64, error)

list
  • 移除列表的最后一个元素,返回值为移除的元素

    func RPop(key string) ([]byte, error)

Documentation

Overview

@Title log capability of zerolog @Description zerolog implementation of log capability @Author Ryan Fan 2021-06-09 @Update Ryan Fan 2021-06-09

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewRedisClient

func NewRedisClient(conf *RedisConf) types.CacheClient

Types

type ConfigRedis

type ConfigRedis struct {
	Default *RedisConf   `mapstructure:"default"`
	Items   []*RedisConf `mapstructure:"items"`
}

type RedisClient

type RedisClient struct {
	// contains filtered or unexported fields
}

func (*RedisClient) Del

func (r *RedisClient) Del(key string) error

///////////////////////////////////////////////////////////////////// general purpose ///////////////////////////////////////////////////////////////////// 删除某个key

func (*RedisClient) Dels

func (r *RedisClient) Dels(keys []string) error

删除多个key

func (*RedisClient) Exists

func (r *RedisClient) Exists(key string) (bool, error)

检查某个key是否存在

func (*RedisClient) Expire

func (r *RedisClient) Expire(key string, expire int) error

使某个key过期

func (*RedisClient) Get

func (r *RedisClient) Get(key string) ([]byte, error)

///////////////////////////////////////////////////////////////////////// set ///////////////////////////////////////////////////////////////////////// 获取某个key的值,返回为[]byte

func (*RedisClient) GetFloat64

func (r *RedisClient) GetFloat64(key string) (float64, error)

func (*RedisClient) GetInt

func (r *RedisClient) GetInt(key string) (int, error)

func (*RedisClient) GetInt64

func (r *RedisClient) GetInt64(key string) (int64, error)

func (*RedisClient) GetString

func (r *RedisClient) GetString(key string) (string, error)

func (*RedisClient) HDel

func (r *RedisClient) HDel(key string, field interface{}) (int, error)

//////////////////////////////////////////////////////////////////// hash map operations //////////////////////////////////////////////////////////////////// 删除某个field

func (*RedisClient) HDels

func (r *RedisClient) HDels(key string, fields []interface{}) (int, error)

删除多个field

func (*RedisClient) HGet

func (r *RedisClient) HGet(key string, field string) ([]byte, error)

获取某个field的值

func (*RedisClient) HGetAll

func (r *RedisClient) HGetAll(key string) (map[string]string, error)

获取所有fields的值

func (*RedisClient) HGetFloat64

func (r *RedisClient) HGetFloat64(key string, field string) (float64, error)

获取某个field的float64值

func (*RedisClient) HGetInt

func (r *RedisClient) HGetInt(key string, field string) (int, error)

获取某个field的int值

func (*RedisClient) HGetInt64

func (r *RedisClient) HGetInt64(key string, field string) (int64, error)

获取某个field的int64值

func (*RedisClient) HGetString

func (r *RedisClient) HGetString(key string, field string) (string, error)

获取某个field的float64值

func (*RedisClient) HMGet

func (r *RedisClient) HMGet(key string, fields []string) ([][]byte, error)

一次获取多个field的值,返回为二维[]byte

func (*RedisClient) HMSet

func (r *RedisClient) HMSet(key string, fieldvalues map[string]interface{}) error

一次设置多个field的值

func (*RedisClient) HSet

func (r *RedisClient) HSet(key string, field interface{}, value interface{}) (int, error)

设置某个field的值

func (*RedisClient) Incr

func (r *RedisClient) Incr(key string) error

将某个key中的值加1

func (*RedisClient) Ping

func (r *RedisClient) Ping() error

检查redis是否存活

func (*RedisClient) Pipeline

func (r *RedisClient) Pipeline(commands []*types.CacheCommand) (reply interface{}, err error)

批量提交命令

func (*RedisClient) RPop

func (r *RedisClient) RPop(key string) ([]byte, error)

/////////////////////////////////////////////////////////// list /////////////////////////////////////////////////////////// RPop 移除列表的最后一个元素,返回值为移除的元素

func (*RedisClient) SAdd

func (r *RedisClient) SAdd(key string, members interface{}) error

集合中添加一个成员

func (*RedisClient) SDiff

func (r *RedisClient) SDiff(keys []string) ([]string, error)

比较不同集合中的不同元素

func (*RedisClient) SInter

func (r *RedisClient) SInter(keys []string) ([]string, error)

取不同keys中集合的交集

func (*RedisClient) SIsMember

func (r *RedisClient) SIsMember(key string, member interface{}) (bool, error)

///////////////////////////////////////////////////////////////////////////// set ///////////////////////////////////////////////////////////////////////////// 检查中成员是否出现在key中

func (*RedisClient) SMembers

func (r *RedisClient) SMembers(key string) ([]string, error)

取集合中的成员

func (*RedisClient) SRem

func (r *RedisClient) SRem(key string, members interface{}) error

func (*RedisClient) SUnion

func (r *RedisClient) SUnion(keys []string) ([]string, error)

取不同keys中集合的并集

func (*RedisClient) Set

func (r *RedisClient) Set(key string, value interface{}) error

设置某个key为value

func (*RedisClient) SetEx

func (r *RedisClient) SetEx(key string, value interface{}, expire int) error

设置某个key为value,并设置过期时间(单位为秒)

func (*RedisClient) Shutdown

func (r *RedisClient) Shutdown()

关闭redis client

func (*RedisClient) ZAdd

func (r *RedisClient) ZAdd(key string, score int64, member interface{}) error

func (*RedisClient) ZCard

func (r *RedisClient) ZCard(key string) (int, error)

func (*RedisClient) ZInterstore

func (r *RedisClient) ZInterstore(destKey string, keys ...interface{}) (int64, error)

func (*RedisClient) ZRange

func (r *RedisClient) ZRange(key string, min, max int64) (map[string]string, error)

func (*RedisClient) ZRangeByScore

func (r *RedisClient) ZRangeByScore(key string, min, max interface{}) ([]string, error)

func (*RedisClient) ZRemRangeByScore

func (r *RedisClient) ZRemRangeByScore(key string, min, max interface{}) error

///////////////////////////////////////////////////////////////////////////////////// sorted set /////////////////////////////////////////////////////////////////////////////////////

func (*RedisClient) ZScore

func (r *RedisClient) ZScore(key string, member interface{}) (int64, error)

type RedisConf

type RedisConf struct {
	Name     string `mapstructure:"name"`
	Host     string `mapstructure:"host"`
	Port     int    `mapstructure:"port"`
	Db       int    `mapstructure:"db"`
	Password string `mapstructure:"password"`
}

type RedisProvider

type RedisProvider struct {
	provider.BaseCacheProvider
	Log types.LogProvider
}

func (*RedisProvider) Init

func (rp *RedisProvider) Init(rootConfiger types.Configer, logger types.LogProvider, args ...interface{}) error

@desc implements types.Provider interface, used to initialize the capability @author Ryan Fan (2021-06-09) @param baseconf.Configer root config interface to extract config info @return error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL