Heavy expressions (i.e. refering to several thousands of series) have long run times to compute.
The @cached:ttl(,resetTtl,invalidate)
operator provides a means to store the results of expressions for a specified time to live (ttl
). This way, you may access results of heavy expressions without the computational overhead. The maximum ttl
is 24 hours.
The @cached
request stores and returns points based on the df/dt which was used.
Note that when relative dates like -1d or -5M are used for df/dt the requested date range will differ each time the request is made and the cached result may not be returned. Therefore, appending a 'b' to relative dates like -1db or -5Mb is recommended. See Date Input Notation for more info. To maximize the chances of the cache being used, use MIN/MAX for df/dt.
invalidate
:The expression is only run if it's not cached. This means if the result is within the ttl (time-to-live), it will not be re-run. To force a rerun, use the optional invalidate
keyword. This forces the invalidation of the values in the cache.
resetTtl
overrides the ttl from the current time. For example, calling @cached:20h
at 00:00 will cache until 20:00. Calling @cached:20h,resetTtl
at 01:00 will reset the ttl to 21:00.
Example usage:
- The first
@cached
run should include:ttl
in order to specify the lifetime of the cached result. - At 00:00
=x@cached
is run. This does not hit the cache because it has never been cached before. The expression is executed. - At 01:00
=x@cached:5h
is run. This caches until 06:00. The expression is executed. - At 01:10
=x@cached
is run. This retrieves from the cache (cache hit!), so the expression is not executed. - At 03:30
=x@cached:5h
is run. This retrieves from the cache (cache hit) and does not change thettl
.ttl
is still 06:00. - At 06:15
=x@cached:5h
is run. This re-executes (no cache hit) because we are beyond the ttl and it sets the new ttl to be 11:15. - At 10:00
=x@cached:5h,resetTtl
is run. This retrieves from the cache (cache hit) and moves the ttl to 15:00 without re-executing. - At 11:30
=x
is run. This does not even try to get the cached values. The expression is executed. - At 11:00
=x@cached:5h,invalidate
is run. This re-executes the expression and caches the new values because we used theinvalidate
flag but keeps the ttl the same (15:00). - At 12:00
=x@cached:5h,invalidate,resetTtl
is run. This re-executes because we used theinvalidate
flag and also moves the ttl to 17:00.