报错无法存储验证码,但redis中能看到数据

问题出在这个代码(pkg/captcha/store_redis.go)

if ok := s.RedisClient.Set(s.KeyPrefix+key, value, ExpireTime); !ok {
    return errors.New("无法存储图片验证码答案")
}

调用接口就报这个错误,但是redis中已经存储了值,如果改成不去处理错误,则能获得base64结果,redis中key值也和响应中的id一致。

讨论数量: 2

错误位置是 pkg/captcha/captcha.go 的 internalCaptcha.Base64Captcha = base64Captcha.NewCaptcha(driver, &store) 这行代码。具体错误信息是 &store 类型有误,这应该是 base64Captcha 库版本不同导致的,目前 base64Captcha.Store interface 的 Set 方法是没有返回值的。只需将 pkg/captcha/store_redis.go 的 Set 方法改一下即可。

func (s *RedisStore) Set(key string, value string) {

    ExpireTime := time.Minute * time.Duration(config.GetInt64("captcha.expire_time"))
    // 方便本地开发调试
    if app.IsLocal() {
        ExpireTime = time.Minute * time.Duration(config.GetInt64("captcha.debug_expire_time"))
    }

    s.RedisClient.Set(s.KeyPrefix+key, value, ExpireTime)
}
1年前 评论
Oyxiaoxi 11个月前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!