Add PartialCached variant #1368

Closed
bep opened this Issue Aug 21, 2015 · 4 comments

Comments

Projects
None yet
4 participants
@bep
Member

bep commented Aug 21, 2015

See

http://discuss.gohugo.io/t/how-to-make-hugo-faster/1597/6

{{ range $.Site.Pages.ByTitle }}
  {{ if and (in .Params.categories "Category") (in .Params.categories "sub-category") }}
    <li><a href="{{ .Permalink }}">{{ .Title }}</a>
        {{ if .Draft }} * {{ end }}</li>
  {{ end }}
{{ end }}

The above is an extract of a partial used on every page. It is an ineffective construct (slightly better in 0.15, I guess) and it gets really bad when page count goes up.

But it's static. And, although it is impossible to make a general assumption about how "cacheable" a given partial is (I think?), the user will know -- and should be able to say so.

So, for a site with 2000 pages, the above should only be needed to be processed once.

@tatsushid not sure how Ace include works in this area?

@bep bep added the Enhancement label Aug 21, 2015

@tatsushid

This comment has been minimized.

Show comment
Hide comment
@tatsushid

tatsushid Aug 22, 2015

Contributor

@bep, Ace parses a template file every time when the ParseSource and CompileResult or CompileResultWithTemplate are called. include is parsed when a base template file is read and is later (compile time) replaced by {{ template "template_name" pipeline }}. So using include is the same using template directly.

Ace caches parsed template files with these template names in a hash and if it finds a name in the hash, it bypasses parsing the template file, just returns a compile result which is a HTML file with Go template syntax.

But this cache mechanism is only implemented in ace.Load function. Hugo doesn't use it to merge all templates and use custom template function. So, instead of using Load, Hugo emulates the function without any cache mechanisms.

Anyway, Ace doesn't cache any rendering results. It just caches compiled result if Load is used. Executing template with values is needed.

Contributor

tatsushid commented Aug 22, 2015

@bep, Ace parses a template file every time when the ParseSource and CompileResult or CompileResultWithTemplate are called. include is parsed when a base template file is read and is later (compile time) replaced by {{ template "template_name" pipeline }}. So using include is the same using template directly.

Ace caches parsed template files with these template names in a hash and if it finds a name in the hash, it bypasses parsing the template file, just returns a compile result which is a HTML file with Go template syntax.

But this cache mechanism is only implemented in ace.Load function. Hugo doesn't use it to merge all templates and use custom template function. So, instead of using Load, Hugo emulates the function without any cache mechanisms.

Anyway, Ace doesn't cache any rendering results. It just caches compiled result if Load is used. Executing template with values is needed.

@tatsushid

This comment has been minimized.

Show comment
Hide comment
@tatsushid

tatsushid Aug 22, 2015

Contributor

Partial is used in both rendering with page specific values and rendering with common values to whole site. There is no way to know that at rendering time but only know a template author so introducing something "partial with cache" function is a reasonable way I think.

Contributor

tatsushid commented Aug 22, 2015

Partial is used in both rendering with page specific values and rendering with common values to whole site. There is no way to know that at rendering time but only know a template author so introducing something "partial with cache" function is a reasonable way I think.

@bep bep changed the title from Add caching option to Partial to Add PartialCached variant Aug 22, 2015

@LiaungYip

This comment has been minimized.

Show comment
Hide comment
@LiaungYip

LiaungYip Aug 8, 2016

Contributor

I think the good people at Camunda are maintaining a version of Hugo with cached partials.

There was some discussion about submitting a PR to get that functionality into main.

See:

https://github.com/camunda-third-party/hugo/tree/camunda

particularly camunda-third-party@e15f8f5 :

feat(tpl): add cachedPartial template function
- allows to specify that a partial should be cached after first
  evaluation, useful for static complex partials often used partials

https://app.camunda.com/jira/browse/CAM-6005

https://app.camunda.com/jira/browse/CAM-6385

Contributor

LiaungYip commented Aug 8, 2016

I think the good people at Camunda are maintaining a version of Hugo with cached partials.

There was some discussion about submitting a PR to get that functionality into main.

See:

https://github.com/camunda-third-party/hugo/tree/camunda

particularly camunda-third-party@e15f8f5 :

feat(tpl): add cachedPartial template function
- allows to specify that a partial should be cached after first
  evaluation, useful for static complex partials often used partials

https://app.camunda.com/jira/browse/CAM-6005

https://app.camunda.com/jira/browse/CAM-6385

LiaungYip referenced this issue in camunda-third-party/hugo Aug 8, 2016

feat(tpl): add cachedPartial template function
- allows to specify that a partial should be cached after first
  evaluation, useful for static complex partials often used partials

@bep bep added this to the v0.18 milestone Oct 10, 2016

@bep

This comment has been minimized.

Show comment
Hide comment
@bep

bep Oct 10, 2016

Member

Suggested API:

partialCached(name string, context interface{}, variant ...string) template.HTML
  • Cache key constructed from name + (optional) variant
  • Note that this will be called from multiple threads, so need a rw mutex
Member

bep commented Oct 10, 2016

Suggested API:

partialCached(name string, context interface{}, variant ...string) template.HTML
  • Cache key constructed from name + (optional) variant
  • Note that this will be called from multiple threads, so need a rw mutex

moorereason added a commit to moorereason/hugo that referenced this issue Oct 11, 2016

tpl: Add partialCached template function
Supports an optional variant string parameter so that a given partial
will be cached based upon the name+variant.

Fixes #1368

@bep bep closed this in 474eb45 Oct 11, 2016

tychoish added a commit to tychoish/hugo that referenced this issue Aug 13, 2017

tpl: Add partialCached template function
Supports an optional variant string parameter so that a given partial
will be cached based upon the name+variant.

Fixes #1368
Closes #2552
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment