Cachearium\CacheAbstract::startCallback PHP Method

startCallback() public method

start() using a callable. Same as start()/c()/end().
public startCallback ( CacheKey $k, callable $c, array $cparams = [], integer $lifetime = null )
$k CacheKey
$c callable A callable. Whatever it prints will be cached.
$cparams array parameters for the callback, optional
$lifetime integer
    public function startCallback(CacheKey $k, callable $c, array $cparams = [], $lifetime = null)
    {
        $data = $this->start($k, $lifetime);
        if ($data === false) {
            call_user_func_array($c, $cparams);
            $data = $this->end(false);
        }
        return $data;
    }

Usage Example

 protected function _callback(CacheAbstract $cache)
 {
     $base = 'callback';
     $key1 = new CacheKey($base, 1);
     $cache->clean($key1);
     $this->assertEquals(CALLBACKVALUE, $cache->startCallback($key1, 'callbackTester'));
     try {
         $data = $cache->getData($key1);
         $this->assertEquals(CALLBACKVALUE, $data->stringify($cache));
     } catch (Cachearium\Exceptions\NotCachedException $e) {
         $this->fail();
     }
 }