MCPcopy
hub / github.com/songquanpeng/one-api / InitChannelCache

Function InitChannelCache

model/cache.go:173–217  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

171var channelSyncLock sync.RWMutex
172
173func InitChannelCache() {
174 newChannelId2channel := make(map[int]*Channel)
175 var channels []*Channel
176 DB.Where("status = ?", ChannelStatusEnabled).Find(&channels)
177 for _, channel := range channels {
178 newChannelId2channel[channel.Id] = channel
179 }
180 var abilities []*Ability
181 DB.Find(&abilities)
182 groups := make(map[string]bool)
183 for _, ability := range abilities {
184 groups[ability.Group] = true
185 }
186 newGroup2model2channels := make(map[string]map[string][]*Channel)
187 for group := range groups {
188 newGroup2model2channels[group] = make(map[string][]*Channel)
189 }
190 for _, channel := range channels {
191 groups := strings.Split(channel.Group, ",")
192 for _, group := range groups {
193 models := strings.Split(channel.Models, ",")
194 for _, model := range models {
195 if _, ok := newGroup2model2channels[group][model]; !ok {
196 newGroup2model2channels[group][model] = make([]*Channel, 0)
197 }
198 newGroup2model2channels[group][model] = append(newGroup2model2channels[group][model], channel)
199 }
200 }
201 }
202
203 // sort by priority
204 for group, model2channels := range newGroup2model2channels {
205 for model, channels := range model2channels {
206 sort.Slice(channels, func(i, j int) bool {
207 return channels[i].GetPriority() > channels[j].GetPriority()
208 })
209 newGroup2model2channels[group][model] = channels
210 }
211 }
212
213 channelSyncLock.Lock()
214 group2model2channels = newGroup2model2channels
215 channelSyncLock.Unlock()
216 logger.SysLog("channels synced from database")
217}
218
219func SyncChannelCache(frequency int) {
220 for {

Callers 2

mainFunction · 0.92
SyncChannelCacheFunction · 0.85

Calls 2

SysLogFunction · 0.92
GetPriorityMethod · 0.80

Tested by

no test coverage detected