redis

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MOBILE_VERI_PREFIX = "verify_mobile:"
	EMAIL_VERI_PREFIX  = "verify_email:"
)

Variables

This section is empty.

Functions

func GetEmailCacheKey

func GetEmailCacheKey(email string) string

GetEmailCacheKey 生成email验证码的cache的key,不需要userID

func GetMobileCacheKey

func GetMobileCacheKey(mobile string) string

GetMobileCacheKey 生成mobile验证码的cache的key

func GetUserEmailCacheKey

func GetUserEmailCacheKey(userID string, email string) string

GetUserEmailCacheKey 生成userId+email验证码的cache的key,需要userID

Types

type RedisClient

type RedisClient struct {
	Client *redis.Client
}

RedisClient is a wrapper for the go-redis client. It holds a long-lived client that manages a connection pool.

func NewRedisClient added in v1.2.0

func NewRedisClient(addr string, password string, db int) (*RedisClient, error)

NewRedisClient creates a new RedisClient. It establishes a connection and pings the server to ensure it's alive.

func (*RedisClient) Close added in v1.2.0

func (r *RedisClient) Close() error

Close closes the underlying redis client and release resources.

func (*RedisClient) Delete

func (r *RedisClient) Delete(keys ...string) (int64, error)

Delete deletes one or more keys. Returns the number of keys that were removed.

func (*RedisClient) Exists

func (r *RedisClient) Exists(key ...string) (int64, error)

Exists checks if one or more keys exist.

func (*RedisClient) Expire

func (r *RedisClient) Expire(key string, duration time.Duration) (bool, error)

Expire sets a new expiration for a key.

func (*RedisClient) Get

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

Get retrieves a value by key. Returns redis.Nil error if key does not exist.

func (*RedisClient) GetSet

func (r *RedisClient) GetSet(key string, value interface{}) ([]byte, error)

GetSet sets a new value for a key and returns the old value.

func (*RedisClient) HMSet

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

HMSet sets multiple hash fields to multiple values.

func (*RedisClient) Incr

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

Incr increments the integer value of a key by one.

func (*RedisClient) Keys

func (r *RedisClient) Keys(pattern string) ([]string, error)

Keys finds all keys matching the given pattern. Warning: KEYS can be slow on a large database.

func (*RedisClient) LikeDeletes

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

LikeDeletes deletes keys matching a pattern. Warning: KEYS can be slow in production.

func (*RedisClient) MExpire

func (r *RedisClient) MExpire(keys []string, duration time.Duration) error

MExpire sets an expiration for multiple keys using a pipeline.

func (*RedisClient) MGet

func (r *RedisClient) MGet(keys ...string) ([]interface{}, error)

MGet retrieves multiple values by keys.

func (*RedisClient) MPFCount

func (r *RedisClient) MPFCount(keys []string) (map[string]int64, error)

MPFCount counts the cardinality of multiple HyperLogLogs using a pipeline.

func (*RedisClient) MSet

func (r *RedisClient) MSet(pairs ...interface{}) (string, error)

MSet sets multiple key-value pairs.

func (*RedisClient) MSetNX

func (r *RedisClient) MSetNX(pairs ...interface{}) (bool, error)

MSetNX sets multiple key-value pairs, only if none of the keys exist.

func (*RedisClient) PFAdd

func (r *RedisClient) PFAdd(key string, els ...interface{}) (int64, error)

PFAdd adds elements to a HyperLogLog.

func (*RedisClient) PFCount

func (r *RedisClient) PFCount(keys ...string) (int64, error)

PFCount returns the approximate cardinality of the set observed by the HyperLogLog.

func (*RedisClient) RPush

func (r *RedisClient) RPush(key string, values ...interface{}) (int64, error)

RPush appends one or more values to a list.

func (*RedisClient) RPushX

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

RPushX appends a value to a list, only if the list exists.

func (*RedisClient) Set

func (r *RedisClient) Set(key string, value interface{}, duration time.Duration) error

Set sets a key-value pair. A duration of 0 means no expiration.

func (*RedisClient) SetNX

func (r *RedisClient) SetNX(key string, value interface{}, duration time.Duration) (bool, error)

SetNX sets a key-value pair only if the key does not exist.

func (*RedisClient) SetXX

func (r *RedisClient) SetXX(key string, value interface{}, duration time.Duration) (bool, error)

SetXX sets a key-value pair only if the key already exists.

func (*RedisClient) TTL

func (r *RedisClient) TTL(key string) (time.Duration, error)

TTL returns the remaining time to live of a key.

func (*RedisClient) ZAdd

func (r *RedisClient) ZAdd(key string, members ...Z) (int64, error)

ZAdd adds one or more members to a sorted set.

func (*RedisClient) ZRange

func (r *RedisClient) ZRange(key string, start, stop int64) ([]string, error)

ZRange returns a range of members from a sorted set, by index.

func (*RedisClient) ZRevRange

func (r *RedisClient) ZRevRange(key string, start, stop int64) ([]string, error)

ZRevRange returns a range of members from a sorted set, by index, in reverse order.

type Verification

type Verification struct {
	Redis             *RedisClient
	EmailCodeLength   int           // ie: 6
	EmailCodeTimeout  time.Duration // ie: time.hour * 24
	MobileCodeLength  int           // ie: 6
	MobileCodeTimeout time.Duration // ie: time.minute *30
}

verification struct

func (*Verification) GetSetEmailVerifyCode added in v1.0.9

func (v *Verification) GetSetEmailVerifyCode(email string) (code string, err error)

func (*Verification) GetSetUserEmailVerifyCode added in v1.0.8

func (v *Verification) GetSetUserEmailVerifyCode(userID, email string) (code string, err error)

先尝试获取缓存中保存的Email验证码,如没有,则新设置,效果同SetUserEmailVerifyCode;如有,则取出此code,并重新设置此code的有效期。 这样保证多次设置 UserEmailVerifyCode 获得的code都是相同的

func (*Verification) SetEmailVerifyCode

func (v *Verification) SetEmailVerifyCode(email string) (string, error)

SetEmailVerifyCode 在缓存中保存Email验证码,有效时间在config.ini中设置,不需要userID

func (*Verification) SetEmailVerifyCodeUUID added in v1.1.0

func (v *Verification) SetEmailVerifyCodeUUID(email string) (string, error)

SetEmailVeirifyCodeUUID 在缓存中保存Email验证码,只是code是uuid。

func (*Verification) SetMobileVerifyCode

func (v *Verification) SetMobileVerifyCode(mobile string) (string, error)

SetMobileVerifyCode 在缓存中保存Mobile验证码,有效时间在config.ini中设置

func (*Verification) SetUserEmailVerifyCode

func (v *Verification) SetUserEmailVerifyCode(userID string, email string) (string, error)

SetUserEmailVerifyCode 在缓存中保存Email验证码,有效时间在config.ini中设置,需要userID

func (*Verification) VerifyCode

func (v *Verification) VerifyCode(key, code string) (bool, error)

验证缓存中的验证码(email或者mobile). 验证成功后,会删除该key

func (*Verification) VerifyEmail

func (v *Verification) VerifyEmail(email, code string) (bool, error)

验证email验证码,不含userID

func (*Verification) VerifyMobile

func (v *Verification) VerifyMobile(mobile, code string) (bool, error)

验证手机验证码

func (*Verification) VerifyUserEmail

func (v *Verification) VerifyUserEmail(userID string, email, code string) (bool, error)

验证userID+email验证码

type Z

type Z = redis.Z

Z is a type alias for redis.Z, representing a member in a sorted set.

Jump to

Keyboard shortcuts

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