Cachearium\CacheAbstract::store PHP Method

store() abstract public method

Saves data in cache.
abstract public store ( mixed $data, CacheKey $k, integer $lifetime ) : boolean
$data mixed Data to save in cache
$k CacheKey
$lifetime integer The lifetime in sceonds, although it is up to the implementation whether it is respected or not.
return boolean true if no problem
    public abstract function store($data, CacheKey $k, $lifetime = 0);

Usage Example

 private function setGetClean(CacheAbstract $cache)
 {
     $base = 'base';
     // enable
     $this->assertTrue($cache->isEnabled());
     $cache->setEnabled(false);
     $this->assertFalse($cache->isEnabled());
     $cache->setEnabled(true);
     $this->assertTrue($cache->isEnabled());
     $cache->disable();
     $this->assertFalse($cache->isEnabled());
     $cache->enable();
     $this->assertTrue($cache->isEnabled());
     $cache->setDefaultLifetime(3600);
     $this->assertEquals(3600, $cache->getDefaultLifetime());
     $key1 = new CacheKey($base, 1);
     $cache->clean($key1);
     try {
         $data = $cache->get($key1);
         $this->fail();
     } catch (Cachearium\Exceptions\NotCachedException $e) {
         $this->assertTrue(true);
     }
     $retval = $cache->store(234, $key1);
     $this->assertTrue($retval);
     try {
         $data = $cache->get($key1);
         $this->assertEquals(234, $data);
     } catch (Cachearium\Exceptions\NotCachedException $e) {
         $this->fail();
     }
     // sleep(1);
     try {
         $data = $cache->get($key1);
         $this->assertEquals(234, $data);
     } catch (Cachearium\Exceptions\NotCachedException $e) {
         $this->fail();
     }
     $cache->clean($key1);
     try {
         $data = $cache->get($key1);
         $this->fail();
     } catch (Cachearium\Exceptions\NotCachedException $e) {
         $this->assertTrue(true);
     }
     $key2 = new CacheKey($base, 2, 'a');
     $key3 = new CacheKey($base, 3, 'a');
     // now change again and delete
     $retval = $cache->store(234, $key2);
     $this->assertEquals(true, $retval);
     try {
         $data = $cache->get($key2);
         $this->assertEquals(234, $data);
     } catch (Cachearium\Exceptions\NotCachedException $e) {
         $this->fail();
     }
     $this->assertTrue($cache->delete($key2));
     // test null
     $retval = $cache->store(null, $key3);
     $this->assertEquals(true, $retval);
     try {
         $data = $cache->get($key3);
         $this->assertEquals(null, $data);
     } catch (Cachearium\Exceptions\NotCachedException $e) {
         $this->fail();
     }
     $this->assertTrue($cache->delete($key3));
     $this->assertArrayHasKey(CacheLogEnum::ACCESSED, $cache->getLogSummary());
     $this->assertGreaterThan(0, $cache->getLogSummary()[CacheLogEnum::ACCESSED]);
 }