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);

// Set $data from cache, or build fresh $data

 if (($data = cache::get($my_cache_token, 900)) === null) {

 // Building fresh content    
   $data = '...';

 // Storing fresh content into cache
   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/lib_cache.inc.php.

## 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.

````php // Create cache identifier

 $my_cache_recorder_token = cache::token('my_unique_cache_name', ['dependency'], 'file', 3600);

// Start recording output / Or output recorded cached data no older than
900 seconds

 if (cache::capture($my_cache_recorder_token, 900)) {

   echo 'Some output'; //

Revisions

Top Editors
Recently Edited Articles