public function testDataFileCache() { $frontCache = new Phalcon\Cache\Frontend\Data(array('lifetime' => 10)); $cache = new Phalcon\Cache\Backend\File($frontCache, array('cacheDir' => 'unit-tests/cache/')); $this->assertFalse($cache->isStarted()); //Save $cache->save('test-data', "nothing interesting"); $this->assertTrue(file_exists('unit-tests/cache/' . $cache->getKey('test-data'))); //Get $cachedContent = $cache->get('test-data'); $this->assertEquals($cachedContent, "nothing interesting"); //Save $cache->save('test-data', "sure, nothing interesting"); //Get $cachedContent = $cache->get('test-data'); $this->assertEquals($cachedContent, "sure, nothing interesting"); //Exists $this->assertTrue($cache->exists('test-data')); //Delete $this->assertTrue($cache->delete('test-data')); // Save & Get : zero string, zero number, false $testSets = array('test-zero-data' => '0', 'test-0-data' => 0, 'test-false-data' => false); foreach ($testSets as $key => $value) { $cache->save($key, $value); $this->assertTrue(file_exists('unit-tests/cache/' . $cache->getKey($key))); $cachedContent = $cache->get($key); $this->assertEquals($cachedContent, $value); } }
public function testAction() { $frontCache = new Phalcon\Cache\Frontend\Data(array("lifetime" => 172800)); // Create the component that will cache "Data" to a "File" backend // Set the cache file directory - important to keep the "/" at the end of // of the value for the folder $cache = new Phalcon\Cache\Backend\File($frontCache, array("cacheDir" => "../cache/cachefile/")); // Try to get cached records $cacheKey = "questions.txt"; if ($cache->exists($cacheKey)) { $questions = $cache->get($cacheKey); foreach ($questions as $item) { echo $item->topic, "<br>"; } } else { $questions = Question::find(array("order" => "q_id")); // Store it in the cache $cache->save($cacheKey, $questions); echo "from db<br>"; foreach ($questions as $item) { echo $item->topic, "<br>"; } } }
public function testIgbinaryFileCache() { if (!$this->_prepareIgbinary()) { return false; } $frontCache = new Phalcon\Cache\Frontend\Igbinary(); $cache = new Phalcon\Cache\Backend\File($frontCache, array('cacheDir' => 'unit-tests/cache/')); $this->assertFalse($cache->isStarted()); //Save $cache->save('test-data', "nothing interesting"); $this->assertTrue(file_exists('unit-tests/cache/test-data')); //Get $cachedContent = $cache->get('test-data'); $this->assertEquals($cachedContent, "nothing interesting"); //Save $cache->save('test-data', "sure, nothing interesting"); //Get $cachedContent = $cache->get('test-data'); $this->assertEquals($cachedContent, "sure, nothing interesting"); //More complex save/get $data = array('null' => null, 'array' => array(1, 2, 3, 4 => 5), 'string', 123.45, 6, true, false, null, 0, ""); $serialized = igbinary_serialize($data); $this->assertEquals($data, igbinary_unserialize($serialized)); $cache->save('test-data', $data); $cachedContent = $cache->get('test-data'); $this->assertEquals($cachedContent, $data); //Exists $this->assertTrue($cache->exists('test-data')); //Delete $this->assertTrue($cache->delete('test-data')); }