// GetOrCreate gets or creates a value for the given key.
func (p *Partition[K, V]) GetOrCreate(key K, create func(key K) (V, error)) (V, error) {
+ v, err := p.doGetOrCreate(key, create)
+ if err != nil {
+ return p.zero, err
+ }
+ if resource.StaleVersion(v) > 0 {
+ p.c.Delete(key)
+ return p.doGetOrCreate(key, create)
+ }
+ return v, err
+}
+
+func (p *Partition[K, V]) doGetOrCreate(key K, create func(key K) (V, error)) (V, error) {
v, _, err := p.c.GetOrCreate(key, create)
return v, err
}
+func (p *Partition[K, V]) GetOrCreateWitTimeout(key K, duration time.Duration, create func(key K) (V, error)) (V, error) {
+ v, err := p.doGetOrCreateWitTimeout(key, duration, create)
+ if err != nil {
+ return p.zero, err
+ }
+ if resource.StaleVersion(v) > 0 {
+ p.c.Delete(key)
+ return p.doGetOrCreateWitTimeout(key, duration, create)
+ }
+ return v, err
+}
+
// GetOrCreateWitTimeout gets or creates a value for the given key and times out if the create function
// takes too long.
-func (p *Partition[K, V]) GetOrCreateWitTimeout(key K, duration time.Duration, create func(key K) (V, error)) (V, error) {
+func (p *Partition[K, V]) doGetOrCreateWitTimeout(key K, duration time.Duration, create func(key K) (V, error)) (V, error) {
resultch := make(chan V, 1)
errch := make(chan error, 1)
shouldDelete := func(key K, v V) bool {
// We always clear elements marked as stale.
- if resource.IsStaleAny(v) {
+ if resource.StaleVersion(v) > 0 {
return true
}
func (p *Partition[K, V]) clearStale() {
p.c.DeleteFunc(func(key K, v V) bool {
- isStale := resource.IsStaleAny(v)
- if isStale {
+ staleVersion := resource.StaleVersion(v)
+ if staleVersion > 0 {
p.trace.Log(
logg.StringFunc(
func() string {
)
}
- return isStale
+ return staleVersion > 0
})
}
)
type testItem struct {
- name string
- isStale bool
+ name string
+ staleVersion uint32
}
-func (t testItem) IsStale() bool {
- return t.isStale
+func (t testItem) StaleVersion() uint32 {
+ return t.staleVersion
}
func (t testItem) IdentifierBase() string {
p2.GetOrCreate("clearBecauseStale", func(string) (testItem, error) {
return testItem{
- isStale: true,
+ staleVersion: 32,
}, nil
})
p2.GetOrCreate("clearNever", func(string) (testItem, error) {
return testItem{
- isStale: false,
+ staleVersion: 0,
}, nil
})
if !ok {
panic(fmt.Sprintf("unknown type %T", new))
}
- if newp != old {
- resource.MarkStale(old)
- }
if vv.s.languagei == newp.s.languagei {
+ if newp != old {
+ resource.MarkStale(old)
+ }
return new
}
is := make(contentNodeIs, s.numLanguages)
if oldp != newp {
resource.MarkStale(oldp)
}
-
vv[newp.s.languagei] = new
return vv
case *resourceSource:
panic(fmt.Sprintf("unknown type %T", new))
}
if vv.LangIndex() == newp.LangIndex() {
+ if vv != newp {
+ resource.MarkStale(vv)
+ }
return new
}
rs := make(resourceSources, s.numLanguages)
)
for _, id := range changes {
- if staler, ok := id.(resource.Staler); ok && !staler.IsStale() {
+ if staler, ok := id.(resource.Staler); ok {
var msgDetail string
if p, ok := id.(*pageState); ok && p.File() != nil {
msgDetail = fmt.Sprintf(" (%s)", p.File().Filename())
func (c *contentParseInfo) contentSource(s resource.StaleInfo) ([]byte, error) {
key := c.sourceKey
+ versionv := s.StaleVersion()
+
v, err := c.h.cacheContentSource.GetOrCreate(key, func(string) (*resources.StaleValue[[]byte], error) {
b, err := c.readSourceAll()
if err != nil {
return &resources.StaleValue[[]byte]{
Value: b,
- IsStaleFunc: func() bool {
- return s.IsStale()
+ StaleVersionFunc: func() uint32 {
+ return s.StaleVersion() - versionv
},
}, nil
})
func (c *cachedContent) contentRendered(ctx context.Context, cp *pageContentOutput) (contentSummary, error) {
ctx = tpl.Context.DependencyScope.Set(ctx, pageDependencyScopeGlobal)
key := c.pi.sourceKey + "/" + cp.po.f.Name
- versionv := cp.contentRenderedVersion
+ versionv := c.version(cp)
v, err := c.pm.cacheContentRendered.GetOrCreate(key, func(string) (*resources.StaleValue[contentSummary], error) {
cp.po.p.s.Log.Trace(logg.StringFunc(func() string {
}
rs := &resources.StaleValue[contentSummary]{
- IsStaleFunc: func() bool {
- return c.IsStale() || cp.contentRenderedVersion != versionv
+ StaleVersionFunc: func() uint32 {
+ return c.version(cp) - versionv
},
}
func (c *cachedContent) contentToC(ctx context.Context, cp *pageContentOutput) (contentTableOfContents, error) {
key := c.pi.sourceKey + "/" + cp.po.f.Name
- versionv := cp.contentRenderedVersion
+ versionv := c.version(cp)
v, err := c.pm.contentTableOfContents.GetOrCreate(key, func(string) (*resources.StaleValue[contentTableOfContents], error) {
source, err := c.pi.contentSource(c)
return &resources.StaleValue[contentTableOfContents]{
Value: ct,
- IsStaleFunc: func() bool {
- return c.IsStale() || cp.contentRenderedVersion != versionv
+ StaleVersionFunc: func() uint32 {
+ return c.version(cp) - versionv
},
}, nil
})
return v.Value, nil
}
+func (c *cachedContent) version(cp *pageContentOutput) uint32 {
+ // Both of these gets incremented on change.
+ return c.StaleVersion() + cp.contentRenderedVersion
+}
+
func (c *cachedContent) contentPlain(ctx context.Context, cp *pageContentOutput) (contentPlainPlainWords, error) {
key := c.pi.sourceKey + "/" + cp.po.f.Name
- versionv := cp.contentRenderedVersion
+ versionv := c.version(cp)
v, err := c.pm.cacheContentPlain.GetOrCreateWitTimeout(key, cp.po.p.s.Conf.Timeout(), func(string) (*resources.StaleValue[contentPlainPlainWords], error) {
var result contentPlainPlainWords
rs := &resources.StaleValue[contentPlainPlainWords]{
- IsStaleFunc: func() bool {
- return c.IsStale() || cp.contentRenderedVersion != versionv
+ StaleVersionFunc: func() uint32 {
+ return c.version(cp) - versionv
},
}
// typically included with .RenderShortcodes.
otherOutputs map[uint64]*pageContentOutput
- contentRenderedVersion int // Incremented on reset.
- contentRendered bool // Set on content render.
+ contentRenderedVersion uint32 // Incremented on reset.
+ contentRendered bool // Set on content render.
// Renders Markdown hooks.
renderHooks *renderHooks
Home Content.
-- content/hometext.txt --
Home Text Content.
+-- content/myothersection/myothersectionpage.md --
+---
+title: "myothersectionpage"
+---
+myothersectionpage Content.
-- layouts/_default/single.html --
Single: {{ .Title }}|{{ .Content }}$
Resources: {{ range $i, $e := .Resources }}{{ $i }}:{{ .RelPermalink }}|{{ .Content }}|{{ end }}$
b.RenameFile("content/mysection/mysectionbundle/mysectionbundletext.txt", "content/mysection/mysectionbundle/mysectionbundletext2.txt").Build()
b.AssertFileContent("public/mysection/mysectionbundle/index.html", "mysectionbundletext2", "My Section Bundle Text 2 Content.", "Len Resources: 2|")
- b.AssertRenderCountPage(3)
- b.AssertRenderCountContent(3)
+ b.AssertRenderCountPage(5)
+ b.AssertRenderCountContent(6)
})
}
b.AssertFileContent("public/mysection/mysectionbundle/index.html", "My Section Bundle Content Content Edited.")
}
+func TestRebuilEditContentFileThenAnother(t *testing.T) {
+ b := TestRunning(t, rebuildFilesSimple)
+ b.EditFileReplaceAll("content/mysection/mysectionbundle/mysectionbundlecontent.md", "Content Content.", "Content Content Edited.").Build()
+ b.AssertFileContent("public/mysection/mysectionbundle/index.html", "My Section Bundle Content Content Edited.")
+ b.AssertRenderCountPage(1)
+ b.AssertRenderCountContent(2)
+
+ b.EditFileReplaceAll("content/myothersection/myothersectionpage.md", "myothersectionpage Content.", "myothersectionpage Content Edited.").Build()
+ b.AssertFileContent("public/myothersection/myothersectionpage/index.html", "myothersectionpage Content Edited")
+ b.AssertRenderCountPage(1)
+ b.AssertRenderCountContent(1)
+}
+
func TestRebuildRenameTextFileInBranchBundle(t *testing.T) {
b := TestRunning(t, rebuildFilesSimple)
b.AssertFileContent("public/mysection/index.html", "My Section")
b.RenameFile("content/hometext.txt", "content/hometext2.txt").Build()
b.AssertFileContent("public/index.html", "hometext2", "Home Text Content.")
- b.AssertRenderCountPage(2)
+ b.AssertRenderCountPage(3)
}
func TestRebuildRenameDirectoryWithLeafBundle(t *testing.T) {
b.AssertFileContent("public/mysectionrenamed/index.html", "My Section")
b.AssertFileContent("public/mysectionrenamed/mysectionbundle/index.html", "My Section Bundle")
b.AssertFileContent("public/mysectionrenamed/mysectionbundle/mysectionbundletext.txt", "My Section Bundle Text 2 Content.")
- b.AssertRenderCountPage(2)
+ b.AssertRenderCountPage(3)
}
func TestRebuildRenameDirectoryWithRegularPageUsedInHome(t *testing.T) {
b.AssertFileContent("public/mysectionrenamed/index.html", "My Section")
b.AssertFileContent("public/mysectionrenamed/mysectionbundle/index.html", "My Section Bundle")
b.AssertFileContent("public/mysectionrenamed/mysectionbundle/mysectionbundletext.txt", "My Section Bundle Text 2 Content.")
- b.AssertRenderCountPage(2)
+ b.AssertRenderCountPage(3)
}
func TestRebuilErrorRecovery(t *testing.T) {
b.AssertFileContent("public/p1/index.html", "Edited")
}
+func TestRenderShortcodesEditSectionContentWithShortcodeInIncludedPageIssue12458(t *testing.T) {
+ t.Parallel()
+
+ files := `
+-- hugo.toml --
+disableLiveReload = true
+disableKinds = ["home", "taxonomy", "term", "rss", "sitemap", "robotsTXT", "404"]
+-- content/mysection/_index.md --
+---
+title: "My Section"
+---
+## p1-h1
+{{% include "p2" %}}
+-- content/mysection/p2.md --
+---
+title: "p2"
+---
+### Original
+{{% myshort %}}
+-- layouts/shortcodes/include.html --
+{{ $p := .Page.GetPage (.Get 0) }}
+{{ $p.RenderShortcodes }}
+-- layouts/shortcodes/myshort.html --
+Myshort Original.
+-- layouts/_default/list.html --
+ {{ .Content }}
+
+
+
+`
+ b := TestRunning(t, files)
+
+ b.AssertFileContent("public/mysection/index.html", "p1-h1")
+ b.EditFileReplaceAll("content/mysection/_index.md", "p1-h1", "p1-h1 Edited").Build()
+ b.AssertFileContent("public/mysection/index.html", "p1-h1 Edited")
+}
+
func TestRenderShortcodesNestedPageContextIssue12356(t *testing.T) {
t.Parallel()
// We currently rebuild all the language versions of the same content file.
// We could probably optimize that case, but it's not trivial.
- b.Assert(int(counters.contentRenderCounter.Load()), qt.Equals, 33)
+ b.Assert(int(counters.contentRenderCounter.Load()), qt.Equals, 4)
b.AssertFileContent("public"+p.RelPermalink()+"index.html", "Edited!!")
}
hash() string
}
+var _ resource.StaleInfo = (*StaleValue[any])(nil)
+
type StaleValue[V any] struct {
// The value.
Value V
- // IsStaleFunc reports whether the value is stale.
- IsStaleFunc func() bool
+ // StaleVersionFunc reports the current version of the value.
+ // This always starts out at 0 and get incremented on staleness.
+ StaleVersionFunc func() uint32
}
-func (s *StaleValue[V]) IsStale() bool {
- return s.IsStaleFunc()
+func (s *StaleValue[V]) StaleVersion() uint32 {
+ return s.StaleVersionFunc()
}
type AtomicStaler struct {
}
func (s *AtomicStaler) MarkStale() {
- atomic.StoreUint32(&s.stale, 1)
+ atomic.AddUint32(&s.stale, 1)
}
-func (s *AtomicStaler) IsStale() bool {
- return atomic.LoadUint32(&(s.stale)) > 0
+func (s *AtomicStaler) StaleVersion() uint32 {
+ return atomic.LoadUint32(&(s.stale))
}
// For internal use.
// StaleInfo tells if a resource is marked as stale.
type StaleInfo interface {
- IsStale() bool
+ StaleVersion() uint32
}
-// IsStaleAny reports whether any of the os is marked as stale.
-func IsStaleAny(os ...any) bool {
- for _, o := range os {
- if s, ok := o.(StaleInfo); ok && s.IsStale() {
- return true
+// StaleVersion returns the StaleVersion for the given os,
+// or 0 if not set.
+func StaleVersion(os any) uint32 {
+ if s, ok := os.(StaleInfo); ok {
+ return s.StaleVersion()
+ }
+ return 0
+}
+
+// StaleVersionSum calculates the sum of the StaleVersionSum for the given oss.
+func StaleVersionSum(oss ...any) uint32 {
+ var version uint32
+ for _, o := range oss {
+ if s, ok := o.(StaleInfo); ok && s.StaleVersion() > 0 {
+ version += s.StaleVersion()
}
}
- return false
+ return version
}
// MarkStale will mark any of the oses as stale, if possible.
*publishOnce
}
-func (r *resourceAdapterInner) IsStale() bool {
- return r.Staler.IsStale() || r.target.IsStale()
+func (r *resourceAdapterInner) StaleVersion() uint32 {
+ // Both of these are incremented on change.
+ return r.Staler.StaleVersion() + r.target.StaleVersion()
}
type resourceTransformations struct {
return &resources.StaleValue[any]{
Value: v,
- IsStaleFunc: func() bool {
- return resource.IsStaleAny(r)
+ StaleVersionFunc: func() uint32 {
+ return resource.StaleVersion(r)
},
}, nil
})
return &resources.StaleValue[any]{
Value: v,
- IsStaleFunc: func() bool {
- return false
+ StaleVersionFunc: func() uint32 {
+ return 0
},
}, nil
})