diff options
Diffstat (limited to 'resources')
-rw-r--r-- | resources/image.go | 4 | ||||
-rw-r--r-- | resources/image_cache.go | 27 | ||||
-rw-r--r-- | resources/resource_spec.go | 32 | ||||
-rw-r--r-- | resources/resource_transformers/htesting/testhelpers.go | 2 | ||||
-rw-r--r-- | resources/testdata/pix.gif | bin | 0 -> 42 bytes |
5 files changed, 42 insertions, 23 deletions
diff --git a/resources/image.go b/resources/image.go index c61e903ab..ad2f9de32 100644 --- a/resources/image.go +++ b/resources/image.go @@ -126,7 +126,7 @@ func (i *imageResource) getExif() *exif.ExifInfo { return enc.Encode(i.meta) } - _, i.metaInitErr = i.getSpec().imageCache.fileCache.ReadOrCreate(key, read, create) + _, i.metaInitErr = i.getSpec().ImageCache.fileCache.ReadOrCreate(key, read, create) }) if i.metaInitErr != nil { @@ -296,7 +296,7 @@ const imageProcWorkers = 1 var imageProcSem = make(chan bool, imageProcWorkers) func (i *imageResource) doWithImageConfig(conf images.ImageConfig, f func(src image.Image) (image.Image, error)) (images.ImageResource, error) { - img, err := i.getSpec().imageCache.getOrCreate(i, conf, func() (*imageResource, image.Image, error) { + img, err := i.getSpec().ImageCache.getOrCreate(i, conf, func() (*imageResource, image.Image, error) { imageProcSem <- true defer func() { <-imageProcSem diff --git a/resources/image_cache.go b/resources/image_cache.go index ca651fd5c..636607f94 100644 --- a/resources/image_cache.go +++ b/resources/image_cache.go @@ -26,16 +26,27 @@ import ( "github.com/gohugoio/hugo/helpers" ) -type imageCache struct { +// ImageCache is a cache for image resources. The backing caches are shared between all sites. +type ImageCache struct { pathSpec *helpers.PathSpec fileCache *filecache.Cache + *imageCacheStore +} + +type imageCacheStore struct { mu sync.RWMutex store map[string]*resourceAdapter } -func (c *imageCache) deleteIfContains(s string) { +// WithPathSpec returns a copy of the ImageCache with the given PathSpec set. +func (c ImageCache) WithPathSpec(ps *helpers.PathSpec) *ImageCache { + c.pathSpec = ps + return &c +} + +func (c *ImageCache) deleteIfContains(s string) { c.mu.Lock() defer c.mu.Unlock() s = c.normalizeKeyBase(s) @@ -48,21 +59,21 @@ func (c *imageCache) deleteIfContains(s string) { // The cache key is a lowercase path with Unix style slashes and it always starts with // a leading slash. -func (c *imageCache) normalizeKey(key string) string { +func (c *ImageCache) normalizeKey(key string) string { return "/" + c.normalizeKeyBase(key) } -func (c *imageCache) normalizeKeyBase(key string) string { +func (c *ImageCache) normalizeKeyBase(key string) string { return strings.Trim(strings.ToLower(filepath.ToSlash(key)), "/") } -func (c *imageCache) clear() { +func (c *ImageCache) clear() { c.mu.Lock() defer c.mu.Unlock() c.store = make(map[string]*resourceAdapter) } -func (c *imageCache) getOrCreate( +func (c *ImageCache) getOrCreate( parent *imageResource, conf images.ImageConfig, createImage func() (*imageResource, image.Image, error)) (*resourceAdapter, error) { relTarget := parent.relTargetPathFromConfig(conf) @@ -163,6 +174,6 @@ func (c *imageCache) getOrCreate( return imgAdapter, nil } -func newImageCache(fileCache *filecache.Cache, ps *helpers.PathSpec) *imageCache { - return &imageCache{fileCache: fileCache, pathSpec: ps, store: make(map[string]*resourceAdapter)} +func newImageCache(fileCache *filecache.Cache, ps *helpers.PathSpec) *ImageCache { + return &ImageCache{fileCache: fileCache, pathSpec: ps, imageCacheStore: &imageCacheStore{store: make(map[string]*resourceAdapter)}} } diff --git a/resources/resource_spec.go b/resources/resource_spec.go index 4d2ceccb3..5ecb021fe 100644 --- a/resources/resource_spec.go +++ b/resources/resource_spec.go @@ -51,6 +51,7 @@ import ( func NewSpec( s *helpers.PathSpec, common *SpecCommon, // may be nil + imageCache *ImageCache, // may be nil incr identity.Incrementer, logger loggers.Logger, errorHandler herrors.ErrorSender, @@ -90,11 +91,6 @@ func NewSpec( PostProcessResources: make(map[string]postpub.PostPublishedResource), JSConfigBuilder: jsconfig.NewBuilder(), }, - imageCache: newImageCache( - fileCaches.ImageCache(), - - s, - ), ResourceCache: &ResourceCache{ fileCache: fileCaches.AssetsCache(), cache: make(map[string]any), @@ -103,11 +99,22 @@ func NewSpec( } } + if imageCache == nil { + imageCache = newImageCache( + fileCaches.ImageCache(), + s, + ) + } else { + imageCache = imageCache.WithPathSpec(s) + + } + rs := &Spec{ PathSpec: s, Logger: logger, ErrorSender: errorHandler, imaging: imaging, + ImageCache: imageCache, ExecHelper: execHelper, Permalinks: permalinks, @@ -128,6 +135,8 @@ type Spec struct { Permalinks page.PermalinkExpander + ImageCache *ImageCache + // Holds default filter settings etc. imaging *images.ImageProcessor @@ -139,7 +148,6 @@ type Spec struct { // The parts of Spec that's comoon for all sites. type SpecCommon struct { incr identity.Incrementer - imageCache *imageCache ResourceCache *ResourceCache FileCaches filecache.Caches @@ -171,13 +179,13 @@ func (r *Spec) BuildConfig() config.BuildConfig { } func (r *Spec) CacheStats() string { - r.imageCache.mu.RLock() - defer r.imageCache.mu.RUnlock() + r.ImageCache.mu.RLock() + defer r.ImageCache.mu.RUnlock() - s := fmt.Sprintf("Cache entries: %d", len(r.imageCache.store)) + s := fmt.Sprintf("Cache entries: %d", len(r.ImageCache.store)) count := 0 - for k := range r.imageCache.store { + for k := range r.ImageCache.store { if count > 5 { break } @@ -189,12 +197,12 @@ func (r *Spec) CacheStats() string { } func (r *Spec) ClearCaches() { - r.imageCache.clear() + r.ImageCache.clear() r.ResourceCache.clear() } func (r *Spec) DeleteBySubstring(s string) { - r.imageCache.deleteIfContains(s) + r.ImageCache.deleteIfContains(s) } func (s *Spec) String() string { diff --git a/resources/resource_transformers/htesting/testhelpers.go b/resources/resource_transformers/htesting/testhelpers.go index 75ae4245e..b1feccc5f 100644 --- a/resources/resource_transformers/htesting/testhelpers.go +++ b/resources/resource_transformers/htesting/testhelpers.go @@ -43,7 +43,7 @@ func NewTestResourceSpec() (*resources.Spec, error) { return nil, err } - spec, err := resources.NewSpec(s, nil, nil, nil, nil, nil) + spec, err := resources.NewSpec(s, nil, nil, nil, nil, nil, nil) return spec, err } diff --git a/resources/testdata/pix.gif b/resources/testdata/pix.gif Binary files differnew file mode 100644 index 000000000..f191b280c --- /dev/null +++ b/resources/testdata/pix.gif |