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) {
$expires = 0;
} elseif (is_int($expiry)) {
$expires = $expiry + time();
} else {
$expires = strtotime($expiry);
}
if (count($keys) > 1) {
return $this->connection->setMulti($keys, $expires);
}
return $this->connection->set(key($keys), current($keys), $expires);
}
public function testWriteDefaultCacheExpiry() { $memcache = new Memcache(array('expiry' => '+5 seconds')); $key = 'default_key'; $data = 'value'; $closure = $memcache->write($key, $data); $this->assertTrue(is_callable($closure)); $params = compact('key', 'data'); $result = $closure($memcache, $params); $expected = $data; $this->assertEqual($expected, $result); $result = $this->_conn->get($key); $this->assertEqual($expected, $result); $result = $this->_conn->delete($key); $this->assertTrue($result); }