2023-06-20 11:09:49 +00:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2023-06-21 09:04:18 +00:00
|
|
|
"errors"
|
2023-06-20 11:09:49 +00:00
|
|
|
"fmt"
|
2023-06-21 09:04:18 +00:00
|
|
|
"math/rand"
|
2023-06-20 11:09:49 +00:00
|
|
|
"one-api/common"
|
2023-06-21 09:26:26 +00:00
|
|
|
"strconv"
|
2023-06-21 09:04:18 +00:00
|
|
|
"strings"
|
2023-06-20 11:09:49 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2023-06-21 09:26:26 +00:00
|
|
|
TokenCacheSeconds = 60 * 60
|
|
|
|
UserId2GroupCacheSeconds = 60 * 60
|
|
|
|
UserId2QuotaCacheSeconds = 10 * 60
|
|
|
|
UserId2StatusCacheSeconds = 60 * 60
|
2023-06-20 11:09:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func CacheGetTokenByKey(key string) (*Token, error) {
|
|
|
|
var token Token
|
|
|
|
if !common.RedisEnabled {
|
2023-06-20 12:09:17 +00:00
|
|
|
err := DB.Where("`key` = ?", key).First(&token).Error
|
2023-06-20 11:09:49 +00:00
|
|
|
return &token, err
|
|
|
|
}
|
|
|
|
tokenObjectString, err := common.RedisGet(fmt.Sprintf("token:%s", key))
|
|
|
|
if err != nil {
|
2023-06-20 12:09:17 +00:00
|
|
|
err := DB.Where("`key` = ?", key).First(&token).Error
|
2023-06-20 11:09:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
jsonBytes, err := json.Marshal(token)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = common.RedisSet(fmt.Sprintf("token:%s", key), string(jsonBytes), TokenCacheSeconds*time.Second)
|
|
|
|
if err != nil {
|
|
|
|
common.SysError("Redis set token error: " + err.Error())
|
|
|
|
}
|
2023-06-20 12:56:35 +00:00
|
|
|
return &token, nil
|
2023-06-20 11:09:49 +00:00
|
|
|
}
|
|
|
|
err = json.Unmarshal([]byte(tokenObjectString), &token)
|
|
|
|
return &token, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func CacheGetUserGroup(id int) (group string, err error) {
|
|
|
|
if !common.RedisEnabled {
|
|
|
|
return GetUserGroup(id)
|
|
|
|
}
|
|
|
|
group, err = common.RedisGet(fmt.Sprintf("user_group:%d", id))
|
|
|
|
if err != nil {
|
|
|
|
group, err = GetUserGroup(id)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
err = common.RedisSet(fmt.Sprintf("user_group:%d", id), group, UserId2GroupCacheSeconds*time.Second)
|
|
|
|
if err != nil {
|
|
|
|
common.SysError("Redis set user group error: " + err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return group, err
|
|
|
|
}
|
|
|
|
|
2023-06-21 09:26:26 +00:00
|
|
|
func CacheGetUserQuota(id int) (quota int, err error) {
|
|
|
|
if !common.RedisEnabled {
|
|
|
|
return GetUserQuota(id)
|
|
|
|
}
|
|
|
|
quotaString, err := common.RedisGet(fmt.Sprintf("user_quota:%d", id))
|
|
|
|
if err != nil {
|
|
|
|
quota, err = GetUserQuota(id)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
err = common.RedisSet(fmt.Sprintf("user_quota:%d", id), fmt.Sprintf("%d", quota), UserId2QuotaCacheSeconds*time.Second)
|
|
|
|
if err != nil {
|
|
|
|
common.SysError("Redis set user quota error: " + err.Error())
|
|
|
|
}
|
|
|
|
return quota, err
|
|
|
|
}
|
|
|
|
quota, err = strconv.Atoi(quotaString)
|
|
|
|
return quota, err
|
|
|
|
}
|
|
|
|
|
2023-06-27 11:22:58 +00:00
|
|
|
func CacheUpdateUserQuota(id int) error {
|
|
|
|
if !common.RedisEnabled {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
quota, err := GetUserQuota(id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = common.RedisSet(fmt.Sprintf("user_quota:%d", id), fmt.Sprintf("%d", quota), UserId2QuotaCacheSeconds*time.Second)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-06-21 09:26:26 +00:00
|
|
|
func CacheIsUserEnabled(userId int) bool {
|
|
|
|
if !common.RedisEnabled {
|
|
|
|
return IsUserEnabled(userId)
|
|
|
|
}
|
|
|
|
enabled, err := common.RedisGet(fmt.Sprintf("user_enabled:%d", userId))
|
|
|
|
if err != nil {
|
|
|
|
status := common.UserStatusDisabled
|
|
|
|
if IsUserEnabled(userId) {
|
|
|
|
status = common.UserStatusEnabled
|
|
|
|
}
|
|
|
|
enabled = fmt.Sprintf("%d", status)
|
|
|
|
err = common.RedisSet(fmt.Sprintf("user_enabled:%d", userId), enabled, UserId2StatusCacheSeconds*time.Second)
|
|
|
|
if err != nil {
|
|
|
|
common.SysError("Redis set user enabled error: " + err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return enabled == "1"
|
|
|
|
}
|
|
|
|
|
2023-06-20 11:09:49 +00:00
|
|
|
var group2model2channels map[string]map[string][]*Channel
|
2023-06-21 09:04:18 +00:00
|
|
|
var channelSyncLock sync.RWMutex
|
2023-06-20 11:09:49 +00:00
|
|
|
|
|
|
|
func InitChannelCache() {
|
2023-06-21 09:04:18 +00:00
|
|
|
newChannelId2channel := make(map[int]*Channel)
|
2023-06-20 11:09:49 +00:00
|
|
|
var channels []*Channel
|
2023-06-25 15:14:15 +00:00
|
|
|
DB.Where("status = ?", common.ChannelStatusEnabled).Find(&channels)
|
2023-06-20 11:09:49 +00:00
|
|
|
for _, channel := range channels {
|
2023-06-21 09:04:18 +00:00
|
|
|
newChannelId2channel[channel.Id] = channel
|
2023-06-20 11:09:49 +00:00
|
|
|
}
|
|
|
|
var abilities []*Ability
|
|
|
|
DB.Find(&abilities)
|
|
|
|
groups := make(map[string]bool)
|
|
|
|
for _, ability := range abilities {
|
|
|
|
groups[ability.Group] = true
|
|
|
|
}
|
2023-06-21 09:04:18 +00:00
|
|
|
newGroup2model2channels := make(map[string]map[string][]*Channel)
|
2023-06-20 11:09:49 +00:00
|
|
|
for group := range groups {
|
2023-06-21 09:04:18 +00:00
|
|
|
newGroup2model2channels[group] = make(map[string][]*Channel)
|
|
|
|
}
|
|
|
|
for _, channel := range channels {
|
|
|
|
groups := strings.Split(channel.Group, ",")
|
|
|
|
for _, group := range groups {
|
|
|
|
models := strings.Split(channel.Models, ",")
|
|
|
|
for _, model := range models {
|
|
|
|
if _, ok := newGroup2model2channels[group][model]; !ok {
|
|
|
|
newGroup2model2channels[group][model] = make([]*Channel, 0)
|
|
|
|
}
|
|
|
|
newGroup2model2channels[group][model] = append(newGroup2model2channels[group][model], channel)
|
|
|
|
}
|
|
|
|
}
|
2023-06-20 11:09:49 +00:00
|
|
|
}
|
2023-06-21 09:04:18 +00:00
|
|
|
channelSyncLock.Lock()
|
|
|
|
group2model2channels = newGroup2model2channels
|
|
|
|
channelSyncLock.Unlock()
|
2023-06-22 02:59:01 +00:00
|
|
|
common.SysLog("channels synced from database")
|
2023-06-20 11:09:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func SyncChannelCache(frequency int) {
|
|
|
|
for {
|
|
|
|
time.Sleep(time.Duration(frequency) * time.Second)
|
2023-06-22 02:59:01 +00:00
|
|
|
common.SysLog("syncing channels from database")
|
2023-06-20 11:09:49 +00:00
|
|
|
InitChannelCache()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func CacheGetRandomSatisfiedChannel(group string, model string) (*Channel, error) {
|
|
|
|
if !common.RedisEnabled {
|
|
|
|
return GetRandomSatisfiedChannel(group, model)
|
|
|
|
}
|
2023-06-21 09:04:18 +00:00
|
|
|
channelSyncLock.RLock()
|
|
|
|
defer channelSyncLock.RUnlock()
|
|
|
|
channels := group2model2channels[group][model]
|
|
|
|
if len(channels) == 0 {
|
|
|
|
return nil, errors.New("channel not found")
|
|
|
|
}
|
|
|
|
idx := rand.Intn(len(channels))
|
|
|
|
return channels[idx], nil
|
2023-06-20 11:09:49 +00:00
|
|
|
}
|