lithium\storage\cache\adapter\Redis::write PHP Method

write() public method

Write values to the cache. All items to be cached will receive an expiration time of $expiry.
public write ( array $keys, string | integer $expiry = null ) : boolean
$keys array Key/value pairs with keys to uniquely identify the to-be-cached item.
$expiry string | integer A `strtotime()` compatible cache time or TTL in seconds. To persist an item use `\lithium\storage\Cache::PERSIST`.
return boolean `true` on successful write, `false` otherwise.
    public function write(array $keys, $expiry = null)
    {
        $expiry = $expiry || $expiry === Cache::PERSIST ? $expiry : $this->_config['expiry'];
        if (!$expiry || $expiry === Cache::PERSIST) {
            $ttl = null;
        } elseif (is_int($expiry)) {
            $ttl = $expiry;
        } else {
            $ttl = strtotime($expiry) - time();
        }
        if (count($keys) > 1) {
            if (!$ttl) {
                return $this->connection->mset($keys);
            }
            $transaction = $this->connection->multi();
            foreach ($keys as $key => $value) {
                if (!$this->connection->setex($key, $ttl, $value)) {
                    $transaction->discard();
                    return false;
                }
            }
            return $transaction->exec() === array_fill(0, count($keys), true);
        }
        $key = key($keys);
        $value = current($keys);
        if (!$ttl) {
            return $this->connection->set($key, $value);
        }
        return $this->connection->setex($key, $ttl, $value);
    }

Usage Example

Example #1
0
 public function testWriteWithScope()
 {
     $adapter = new Redis(array('scope' => 'primary'));
     $keys = array('key1' => 'test1');
     $expiry = '+1 minute';
     $adapter->write($keys, $expiry);
     $expected = 'test1';
     $result = $this->_redis->get('primary:key1');
     $this->assertEqual($expected, $result);
     $result = $this->_redis->get('key1');
     $this->assertFalse($result);
 }
All Usage Examples Of lithium\storage\cache\adapter\Redis::write