summaryrefslogtreecommitdiffstats
path: root/resources
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2022-04-11 10:34:08 +0200
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2022-04-13 09:18:17 +0200
commit2dbdf38a5411c554146ddd915a0aea3b2eaf4407 (patch)
tree333aaaac81aff606b1e6d39adc1663cec0fa8a32 /resources
parentf8c4e1690a462a2dcafa05aeaab65d1fad6df61e (diff)
resources: Add `key` to reources.GetRemote options map
If set, `key` will be used as the only cache key element for the resource. The default behaviour is to calculate the key based on the URL and all the options. This means that you can now do: ``` {{ $cacheKey := print $url (now.Format "2006-01-02") }} {{ $resource := resource.GetRemote $url (dict "key" $cacheKey) }} ``` Fixes #9755
Diffstat (limited to 'resources')
-rw-r--r--resources/resource_factories/create/remote.go10
-rw-r--r--resources/resource_factories/create/remote_test.go11
2 files changed, 20 insertions, 1 deletions
diff --git a/resources/resource_factories/create/remote.go b/resources/resource_factories/create/remote.go
index 56bd99cb1..32dfafe5c 100644
--- a/resources/resource_factories/create/remote.go
+++ b/resources/resource_factories/create/remote.go
@@ -27,6 +27,7 @@ import (
"strings"
"github.com/gohugoio/hugo/common/hugio"
+ "github.com/gohugoio/hugo/common/maps"
"github.com/gohugoio/hugo/common/types"
"github.com/gohugoio/hugo/helpers"
"github.com/gohugoio/hugo/media"
@@ -79,7 +80,7 @@ func (c *Client) FromRemote(uri string, optionsm map[string]any) (resource.Resou
return nil, errors.Wrapf(err, "failed to parse URL for resource %s", uri)
}
- resourceID := helpers.HashString(uri, optionsm)
+ resourceID := calculateResourceID(uri, optionsm)
_, httpResponse, err := c.cacheGetResource.GetOrCreate(resourceID, func() (io.ReadCloser, error) {
options, err := decodeRemoteOptions(optionsm)
@@ -199,6 +200,13 @@ func (c *Client) validateFromRemoteArgs(uri string, options fromRemoteOptions) e
return nil
}
+func calculateResourceID(uri string, optionsm map[string]any) string {
+ if key, found := maps.LookupEqualFold(optionsm, "key"); found {
+ return helpers.HashString(key)
+ }
+ return helpers.HashString(uri, optionsm)
+}
+
func addDefaultHeaders(req *http.Request, accepts ...string) {
for _, accept := range accepts {
if !hasHeaderValue(req.Header, "Accept", accept) {
diff --git a/resources/resource_factories/create/remote_test.go b/resources/resource_factories/create/remote_test.go
index cfd18269f..c2a3b7b32 100644
--- a/resources/resource_factories/create/remote_test.go
+++ b/resources/resource_factories/create/remote_test.go
@@ -83,3 +83,14 @@ func TestDecodeRemoteOptions(t *testing.T) {
}
}
+
+func TestCalculateResourceID(t *testing.T) {
+ c := qt.New(t)
+
+ c.Assert(calculateResourceID("foo", nil), qt.Equals, "5917621528921068675")
+ c.Assert(calculateResourceID("foo", map[string]any{"bar": "baz"}), qt.Equals, "7294498335241413323")
+
+ c.Assert(calculateResourceID("foo", map[string]any{"key": "1234", "bar": "baz"}), qt.Equals, "14904296279238663669")
+ c.Assert(calculateResourceID("asdf", map[string]any{"key": "1234", "bar": "asdf"}), qt.Equals, "14904296279238663669")
+ c.Assert(calculateResourceID("asdf", map[string]any{"key": "12345", "bar": "asdf"}), qt.Equals, "12191037851845371770")
+}