Solar_Cache_Adapter_Memcache::fetchOrAdd()

public mixed Solar_Cache_Adapter_Memcache::fetchOrAdd ( 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 adds it to the cache in a race-condition-safe way.

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

  • (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 adds it to the cache in a race-condition-safe way.

<?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 adding 
// if it does not exist.
$callback = array('Solar_Example', 'createData');
$args = array($id);
$data = $cache->fetchOrAdd($id, $callback, $args);

See Also



Local