lithium\storage\Cache::read PHP Méthode

read() public static méthode

Can handle single- and multi-key reads. Read-through caching can be used by passing expiry and the to-be-cached value in the write option. Following three ways to achieve this. Cache::read('default', 'foo', array( 'write' => array('+5 days' => 'bar') )); // returns 'bar' Cache::read('default', 'foo', array( 'write' => array('+5 days' => function() { return 'bar'; }) )); Cache::read('default', 'foo', array( 'write' => function() { return array('+5 days' => 'bar'); } ));
public static read ( string $name, mixed $key, array $options = [] ) : mixed
$name string Configuration to be used for reading.
$key mixed Key to uniquely identify the cache entry or an array of keys for multikey-reads.
$options array Options for the method and strategies. - `'write'`: Allows for read-through caching see description for usage. - `'strategies'` _boolean_: Indicates if strategies should be used, defaults to `true`. - `'conditions'` _mixed_: A function or item that must return or evaluate to `true` in order to continue write operation.
Résultat mixed For single-key reads will return the result if the cache key has been found otherwise returns `null`. When reading multiple keys a results array is returned mapping keys to retrieved values. Keys where the value couldn't successfully been read will not be contained in the results array.
    public static function read($name, $key, array $options = array())
    {
        $options += array('conditions' => null, 'strategies' => true, 'write' => null);
        if (is_callable($options['conditions']) && !$options['conditions']()) {
            return false;
        }
        try {
            $adapter = static::adapter($name);
        } catch (ConfigException $e) {
            return false;
        }
        $key = static::key($key);
        if ($isMulti = is_array($key)) {
            $keys = $key;
        } else {
            $keys = array($key);
        }
        $params = compact('keys');
        $results = static::_filter(__FUNCTION__, $params, function ($self, $params) use($adapter) {
            return $adapter->read($params['keys']);
        });
        if ($write = $options['write']) {
            $write = is_callable($write) ? $write() : $write;
            list($expiry, $value) = each($write);
            $value = is_callable($value) ? $value() : $value;
            foreach ($keys as $key) {
                if (isset($results[$key])) {
                    continue;
                }
                if (!static::write($name, $key, $value, $expiry)) {
                    return false;
                }
                $results[$key] = static::applyStrategies('write', $name, $value, array('key' => $key, 'mode' => 'LIFO', 'class' => __CLASS__));
            }
        }
        if ($options['strategies']) {
            foreach ($results as $key => &$result) {
                $result = static::applyStrategies(__FUNCTION__, $name, $result, array('key' => $key, 'mode' => 'LIFO', 'class' => __CLASS__));
            }
        }
        return $isMulti ? $results : ($results ? reset($results) : null);
    }

Usage Example

 public function testExpiresQuick()
 {
     Cache::write($this->cachedName, 'foo', 'bar', '+5 second');
     $this->assertEqual('bar', Cache::read($this->cachedName, 'foo'));
     sleep(10);
     $this->assertNull(Cache::read($this->cachedName, 'foo'));
 }
All Usage Examples Of lithium\storage\Cache::read