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