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) Connect() *redis.Client
- 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) (keys []string, err 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) (resultMap map[string]int64, err 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) 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 ¶
type RedisClient struct { Addr string // 例如: xxxxx.a2fdoa.0001.usw2.cache.amazonaws.com:6379 Password string DB int // 例如:0 }
func (*RedisClient) Connect ¶
func (r *RedisClient) Connect() *redis.Client
func (*RedisClient) Delete ¶
func (r *RedisClient) Delete(keys ...string) (int64, error)
Delete 返回删除的key的数量
func (*RedisClient) Exists ¶
func (r *RedisClient) Exists(key ...string) (int64, error)
Exists 检查某一个或多个key是否存在,如不存在,返回的数字是0
func (*RedisClient) Get ¶
func (r *RedisClient) Get(key string) ([]byte, error)
Get 返回[]byte,如果不存在,则error为 redis: nil
func (*RedisClient) GetSet ¶
func (r *RedisClient) GetSet(key string, value interface{}) ([]byte, error)
GetSet 设置新值并获取原来的值,不改变duration。注意,如果没有原来的值,则duration会是永久的 获取:GetSet(原子性),设置新值,返回旧值。比如一个按小时计算的计数器,可以用GetSet获取计数并重置为0。
func (*RedisClient) HMSet ¶
func (r *RedisClient) HMSet(key string, fields map[string]interface{}) (bool, error)
func (*RedisClient) Incr ¶
func (r *RedisClient) Incr(key string) (int64, error)
如果key不存在,将key设为"1";如果key存在,key增加1
func (*RedisClient) Keys ¶
func (r *RedisClient) Keys(pattern string) (keys []string, err error)
Keys 用于查找所有符合给定模式 pattern 的 key
func (*RedisClient) LikeDeletes ¶
func (r *RedisClient) LikeDeletes(key string) (int64, error)
func (*RedisClient) MExpire ¶
func (r *RedisClient) MExpire(keys []string, duration time.Duration) error
自定义的方法,通过pipeline实现批量给key设置过期时间 https://www.jianshu.com/p/4045a3721b3c http://vearne.cc/archives/1113
func (*RedisClient) MGet ¶
func (r *RedisClient) MGet(keys ...string) ([]interface{}, error)
func (*RedisClient) MPFCount ¶
func (r *RedisClient) MPFCount(keys []string) (resultMap map[string]int64, err error)
自定义的方法,通过pipeline实现批量PFCount 返回值{"key1": 4, "key2": 18, ...}
func (*RedisClient) MSet ¶
func (r *RedisClient) MSet(pairs ...interface{}) (string, error)
func (*RedisClient) MSetNX ¶
func (r *RedisClient) MSetNX(pairs ...interface{}) (bool, error)
func (*RedisClient) PFAdd ¶
func (r *RedisClient) PFAdd(key string, els ...interface{}) (int64, error)
HyperLogLog http://remcarpediem.net/2019/06/16/用户日活月活怎么统计-Redis-HyperLogLog-详解/
func (*RedisClient) PFCount ¶
func (r *RedisClient) PFCount(keys ...string) (int64, error)
会将每个key的value merge去重后统计数量
func (*RedisClient) RPush ¶
func (r *RedisClient) RPush(key string, values ...interface{}) (int64, error)
一次放入多个value进一个list的尾部
func (*RedisClient) RPushX ¶
func (r *RedisClient) RPushX(key string, value interface{}) (int64, error)
一次放入1个value的尾部
func (*RedisClient) Set ¶
func (r *RedisClient) Set(key string, value interface{}, duration time.Duration) error
Set 设置key value,如果duration为0,则意味着无有效期,永远存在
func (*RedisClient) TTL ¶
func (r *RedisClient) TTL(key string) (time.Duration, error)
TTL 返回 以毫秒为单位的整数值TTL或负值 -1ns, 如果key没有到期超时(没有设置有效期)。-2ns, 如果键不存在(或者键已经过期了)。
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) 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)
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验证码