feat: 新增双因素校验(OTP)

This commit is contained in:
meilin.huang
2023-06-17 15:15:03 +08:00
parent b42a98aff5
commit ef4e34c584
23 changed files with 462 additions and 111 deletions

View File

@@ -1,15 +1,25 @@
package cache
import "mayfly-go/pkg/rediscli"
import (
"mayfly-go/pkg/global"
"mayfly-go/pkg/rediscli"
"strconv"
"time"
)
var strCache map[string]string
var tm *TimedCache
// 如果系统有设置redis信息则从redis获取否则本机内存获取
func GetStr(key string) string {
if rediscli.GetCli() == nil {
checkStrCache()
return strCache[key]
if !useRedisCache() {
checkCache()
val, _ := tm.Get(key)
if val == nil {
return ""
}
return val.(string)
}
res, err := rediscli.Get(key)
if err != nil {
return ""
@@ -17,28 +27,45 @@ func GetStr(key string) string {
return res
}
// 如果系统有设置redis信息则使用redis存否则存于本机内存
func SetStr(key, value string) {
if rediscli.GetCli() == nil {
checkStrCache()
strCache[key] = value
func GetInt(key string) int {
val := GetStr(key)
if val == "" {
return 0
}
if intV, err := strconv.Atoi(val); err != nil {
global.Log.Error("获取缓存中的int值转换失败", err)
return 0
} else {
return intV
}
}
// 如果系统有设置redis信息则使用redis存否则存于本机内存。duration == -1则为永久缓存
func SetStr(key, value string, duration time.Duration) {
if !useRedisCache() {
checkCache()
tm.Add(key, value, duration)
return
}
rediscli.Set(key, value, 0)
rediscli.Set(key, value, duration)
}
// 删除指定key
func Del(key string) {
if rediscli.GetCli() == nil {
checkStrCache()
delete(strCache, key)
if !useRedisCache() {
checkCache()
tm.Delete(key)
return
}
rediscli.Del(key)
}
func checkStrCache() {
if strCache == nil {
strCache = make(map[string]string)
func checkCache() {
if tm == nil {
tm = NewTimedCache(time.Minute*time.Duration(5), 30*time.Second)
}
}
func useRedisCache() bool {
return rediscli.GetCli() != nil
}