cache. Caching results of code's work

^cache[file]
^cache[file](number of seconds){code}
^cache[file](number of seconds){code}
{error handler}
^cache[file][expiration date]{code}
^cache[file][expiration date]{code}
{error handler}
^cache[
] = expiration date   [3.1.5]

Operator
cache caches the string resulted from code's work. Subsequent calls then do not re-execute the code, but only output cached result. It saves time and servers' resources during request processing.

We do recommend you to link modules (
^use[…]) from within the code of operator cache instead of doing it statically (@USE).
We also strongly recommend that you work also with DB (
^connect[…]) within cache when possible, to save your SQL-server's resources and increase your sites' productivity.

File is a name of cache-file. If this file exists and is not expired, its content will be sent to the client. If it doesn't exist, the code will be executed and result will be saved in the file with specified name.

Number of seconds is time to store result of the code's work, given in seconds. If the number is zero, the result is not saved and the file with previously cached result is deleted.

Expiration date is date and time, until which result of the code's work is considered valid. If the date is in the past, the result is not saved and the file with previously cached result is deleted.

Code is the code, whose result is to be cached.

Error handler-here the error in code can be handled. In this respect the operator resembles try, see section "Error handling". Unlike try, $exception.handled[cache] can be specified, which gives Parser the command to handle the error in a special way: to get from file the expired content, earlier saved result of code's work, ignoring the fact that the content has expired.

The cached file can be deleted by
^cache[file]

It is possible to use within the code commands to change the time for the result of the code's work to be stored:
^cache(number of seconds)
^cache[expiration date]

Minimum time for the code to be stored is used.

Current expiration date can be learned by
$expire_date[^cache[]]

Example
^cache[/data/cache/test1](5){
   Press 'reload', changes every 5 seconds:^math:random(100)
}

Changing expiration time
^cache[/data/cache/test2](5){
   Within cache code you found out 
   that the page shouldn't be cached: 
^cache(0)
}



Copyright © 1997–2021 Art. Lebedev Studio | http://www.artlebedev.com Last updated: 29.12.2016