add jwt / complete the rotation verification

This commit is contained in:
landaiqing
2024-08-12 22:05:59 +08:00
parent 54f6256c00
commit 48c5aeb0f4
31 changed files with 1702 additions and 44 deletions

67
utils/cache.go Normal file
View File

@@ -0,0 +1,67 @@
package utils
import (
"sync"
"time"
)
type cachedata = struct {
data []byte
createAt time.Time
}
var mux sync.Mutex
var cachemaps = make(map[string]*cachedata)
// WriteCache .
func WriteCache(key string, data []byte) {
mux.Lock()
defer mux.Unlock()
cachemaps[key] = &cachedata{
createAt: time.Now(),
data: data,
}
}
// ReadCache .
func ReadCache(key string) []byte {
mux.Lock()
defer mux.Unlock()
if cd, ok := cachemaps[key]; ok {
return cd.data
}
return []byte{}
}
// ClearCache .
func ClearCache(key string) {
mux.Lock()
defer mux.Unlock()
delete(cachemaps, key)
}
// RunTimedTask .
func RunTimedTask() {
ticker := time.NewTicker(time.Minute * 5)
go func() {
for range ticker.C {
checkCacheOvertimeFile()
}
}()
}
func checkCacheOvertimeFile() {
var keys = make([]string, 0)
for key, data := range cachemaps {
ex := time.Now().Unix() - data.createAt.Unix()
if ex > (60 * 30) {
keys = append(keys, key)
}
}
for _, key := range keys {
ClearCache(key)
}
}