User Tools

Site Tools


how_to_use_cache

How To Use Cache

Basic Example

  $my_cache_token = cache::token('my_unique_cache_name', array('dependency'), 'file', 3600);
  if (($data = cache::get($my_cache_token, 900)) === null) {
      
    $data = '...';
    
    cache::set($my_cache_token, $data);
  }
  
  var_dump($data);

The number 3600 tells how many seconds the content should be cached.

The number 900 tells the maximum allowed age of returned cache.

Dependency parameters will distinguish different cache content by the same id in relation to the environment. For e.g. currency cache will vary in relation to the selected currency, language vary in relation to the selected language, and so on. A list of these depenecies can be found in library/lib_cache.inc.php.

Output Buffer Recorder Example

  $my_cache_recorder_token = cache::token('my_unique_cache_name', array('dependency'), 'file', 3600);
  if (cache::capture($my_cache_recorder_token, 900)) {
  
    echo 'Some output';
  
    cache::end_capture($my_cache_recorder_token);
  }

Please note only the output buffer will be recorded, not any variables set inside.

how_to_use_cache.txt · Last modified: 2020/08/17 10:27 by A User Not Logged in