redis

package
v1.0.32 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2023 License: MPL-2.0 Imports: 10 Imported by: 1

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 []*CacheCommand) (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) BfAdd

func (r *RedisClient) BfAdd(key string, item string) (exists bool, err error)

BfAdd - Add (or create and add) a new value to the filter args: key - the name of the filter item - the item to add

func (*RedisClient) BfAddMulti

func (r *RedisClient) BfAddMulti(key string, items []interface{}) ([]int64, error)

BfAddMulti - Adds one or more items to the Bloom Filter, creating the filter if it does not yet exist. args: key - the name of the filter item - One or more items to add

func (*RedisClient) BfExists

func (r *RedisClient) BfExists(key string, item string) (exists bool, err error)

BfExists - Determines whether an item may exist in the Bloom Filter or not. args: key - the name of the filter item - the item to check for

func (*RedisClient) BfExistsMulti

func (r *RedisClient) BfExistsMulti(key string, items []interface{}) ([]int64, error)

BfExistsMulti - Determines if one or more items may exist in the filter or not. args: key - the name of the filter item - one or more items to check

func (*RedisClient) BfReserve

func (r *RedisClient) BfReserve(key string, errorRate float64, capacity uint64) (err error)

BfReserve - Creates an empty Bloom Filter with a given desired error ratio and initial capacity. args: key - the name of the filter error_rate - the desired probability for false positives capacity - the number of entries you intend to add to the filter

func (*RedisClient) DecrBy

func (r *RedisClient) DecrBy(key string, number int) error

func (*RedisClient) Del

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

Del 删除某个key

func (*RedisClient) Dels

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

Dels 删除多个key

func (*RedisClient) Eval

func (r *RedisClient) Eval(scriptContent string, keys []interface{}, args []interface{}) (interface{}, error)

func (*RedisClient) Exists

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

Exists 检查某个key是否存在

func (*RedisClient) Expire

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

Expire 使某个key过期

func (*RedisClient) Get

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

Get 获取某个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)

HDel 删除某个field

func (*RedisClient) HDels

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

HDels 删除多个field

func (*RedisClient) HGet

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

HGet 获取某个field的值

func (*RedisClient) HGetAll

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

HGetAll 获取所有fields的值

func (*RedisClient) HGetFloat64

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

HGetFloat64 获取某个field的float64值

func (*RedisClient) HGetInt

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

HGetInt 获取某个field的int值

func (*RedisClient) HGetInt64

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

HGetInt64 获取某个field的int64值

func (*RedisClient) HGetString

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

HGetString 获取某个field的float64值

func (*RedisClient) HLen

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

HLen 设置某个field的值

func (*RedisClient) HMGet

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

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

func (*RedisClient) HMSet

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

HMSet 一次设置多个field的值

func (*RedisClient) HSet

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

HSet 设置某个field的值

func (*RedisClient) Incr

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

Incr 将某个key中的值加1

func (*RedisClient) IncrBy

func (r *RedisClient) IncrBy(key string, number int) error

func (*RedisClient) Ping

func (r *RedisClient) Ping() error

Ping 检查redis是否存活

func (*RedisClient) Pipeline

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

Pipeline 批量提交命令

func (*RedisClient) RPop

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

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

func (*RedisClient) SAdd

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

SAdd 集合中添加一个成员

func (*RedisClient) SDiff

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

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

func (*RedisClient) SInter

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

SInter 取不同keys中集合的交集

func (*RedisClient) SIsMember

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

SIsMember 检查中成员是否出现在key中

func (*RedisClient) SMembers

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

SMembers 取集合中的成员

func (*RedisClient) SRem

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

SRem 集合中删除一个成员

func (*RedisClient) SUnion

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

SUnion 取不同keys中集合的并集

func (*RedisClient) Set

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

Set 设置某个key为value

func (*RedisClient) SetEx

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

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

func (*RedisClient) Shutdown

func (r *RedisClient) Shutdown()

Shutdown 关闭redis client

func (*RedisClient) Ttl added in v1.0.4

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

Ttl 获取某个key的过期时间

func (*RedisClient) ZAdd

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

ZAdd add a member

func (*RedisClient) ZCard

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

ZCard get members total

func (*RedisClient) ZIncrBy added in v1.0.4

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

ZIncrBy add increment to member's score

func (*RedisClient) ZInterstore

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

ZInterstore get intersect of set

func (*RedisClient) ZRange

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

ZRange get members

func (*RedisClient) ZRangeByScore

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

ZRangeByScore get members by score

func (*RedisClient) ZRem added in v1.0.5

func (r *RedisClient) ZRem(destKey string, members ...interface{}) (int64, error)

ZRem delete members

func (*RedisClient) ZRemRangeByScore

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

ZRemRangeByScore delete members by score

func (*RedisClient) ZScore

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

ZScore get score of member

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 {
	cache.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