This commit is contained in:
Sakurasan
2023-08-10 22:54:56 +08:00
parent adbc388920
commit a0155fc0b1
6 changed files with 225 additions and 29 deletions

View File

@@ -1,8 +1,10 @@
package store
import (
"errors"
"log"
"math/rand"
"time"
"github.com/Sakurasan/to"
"github.com/patrickmn/go-cache"
@@ -40,6 +42,24 @@ func FromKeyCacheRandomItemKey() Key {
return item.Object.(Key)
}
func SelectKeyCache(apitype string) (Key, error) {
var keys []Key
items := KeysCache.Items()
for _, item := range items {
if item.Object.(Key).ApiType == apitype {
keys = append(keys, item.Object.(Key))
}
}
if len(keys) == 0 {
return Key{}, errors.New("No key found")
} else if len(keys) == 1 {
return keys[0], nil
}
rand.Seed(time.Now().UnixNano())
idx := rand.Intn(len(keys))
return keys[idx], nil
}
func LoadAuthCache() {
AuthCache = cache.New(cache.NoExpiration, cache.NoExpiration)
users, err := GetAllUsers()