all: Replace RWMutex struct caches with ConcurrentMap

This commit is contained in:
Bjørn Erik Pedersen
2026-05-23 12:12:51 +02:00
parent d8c70218b7
commit c4bbc2805c
13 changed files with 66 additions and 168 deletions
+13 -29
View File
@@ -20,8 +20,8 @@ import (
"image"
"path"
"path/filepath"
"sync"
"github.com/bep/helpers/maphelpers"
"github.com/bep/overlayfs"
"github.com/gohugoio/hugo/common/hashing"
"github.com/gohugoio/hugo/common/hugio"
@@ -52,7 +52,7 @@ func New(d *deps.Deps) *Namespace {
return &Namespace{
readFileFs: readFileFs,
Filters: &images.Filters{},
cache: map[string]image.Config{},
cache: maphelpers.NewConcurrentMap[string, image.Config](),
deps: d,
createClient: create.New(d.ResourceSpec),
}
@@ -62,8 +62,7 @@ func New(d *deps.Deps) *Namespace {
type Namespace struct {
*images.Filters
readFileFs afero.Fs
cacheMu sync.RWMutex
cache map[string]image.Config
cache *maphelpers.ConcurrentMap[string, image.Config]
deps *deps.Deps
createClient *create.Client
}
@@ -80,34 +79,19 @@ func (ns *Namespace) Config(path any) (image.Config, error) {
return image.Config{}, errors.New("config needs a filename")
}
// Check cache for image config.
ns.cacheMu.RLock()
config, ok := ns.cache[filename]
ns.cacheMu.RUnlock()
return ns.cache.GetOrCreate(filename, func() (image.Config, error) {
f, err := ns.readFileFs.Open(filename)
if err != nil {
return image.Config{}, err
}
defer f.Close()
if ok {
return config, nil
}
ext := filepath.Ext(filename)
format, _ := images.ImageFormatFromExt(ext)
f, err := ns.readFileFs.Open(filename)
if err != nil {
return image.Config{}, err
}
defer f.Close()
ext := filepath.Ext(filename)
format, _ := images.ImageFormatFromExt(ext)
config, _, err = ns.deps.ResourceSpec.Imaging.Codec.DecodeConfig(format, f)
if err != nil {
config, _, err := ns.deps.ResourceSpec.Imaging.Codec.DecodeConfig(format, f)
return config, err
}
ns.cacheMu.Lock()
ns.cache[filename] = config
ns.cacheMu.Unlock()
return config, nil
})
}
// Filter applies the given filters to the image given as the last element in args.
+2 -1
View File
@@ -113,7 +113,8 @@ func TestNSConfig(t *testing.T) {
c.Assert(err, qt.IsNil)
c.Assert(result, qt.Equals, test.expect)
c.Assert(len(ns.cache), qt.Not(qt.Equals), 0)
_, cached := ns.cache.Lookup(sp)
c.Assert(cached, qt.IsTrue)
}
}