write() public méthode

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`.
Résultat boolean `true` on successful write, `false` otherwise.
 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}"));
 }
Exemple #2
0
 public function testWriteWithScope()
 {
     $now = time();
     $adapter = new File(array('scope' => 'primary'));
     $time = $now + 5;
     $expiry = 5;
     $keys = array('key1' => 'test1');
     $adapter->write($keys, $expiry);
     $file = Libraries::get(true, 'resources') . '/tmp/cache/primary_key1';
     $expected = "{:expiry:{$time}}\ntest1";
     $result = file_get_contents($file);
     $this->assertEqual($expected, $result);
 }