MCPcopy
hub / github.com/mudler/LocalAI / AvailableGalleryModelsCached

Function AvailableGalleryModelsCached

core/gallery/gallery.go:317–348  ·  view source on GitHub ↗

AvailableGalleryModelsCached returns gallery models from an in-memory cache. Local-only fields (installed status) are refreshed on every call. A background goroutine is triggered to re-fetch the full model list (including network calls) so subsequent requests pick up changes without blocking the cal

(galleries []config.Gallery, systemState *system.SystemState)

Source from the content-addressed store, hash-verified

315// calls) so subsequent requests pick up changes without blocking the caller.
316// The first call with an empty cache blocks until the initial load completes.
317func AvailableGalleryModelsCached(galleries []config.Gallery, systemState *system.SystemState) (GalleryElements[*GalleryModel], error) {
318 availableModelsMu.RLock()
319 cached := availableModelsCache
320 availableModelsMu.RUnlock()
321
322 if cached != nil {
323 // Refresh installed status under write lock to avoid races with
324 // concurrent readers and the background refresh goroutine.
325 availableModelsMu.Lock()
326 for _, m := range cached {
327 _, err := os.Stat(filepath.Join(systemState.Model.ModelsPath, fmt.Sprintf("%s.yaml", m.GetName())))
328 m.SetInstalled(err == nil)
329 }
330 availableModelsMu.Unlock()
331 // Trigger a background refresh if one is not already running.
332 triggerGalleryRefresh(galleries, systemState)
333 return cached, nil
334 }
335
336 // No cache yet — must do a blocking load.
337 models, err := AvailableGalleryModels(galleries, systemState)
338 if err != nil {
339 return nil, err
340 }
341
342 availableModelsMu.Lock()
343 availableModelsCache = models
344 galleryGeneration.Add(1)
345 availableModelsMu.Unlock()
346
347 return models, nil
348}
349
350// triggerGalleryRefresh starts a background goroutine that refreshes the
351// gallery model cache. Only one refresh runs at a time; concurrent calls

Callers 1

RegisterUIAPIRoutesFunction · 0.92

Calls 7

triggerGalleryRefreshFunction · 0.85
AvailableGalleryModelsFunction · 0.85
AddMethod · 0.80
LockMethod · 0.65
GetNameMethod · 0.65
SetInstalledMethod · 0.65
UnlockMethod · 0.65

Tested by

no test coverage detected