This commit is contained in:
(Moai Emoji) 2026-03-24 23:35:38 +00:00 committed by GitHub
commit c748722000
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 28 additions and 12 deletions

View file

@ -1,6 +1,7 @@
package pkg
import (
"sync"
"time"
)
@ -10,22 +11,23 @@ type cacheEntry struct {
}
type repositoryCache struct {
mu sync.RWMutex
// cache maps the URL to the last modified time and the data
cache map[string]cacheEntry
}
func (c *repositoryCache) ensureCache() {
if c.cache == nil {
c.cache = make(map[string]cacheEntry)
}
}
func (c *repositoryCache) lastModified(url string) *time.Time {
if c == nil {
return nil
}
c.ensureCache()
c.mu.RLock()
defer c.mu.RUnlock()
if c.cache == nil {
return nil
}
e, found := c.cache[url]
if !found {
@ -36,7 +38,13 @@ func (c *repositoryCache) lastModified(url string) *time.Time {
}
func (c *repositoryCache) getPackageList(url string) []RemotePackage {
c.ensureCache()
c.mu.RLock()
defer c.mu.RUnlock()
if c.cache == nil {
return nil
}
e, found := c.cache[url]
if !found {
@ -51,7 +59,13 @@ func (c *repositoryCache) cacheList(url string, lastModified time.Time, data []R
return
}
c.ensureCache()
c.mu.Lock()
defer c.mu.Unlock()
if c.cache == nil {
c.cache = make(map[string]cacheEntry)
}
c.cache[url] = cacheEntry{
lastModified: lastModified,
data: data,

View file

@ -10,6 +10,7 @@ import (
"net/http"
"net/url"
"path/filepath"
"sync"
"github.com/stashapp/stash/pkg/logger"
"github.com/stashapp/stash/pkg/models"
@ -31,13 +32,14 @@ type Manager struct {
Client *http.Client
cache *repositoryCache
cacheOnce sync.Once
cache *repositoryCache
}
func (m *Manager) getCache() *repositoryCache {
if m.cache == nil {
m.cacheOnce.Do(func() {
m.cache = &repositoryCache{}
}
})
return m.cache
}