| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- // Package redis
- /**
- * @ File:
- * @ Date: 2021/5/12 10:29
- * @ Author: JYQ
- * @ Description:
- */
- package redis
- import (
- "sportfitness/base/errors"
- "time"
- "github.com/go-redis/redis/v8"
- )
- type CaptchaStore struct {
- }
- func (c CaptchaStore) genKey(id string) string {
- return prefix + ":captcha:" + id
- }
- func (c CaptchaStore) Set(id string, value string) {
- handleErr(getClient().Set(ctx, c.genKey(id), value, time.Minute*2).Err())
- }
- func (c CaptchaStore) Get(id string, clear bool) string {
- cmds, _ := getClient().TxPipelined(ctx, func(pipe redis.Pipeliner) error {
- key := c.genKey(id)
- pipe.Get(ctx, key)
- if clear {
- pipe.Del(ctx, key)
- }
- return nil
- })
- cmd := cmds[0].(*redis.StringCmd)
- r, err := cmd.Result()
- if err == redis.Nil {
- panic(errors.ErrVerifyCode)
- }
- return r
- }
- func (c CaptchaStore) Verify(id, answer string, clear bool) bool {
- v := c.Get(id, clear)
- return v == answer
- }
|