Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
new-api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
6c579a60
authored
Jun 21, 2023
by
JustSong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: select channel without database (#158)
parent
ecf919b1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
32 additions
and
12 deletions
+32
-12
model/cache.go
+32
-12
No files found.
model/cache.go
View file @
6c579a60
...
...
@@ -2,8 +2,11 @@ package model
import
(
"encoding/json"
"errors"
"fmt"
"math/rand"
"one-api/common"
"strings"
"sync"
"time"
)
...
...
@@ -57,18 +60,15 @@ func CacheGetUserGroup(id int) (group string, err error) {
return
group
,
err
}
var
channelId2channel
map
[
int
]
*
Channel
var
channelSyncLock
sync
.
RWMutex
var
group2model2channels
map
[
string
]
map
[
string
][]
*
Channel
var
channelSyncLock
sync
.
RWMutex
func
InitChannelCache
()
{
channelSyncLock
.
Lock
()
defer
channelSyncLock
.
Unlock
()
channelId2channel
=
make
(
map
[
int
]
*
Channel
)
newChannelId2channel
:=
make
(
map
[
int
]
*
Channel
)
var
channels
[]
*
Channel
DB
.
Find
(
&
channels
)
for
_
,
channel
:=
range
channels
{
c
hannelId2channel
[
channel
.
Id
]
=
channel
newC
hannelId2channel
[
channel
.
Id
]
=
channel
}
var
abilities
[]
*
Ability
DB
.
Find
(
&
abilities
)
...
...
@@ -76,11 +76,26 @@ func InitChannelCache() {
for
_
,
ability
:=
range
abilities
{
groups
[
ability
.
Group
]
=
true
}
group2model2channels
=
make
(
map
[
string
]
map
[
string
][]
*
Channel
)
newGroup2model2channels
:
=
make
(
map
[
string
]
map
[
string
][]
*
Channel
)
for
group
:=
range
groups
{
group2model2channels
[
group
]
=
make
(
map
[
string
][]
*
Channel
)
// TODO: implement this
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
)
}
}
}
channelSyncLock
.
Lock
()
group2model2channels
=
newGroup2model2channels
channelSyncLock
.
Unlock
()
common
.
SysLog
(
"Channels synced from database"
)
}
func
SyncChannelCache
(
frequency
int
)
{
...
...
@@ -95,7 +110,12 @@ func CacheGetRandomSatisfiedChannel(group string, model string) (*Channel, error
if
!
common
.
RedisEnabled
{
return
GetRandomSatisfiedChannel
(
group
,
model
)
}
return
GetRandomSatisfiedChannel
(
group
,
model
)
// TODO: implement this
return
nil
,
nil
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
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment