Usage

Once you have instantiated the cache, you can use it to store and retrieve data that takes more resources to generate than a cache call does.

In the following example, we simulate caching the results of a database call or other resource-intensive task. If the cache entry does not exist, we generate the data and save it in the cache; if it does exist, we use that instead (thus speeding up the script execution).

<?php
require_once 'Solar.php';
Solar::start('/path/to/config/Solar.config.php');

// get the request environment (automatically registered when you
// call the Solar::start() method)
$request = Solar_Registry::get('request');

// every cache entry needs a unique ID; we'll assume that ID
// is passed as part of the URL.
$id = $request->get('id');

// connect to the cache
$cache = Solar::factory('Solar_Cache');

// try to get the cache entry.  if the entry is past its lifetime,
// this will additonally delete the entry for us, keeping the cache
// clean.
$output = $cache->fetch($id);

// did we get it?
if (! $output) {
    // no output stored in the cache under that ID; build it.
    // assume we connect to a database and transform the data in 
    // some way.
    $output = 'html built from database results';

    // save the output in the cache, replacing anything that
    // may have been in that entry before.
    $cache->save($id, $output);
}

// now we have the output, whether from the cache or from generating it.
// we can output it or do whatever else we need.
echo $output;
?>