| 1 |
// Copyright 2019 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package resources |
| 15 |
|
| 16 |
import ( |
| 17 |
"context" |
| 18 |
"encoding/json" |
| 19 |
"io" |
| 20 |
"path" |
| 21 |
"path/filepath" |
| 22 |
"strings" |
| 23 |
"sync" |
| 24 |
|
| 25 |
"github.com/bep/helpers/maphelpers" |
| 26 |
"github.com/gohugoio/hugo/resources/resource" |
| 27 |
|
| 28 |
"github.com/gohugoio/hugo/cache/dynacache" |
| 29 |
"github.com/gohugoio/hugo/cache/filecache" |
| 30 |
) |
| 31 |
|
| 32 |
func newResourceCache(rs *Spec, memCache *dynacache.Cache) *ResourceCache { |
| 33 |
return &ResourceCache{ |
| 34 |
fileCache: rs.FileCaches.AssetsCache(), |
| 35 |
cacheResource: dynacache.GetOrCreatePartition[string, resource.Resource]( |
| 36 |
memCache, |
| 37 |
"/res1", |
| 38 |
dynacache.OptionsPartition{ClearWhen: dynacache.ClearOnChange, Weight: 40}, |
| 39 |
), |
| 40 |
cacheResourceFile: dynacache.GetOrCreatePartition[string, resource.Resource]( |
| 41 |
memCache, |
| 42 |
"/res2", |
| 43 |
dynacache.OptionsPartition{ClearWhen: dynacache.ClearOnChange, Weight: 40}, |
| 44 |
), |
| 45 |
CacheResourceRemote: dynacache.GetOrCreatePartition[string, resource.Resource]( |
| 46 |
memCache, |
| 47 |
"/resr", |
| 48 |
dynacache.OptionsPartition{ClearWhen: dynacache.ClearOnChange, Weight: 40}, |
| 49 |
), |
| 50 |
cacheResources: dynacache.GetOrCreatePartition[string, resource.Resources]( |
| 51 |
memCache, |
| 52 |
"/ress", |
| 53 |
dynacache.OptionsPartition{ClearWhen: dynacache.ClearOnRebuild, Weight: 40}, |
| 54 |
), |
| 55 |
cacheResourceTransformation: dynacache.GetOrCreatePartition[string, *resourceAdapterInner]( |
| 56 |
memCache, |
| 57 |
"/res1/tra", |
| 58 |
dynacache.OptionsPartition{ClearWhen: dynacache.ClearOnChange, Weight: 40}, |
| 59 |
), |
| 60 |
|
| 61 |
cacheResourceTransformationPublished: maphelpers.NewConcurrentMap[string, string](), |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
type ResourceCache struct { |
| 66 |
sync.RWMutex |
| 67 |
|
| 68 |
cacheResource *dynacache.Partition[string, resource.Resource] |
| 69 |
cacheResourceFile *dynacache.Partition[string, resource.Resource] |
| 70 |
CacheResourceRemote *dynacache.Partition[string, resource.Resource] |
| 71 |
cacheResources *dynacache.Partition[string, resource.Resources] |
| 72 |
cacheResourceTransformation *dynacache.Partition[string, *resourceAdapterInner] |
| 73 |
|
| 74 |
// Used in rebuilds. Maps the target path to the last published transformation key. |
| 75 |
cacheResourceTransformationPublished *maphelpers.ConcurrentMap[string, string] |
| 76 |
|
| 77 |
fileCache *filecache.Cache |
| 78 |
} |
| 79 |
|
| 80 |
func (c *ResourceCache) cleanKey(key string) string { |
| 81 |
return strings.TrimPrefix(path.Clean(strings.ToLower(filepath.ToSlash(key))), "/") |
| 82 |
} |
| 83 |
|
| 84 |
func (c *ResourceCache) Get(ctx context.Context, key string) (resource.Resource, bool) { |
| 85 |
return c.cacheResource.Get(ctx, key) |
| 86 |
} |
| 87 |
|
| 88 |
func (c *ResourceCache) GetOrCreate(key string, f func() (resource.Resource, error)) (resource.Resource, error) { |
| 89 |
return c.cacheResource.GetOrCreate(key, func(key string) (resource.Resource, error) { |
| 90 |
return f() |
| 91 |
}) |
| 92 |
} |
| 93 |
|
| 94 |
func (c *ResourceCache) GetOrCreateFile(key string, f func() (resource.Resource, error)) (resource.Resource, error) { |
| 95 |
return c.cacheResourceFile.GetOrCreate(key, func(key string) (resource.Resource, error) { |
| 96 |
return f() |
| 97 |
}) |
| 98 |
} |
| 99 |
|
| 100 |
func (c *ResourceCache) GetOrCreateResources(key string, f func() (resource.Resources, error)) (resource.Resources, error) { |
| 101 |
return c.cacheResources.GetOrCreate(key, func(key string) (resource.Resources, error) { |
| 102 |
return f() |
| 103 |
}) |
| 104 |
} |
| 105 |
|
| 106 |
func (c *ResourceCache) getFilenames(key string) (string, string) { |
| 107 |
filenameMeta := key + ".json" |
| 108 |
filenameContent := key + ".content" |
| 109 |
|
| 110 |
return filenameMeta, filenameContent |
| 111 |
} |
| 112 |
|
| 113 |
func (c *ResourceCache) getFromFile(key string) (filecache.ItemInfo, io.ReadCloser, transformedResourceMetadata, bool) { |
| 114 |
c.RLock() |
| 115 |
defer c.RUnlock() |
| 116 |
|
| 117 |
var meta transformedResourceMetadata |
| 118 |
filenameMeta, filenameContent := c.getFilenames(key) |
| 119 |
|
| 120 |
jsonContent, _ := c.fileCache.GetBytes(filenameMeta) |
| 121 |
if jsonContent == nil { |
| 122 |
return filecache.ItemInfo{}, nil, meta, false |
| 123 |
} |
| 124 |
|
| 125 |
if err := json.Unmarshal(jsonContent, &meta); err != nil { |
| 126 |
return filecache.ItemInfo{}, nil, meta, false |
| 127 |
} |
| 128 |
|
| 129 |
fi, rc, _ := c.fileCache.Get(filenameContent) |
| 130 |
|
| 131 |
return fi, rc, meta, rc != nil |
| 132 |
} |
| 133 |
|
| 134 |
// writeMeta writes the metadata to file and returns a writer for the content part. |
| 135 |
func (c *ResourceCache) writeMeta(key string, meta transformedResourceMetadata) (filecache.ItemInfo, io.WriteCloser, error) { |
| 136 |
filenameMeta, filenameContent := c.getFilenames(key) |
| 137 |
raw, err := json.Marshal(meta) |
| 138 |
if err != nil { |
| 139 |
return filecache.ItemInfo{}, nil, err |
| 140 |
} |
| 141 |
|
| 142 |
_, fm, err := c.fileCache.WriteCloser(filenameMeta) |
| 143 |
if err != nil { |
| 144 |
return filecache.ItemInfo{}, nil, err |
| 145 |
} |
| 146 |
defer fm.Close() |
| 147 |
|
| 148 |
if _, err := fm.Write(raw); err != nil { |
| 149 |
return filecache.ItemInfo{}, nil, err |
| 150 |
} |
| 151 |
|
| 152 |
fi, fc, err := c.fileCache.WriteCloser(filenameContent) |
| 153 |
|
| 154 |
return fi, fc, err |
| 155 |
} |