stash/pkg/pkg/cache.go
(Moai Emoji) 8af2cfe525
Add mutex to repositoryCache for thread safety (#6741)
* Add mutex to package cache to prevent concurrent map write crash
* use sync.Once for cache init
2026-03-30 09:09:28 +11:00

73 lines
1 KiB
Go

package pkg
import (
"sync"
"time"
)
type cacheEntry struct {
lastModified time.Time
data []RemotePackage
}
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) lastModified(url string) *time.Time {
if c == nil {
return nil
}
c.mu.RLock()
defer c.mu.RUnlock()
if c.cache == nil {
return nil
}
e, found := c.cache[url]
if !found {
return nil
}
return &e.lastModified
}
func (c *repositoryCache) getPackageList(url string) []RemotePackage {
c.mu.RLock()
defer c.mu.RUnlock()
if c.cache == nil {
return nil
}
e, found := c.cache[url]
if !found {
return nil
}
return e.data
}
func (c *repositoryCache) cacheList(url string, lastModified time.Time, data []RemotePackage) {
if c == nil {
return
}
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,
}
}