예제 #1
0
 public function ytestOutputFileCache()
 {
     for ($i = 0; $i < 2; $i++) {
         $time = date('H:i:s');
         $frontCache = new Phalcon\Cache\Frontend\Output(array('lifetime' => 2));
         $cache = new Phalcon\Cache\Backend\File($frontCache, array('cacheDir' => 'unit-tests/cache/', 'prefix' => 'unit'));
         // on the second run set useSafeKey to true to test the compatibility toggle
         if ($i == 1) {
             $cache->useSafeKey(true);
         }
         $this->assertFalse($cache->isStarted());
         ob_start();
         //First time cache
         $content = $cache->start('testoutput');
         $this->assertTrue($cache->isStarted());
         if ($content !== null) {
             $this->assertTrue(false);
         }
         echo $time;
         $cache->save(null, null, null, true);
         $obContent = ob_get_contents();
         ob_end_clean();
         $this->assertEquals($time, $obContent);
         $this->assertTrue(file_exists('unit-tests/cache/unit' . $cache->getKey('testoutput')));
         //Same cache
         $content = $cache->start('testoutput');
         $this->assertTrue($cache->isStarted());
         if ($content === null) {
             $this->assertTrue(false);
         }
         $this->assertEquals($time, $obContent);
         //Refresh cache
         sleep(3);
         $time2 = date('H:i:s');
         ob_start();
         $content = $cache->start('testoutput');
         $this->assertTrue($cache->isStarted());
         if ($content !== null) {
             $this->assertTrue(false);
         }
         echo $time2;
         $cache->save(null, null, null, true);
         $obContent2 = ob_get_contents();
         ob_end_clean();
         $this->assertNotEquals($time, $obContent2);
         $this->assertEquals($time2, $obContent2);
         //Check keys
         $keys = $cache->queryKeys();
         $this->assertEquals($keys, array(0 => 'unit' . $cache->getKey('testoutput')));
         $this->assertTrue($cache->exists('testoutput'));
         //Delete cache
         $this->assertTrue($cache->delete('testoutput'));
     }
 }
예제 #2
0
 public function testOutputFileCache()
 {
     $time = date('H:i:s');
     $frontCache = new Phalcon\Cache\Frontend\Output(array('lifetime' => 2));
     $cache = new Phalcon\Cache\Backend\File($frontCache, array('cacheDir' => 'unit-tests/cache/', 'prefix' => 'unit'));
     $this->assertFalse($cache->isStarted());
     ob_start();
     //First time cache
     $content = $cache->start('testoutput');
     $this->assertTrue($cache->isStarted());
     if ($content !== null) {
         $this->assertTrue(false);
     }
     echo $time;
     $cache->save(null, null, null, true);
     $obContent = ob_get_contents();
     ob_end_clean();
     $this->assertEquals($time, $obContent);
     $this->assertTrue(file_exists('unit-tests/cache/unittestoutput'));
     //Same cache
     $content = $cache->start('testoutput');
     $this->assertTrue($cache->isStarted());
     if ($content === null) {
         $this->assertTrue(false);
     }
     $this->assertEquals($time, $obContent);
     //Refresh cache
     sleep(3);
     $time2 = date('H:i:s');
     ob_start();
     $content = $cache->start('testoutput');
     $this->assertTrue($cache->isStarted());
     if ($content !== null) {
         $this->assertTrue(false);
     }
     echo $time2;
     $cache->save(null, null, null, true);
     $obContent2 = ob_get_contents();
     ob_end_clean();
     $this->assertNotEquals($time, $obContent2);
     $this->assertEquals($time2, $obContent2);
     //Check keys
     $keys = $cache->queryKeys();
     $this->assertEquals($keys, array(0 => 'unittestoutput'));
     // $cache->exists('testoutput') is not always true because Travis CI could be slow sometimes
     //Exists?
     if ($cache->exists('testoutput')) {
         $this->assertTrue($cache->exists('testoutput'));
         //Delete cache
         $this->assertTrue($cache->delete('testoutput'));
     }
 }
<?php

//Create an Output frontend. Cache the files for 2 days
$frontCache = new Phalcon\Cache\Frontend\Output(array("lifetime" => 172800));
// Create the component that will cache from the "Output" to a "File" backend
// Set the cache file directory - it's important to keep the "/" at the end of
// the value for the folder
$cache = new Phalcon\Cache\Backend\File($frontCache, array("cacheDir" => "../app/cache/"));
// Get/Set the cache file to ../app/cache/my-cache.html
$content = $cache->start("my-cache.html");
// If $content is null then the content will be generated for the cache
if ($content === null) {
    //Print date and time
    echo date("r");
    //Generate a link to the sign-up action
    echo Phalcon\Tag::linkTo(array("user/signup", "Sign Up", "class" => "signup-button"));
    // Store the output into the cache file
    $cache->save();
} else {
    // Echo the cached output
    echo $content;
}
<?php

//Cache the file for 2 days
$frontendOptions = array('lifetime' => 172800);
//Create a output cache
$frontCache = \Phalcon\Cache\Frontend\Output($frontOptions);
//Set the cache directory
$backendOptions = array('cacheDir' => '../app/cache/');
//Create the File backend
$cache = new \Phalcon\Cache\Backend\File($frontCache, $backendOptions);
$content = $cache->start('my-cache');
if ($content === null) {
    echo '<h1>', time(), '</h1>';
    $cache->save();
} else {
    echo $content;
}