lithium\storage\cache\adapter\File::write PHP Метод

write() публичный Метод

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`.
Результат 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 ($this->_config['scope']) {
            $keys = $this->_addScopePrefix($this->_config['scope'], $keys, '_');
        }
        foreach ($keys as $key => $value) {
            if (!$this->_write($key, $value, $expires)) {
                return false;
            }
        }
        return true;
    }

Usage Example

Пример #1
0
 public function testWriteDefaultCacheExpiry()
 {
     $file = new File(array('expiry' => '+1 minute'));
     $key = 'default_keykey';
     $data = 'data';
     $time = time() + 60;
     $closure = $file->write($key, $data);
     $this->assertTrue(is_callable($closure));
     $params = compact('key', 'data');
     $result = $closure($file, $params, null);
     $expected = 25;
     $this->assertEqual($expected, $result);
     $this->assertTrue(file_exists(Libraries::get(true, 'resources') . "/tmp/cache/{$key}"));
     $this->assertEqual(file_get_contents(Libraries::get(true, 'resources') . "/tmp/cache/{$key}"), "{:expiry:{$time}}\ndata");
     $this->assertTrue(unlink(Libraries::get(true, 'resources') . "/tmp/cache/{$key}"));
     $this->assertFalse(file_exists(Libraries::get(true, 'resources') . "/tmp/cache/{$key}"));
 }
All Usage Examples Of lithium\storage\cache\adapter\File::write