Phalcon\Cache\Backend\Aerospike::save PHP Method

save() public method

public save ( integer | string $keyName = null, string $content = null, integer $lifetime = null, boolean $stopBuffer = true ) : boolean
$keyName integer | string
$content string
$lifetime integer
$stopBuffer boolean
return boolean
    public function save($keyName = null, $content = null, $lifetime = null, $stopBuffer = true)
    {
        if ($keyName === null) {
            $prefixedKey = $this->_lastKey;
        } else {
            $prefixedKey = $this->getPrefixedIdentifier($keyName);
        }
        if (!$prefixedKey) {
            throw new Exception('The cache must be started first');
        }
        if (null === $content) {
            $cachedContent = $this->_frontend->getContent();
        } else {
            $cachedContent = $content;
        }
        if (null === $lifetime) {
            $lifetime = $this->_lastLifetime;
            if (null === $lifetime) {
                $lifetime = $this->_frontend->getLifetime();
            }
        }
        $aKey = $this->buildKey($prefixedKey);
        $bins['value'] = $cachedContent;
        $status = $this->db->put($aKey, $bins, $lifetime, [\Aerospike::OPT_POLICY_KEY => \Aerospike::POLICY_KEY_SEND]);
        if (\Aerospike::OK != $status) {
            throw new Exception(sprintf('Failed storing data in Aerospike: %s', $this->db->error()), $this->db->errorno());
        }
        if (true === $stopBuffer) {
            $this->_frontend->stop();
        }
        if (true === $this->_frontend->isBuffering()) {
            echo $cachedContent;
        }
        $this->_started = false;
        return \Aerospike::OK == $status;
    }

Usage Example

Example #1
0
 public function testShouldUseOutputFrontend()
 {
     $time = date('H:i:s');
     $frontCache = new CacheOutput(['lifetime' => 10]);
     $cache = new CacheAerospike($frontCache, $this->getConfig());
     ob_start();
     $content = $cache->start('test-output');
     $this->keys[] = 'test-output';
     $this->assertNull($content);
     echo $time;
     $obContent = ob_get_contents();
     $cache->save(null, null, null, true);
     ob_end_clean();
     $this->assertEquals($time, $obContent);
     $this->assertEquals($time, $cache->get('test-output'));
     $content = $cache->start('test-output');
     $this->assertEquals($content, $obContent);
     $this->assertEquals($content, $cache->get('test-output'));
     $keys = $cache->queryKeys();
     $this->assertEquals([0 => 'test-output'], $keys);
 }
All Usage Examples Of Phalcon\Cache\Backend\Aerospike::save