-
Notifications
You must be signed in to change notification settings - Fork 37
HOW TO Use cache to improve performances
Plomino provides a cache mechanism.
If your Plomino application contains some time consumming formulas, you can speed up the page display using the cache API provided by Plomino.
Here is an example:
result = db.getCache('my_cache_key')
if not result:
result = make_something_which_cost_CPU(stuff)
db.setCache('my_cache_key', result)
return result
The first time the formula will be called, the make_something_which_cost_CPU
function will executed, and the result will be put into the cache.
Next time the formula is called, the result is directly read from the cache.
As the cache key is a constant (my_cache_key
), it will be the same in all the cases (for all the users, in all the pages, etc.).
But of course, the make_something_which_cost_CPU
function might return a different value depending on the context. If so, you need the produce a cache key that will reflect this context accurately.
For instance, if the result is different according the user, an accurate cache key could be:
cache_key = "result_for_"+context.getCurrentUser().getMemberId()
or depending on the document:
cache_key = "result_for_"+context.id
or anything you might need.
Another use case is the repeated usage of a same formula in the same page: sometimes, when rendering a document using a form, several computed fields make the same computing (typical example: you display a table of values, and also a bar chart based on those values).
The code itself can be factorized using a script library in the /resources
folder, but it will be run twice anyway when rendering the page.
And it might impact the performances.
Unfortunately, setCache and getCache might not be relevant because you want the formula to be re-evaluated everytime a user displays the page. In that case, you can use setRequestCache
and getRequestCache
, so the cache will be associated with the current request, and will only last as long as the request will:
result = db.getRequestCache('my_cache_key')
if not result:
result = make_something_which_cost_CPU(stuff)
db.setRequestCache('my_cache_key', result)