mirror of
https://gitee.com/dromara/mayfly-go
synced 2025-11-04 08:20:25 +08:00
feat: 新增双因素校验(OTP)
This commit is contained in:
61
server/pkg/cache/str_cache.go
vendored
61
server/pkg/cache/str_cache.go
vendored
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user