User Tools

Site Tools


How To Use Cache

The LiteCart framework provides two simple ways to store cache. Either by storing variable data or recording output data.

Storing Variable Data

Here is an example for storing and fetching variable data from cache.

Make note if get() returns null then no cached data was returned and we need to generate the data.

  $my_cache_token = cache::token('my_unique_cache_name', ['dependency'], 'file', 3600);
  if (($data = cache::get($my_cache_token, 900)) === null) {
    $data = '...';
    cache::set($my_cache_token, $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/

Recording Output Data

This is an example for recording the output or conditionally output cached data.

If capture() returnes true, then a recorder was started to capture anything output inside the condition. If it returns false, then cached data has been output and anything inside the condition will be skipped.

  $my_cache_recorder_token = cache::token('my_unique_cache_name', ['dependency'], 'file', 3600);
  if (cache::capture($my_cache_recorder_token, 900)) {
    echo 'Some output'; // <--- This will be recorded

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

Predefined Dependencies

See the file for a list of predefined dependencies that will make your cached data unique to the given dependencies. E.g. country, currency, language, domain, etc.

how_to_use_cache.txt · Last modified: 2021/11/30 04:44 by tim