Documentation
¶
Index ¶
- Constants
- func GetEmailCacheKey(email string) string
- func GetMobileCacheKey(mobile string) string
- func GetUserEmailCacheKey(userID string, email string) string
- type RedisClient
- func (r *RedisClient) Close() error
- func (r *RedisClient) Delete(keys ...string) (int64, error)
- func (r *RedisClient) Exists(key ...string) (int64, error)
- func (r *RedisClient) Expire(key string, duration time.Duration) (bool, error)
- func (r *RedisClient) Get(key string) ([]byte, error)
- func (r *RedisClient) GetSet(key string, value interface{}) ([]byte, error)
- func (r *RedisClient) HMSet(key string, fields map[string]interface{}) (bool, error)
- func (r *RedisClient) Incr(key string) (int64, error)
- func (r *RedisClient) Keys(pattern string) ([]string, error)
- func (r *RedisClient) LikeDeletes(key string) (int64, error)
- func (r *RedisClient) MExpire(keys []string, duration time.Duration) error
- func (r *RedisClient) MGet(keys ...string) ([]interface{}, error)
- func (r *RedisClient) MPFCount(keys []string) (map[string]int64, error)
- func (r *RedisClient) MSet(pairs ...interface{}) (string, error)
- func (r *RedisClient) MSetNX(pairs ...interface{}) (bool, error)
- func (r *RedisClient) PFAdd(key string, els ...interface{}) (int64, error)
- func (r *RedisClient) PFCount(keys ...string) (int64, error)
- func (r *RedisClient) RPush(key string, values ...interface{}) (int64, error)
- func (r *RedisClient) RPushX(key string, value interface{}) (int64, error)
- func (r *RedisClient) Set(key string, value interface{}, duration time.Duration) error
- func (r *RedisClient) SetNX(key string, value interface{}, duration time.Duration) (bool, error)
- func (r *RedisClient) SetXX(key string, value interface{}, duration time.Duration) (bool, error)
- func (r *RedisClient) TTL(key string) (time.Duration, error)
- func (r *RedisClient) ZAdd(key string, members ...Z) (int64, error)
- func (r *RedisClient) ZRange(key string, start, stop int64) ([]string, error)
- func (r *RedisClient) ZRevRange(key string, start, stop int64) ([]string, error)
- type Verification
- func (v *Verification) GetSetEmailVerifyCode(email string) (code string, err error)
- func (v *Verification) GetSetUserEmailVerifyCode(userID, email string) (code string, err error)
- func (v *Verification) SetEmailVerifyCode(email string) (string, error)
- func (v *Verification) SetEmailVerifyCodeUUID(email string) (string, error)
- func (v *Verification) SetMobileVerifyCode(mobile string) (string, error)
- func (v *Verification) SetUserEmailVerifyCode(userID string, email string) (string, error)
- func (v *Verification) VerifyCode(key, code string) (bool, error)
- func (v *Verification) VerifyEmail(email, code string) (bool, error)
- func (v *Verification) VerifyMobile(mobile, code string) (bool, error)
- func (v *Verification) VerifyUserEmail(userID string, email, code string) (bool, error)
- type Z
Constants ¶
const ( MOBILE_VERI_PREFIX = "verify_mobile:" EMAIL_VERI_PREFIX = "verify_email:" )
Variables ¶
This section is empty.
Functions ¶
func GetEmailCacheKey ¶
GetEmailCacheKey 生成email验证码的cache的key,不需要userID
func GetMobileCacheKey ¶
GetMobileCacheKey 生成mobile验证码的cache的key
func GetUserEmailCacheKey ¶
GetUserEmailCacheKey 生成userId+email验证码的cache的key,需要userID
Types ¶
type RedisClient ¶
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) 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) 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.
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验证码