// 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 }