Version 7, last updated by micrypt at May 06, 2011 01:26 UTC
TODO// Reword this to read reasonably well. Adapted from mailing list exchange.
Question:
Many of our snippets/views are expensive to generate (we're doing cpu-bound work for each page).
Use case of caching many different *pages* per template. e.g. we have one lift template, and using url-rewriting it represents thousands of pages each with their own url.
Reply:
Examine LiftRules.templateCache. It is possible to create your own TemplateCache subclass that hits disk or Redis or an alternative caching backend.
If the particular template isn't found (e.g. cached_camera_foo_bar), you can use Lift's templating mechanism to calculate the tempate so that it doesn't have to be recalculated on each display.
In index.html, we've got a snippet: Which does:
The embed tag goes out and looks for a template named /magic/{left}/{right}
The MagicCache looks for templates that have "magic" in the first part of the path and considers those templates to be pre-computed. If the precomputed value for the left and right (well... we should probably include the locale here) is not already cached, the default template is loaded and run as a snippet... the result is then cached. See MagicCache.scala
You'll see output in the console the first time a magic template is run... but not on subsequent loads of the given template.
Improvements to the code to meet your needs:
- Include the locale in the key for looking up pre-computed pages
- Store the precomputed pages someplace that's not in memory
- Don't block while computing the page value (this blocks all threads), but mark that a given key is work in progress
Question:
Many of our snippets/views are expensive to generate (we're doing cpu-bound work for each page).
Use case of caching many different *pages* per template. e.g. we have one lift template, and using url-rewriting it represents thousands of pages each with their own url.
Reply:
Examine LiftRules.templateCache. It is possible to create your own TemplateCache subclass that hits disk or Redis or an alternative caching backend.
If the particular template isn't found (e.g. cached_camera_foo_bar), you can use Lift's templating mechanism to calculate the tempate so that it doesn't have to be recalculated on each display.
In index.html, we've got a snippet: Which does:
object DoLeftRight {
def render =
(S.param("left"), S.param("right")) match {
case (Full(left), Full(right)) =>
case _ => <b>Missing left and right query parameters</b>
}
}
So if you've got left and right query params, the snippet returns an embed tag.The embed tag goes out and looks for a template named /magic/{left}/{right}
The MagicCache looks for templates that have "magic" in the first part of the path and considers those templates to be pre-computed. If the precomputed value for the left and right (well... we should probably include the locale here) is not already cached, the default template is loaded and run as a snippet... the result is then cached. See MagicCache.scala
You'll see output in the console the first time a magic template is run... but not on subsequent loads of the given template.
Improvements to the code to meet your needs:
- Include the locale in the key for looking up pre-computed pages
- Store the precomputed pages someplace that's not in memory
- Don't block while computing the page value (this blocks all threads), but mark that a given key is work in progress