Solar_Cache_Adapter_File::fetchOrSave()
public
mixed
Solar_Cache_Adapter_File::fetchOrSave
( string $key
, callback $callback
, array $args = array ()
, int $life = NULL
)
Fetches data if it exists; if not, uses a callback to create the data and saves it to the cache.
Inherited from Solar_Cache_Adapter.
Parameters
(string)
$key
: The entry ID.(callback)
$callback
: A PHP callback to use if the data needs to be created for saving.(array)
$args
: Arguments to the callback, if any.(int)
$life
: A custom lifespan, in seconds, for the entry; if null, uses the default lifespan for the adapter instance.
Returns
(mixed) The fetched or created data.
Description
Fetches data if it exists; if not, uses a callback to create the data and saves it to the cache.
<?php
// create a request object
$request = Solar_Registry::get('request');
// create an entry ID named for the current URI
$id = $request->server('REQUEST_URI');
// create a cache object
$cache = Solar::factory('Solar_Cache');
// fetch that ID from the cache, but use a static method callback
// Solar_Example::createData($id) to create the data for saving
// if it does not exist.
$callback = array('Solar_Example', 'createData');
$args = array($id);
$data = $cache->fetchOrSave($id, $callback, $args);