예제 #1
0
 public function initialize()
 {
     if (isset($_SERVER["HTTP_HOST"])) {
         $baseUrl = $_SERVER["HTTP_HOST"];
     } else {
         $baseUrl = "localhost";
     }
     if (!isset($this->environments[$baseUrl])) {
         $this->environments[$baseUrl] = self::DEVELOPMENT;
         //throw new ConfigException("Cant detect Url");
     }
     $this->mode = $this->environments[$baseUrl];
     $this->debug = $this->detectDebugMode();
     if (strcmp($this->mode, Configurator::DEVELOPMENT) == 0) {
         if (!$this->debug) {
             $this->debug = true;
         }
         $this->development = true;
         $this->production = false;
         $this->beta = false;
     } else {
         if (strcmp($this->mode, Configurator::PRODUCTION) == 0) {
             $this->development = false;
             $this->production = true;
             $this->beta = false;
         } else {
             if (strcmp($this->mode, Configurator::BETA) == 0) {
                 $this->development = false;
                 $this->production = false;
                 $this->beta = true;
             }
         }
     }
     if ($this->development && !$this->testCache) {
         $this->getConfig($this->configPath, $this->configTypes);
     } else {
         $frontCache = new \Phalcon\Cache\Frontend\Data(array("lifetime" => CACHE_MAX_LIFETIME));
         $cache = new \Phalcon\Cache\Backend\File($frontCache, array('cacheDir' => $this->cachePath));
         $this->config = $cache->get('config');
         if ($this->config === null) {
             $this->getConfig($this->configPath, $this->configTypes);
             $cache->save('config', $this->config);
         }
     }
 }
예제 #2
0
 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>";
         }
     }
 }
예제 #3
0
 public function _executeTestIssues2131($di)
 {
     $di->set('modelsCache', function () {
         $frontCache = new Phalcon\Cache\Frontend\Data();
         $modelsCache = new Phalcon\Cache\Backend\File($frontCache, array('cacheDir' => 'unit-tests/cache/'));
         $modelsCache->delete("cache-2131");
         return $modelsCache;
     }, true);
     $personas = Personas::query()->where("estado='I'")->cache(array("key" => "cache-2131"))->execute();
     $this->assertTrue($personas->isFresh());
     $personas = Personas::query()->where("estado='I'")->cache(array("key" => "cache-2131"))->execute();
     $this->assertFalse($personas->isFresh());
 }
<?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;
}
예제 #5
0
 public function testCacheFileFlush()
 {
     $frontCache = new Phalcon\Cache\Frontend\Data(array('lifetime' => 10));
     // File
     $cache = new Phalcon\Cache\Backend\File($frontCache, array('cacheDir' => 'unit-tests/cache/'));
     $cache->save('data', "1");
     $cache->save('data2', "2");
     $this->assertTrue($cache->flush());
     $this->assertFalse(file_exists('unit-tests/cache/data'));
     $this->assertFalse(file_exists('unit-tests/cache/data2'));
 }
예제 #6
0
 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'));
 }
<?php

// Cache the files for 2 days using a Data frontend
$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" => "../app/cache/"));
// Try to get cached records
$cacheKey = 'robots_order_id.cache';
$robots = $cache->get($cacheKey);
if ($robots === null) {
    // $robots is null due to cache expiration or data does not exist
    // Make the database call and populate the variable
    $robots = Robots::find(array("order" => "id"));
    // Store it in the cache
    $cache->save($cacheKey, $robots);
}
// Use $robots :)
foreach ($robots as $robot) {
    echo $robot->name, "\n";
}
예제 #8
0
 /**
  * 获取token
  */
 protected function getToken()
 {
     $weixin = new \Weixin();
     //检查基础token是否存在
     $frontCache = new \Phalcon\Cache\Frontend\data(array("lifetime" => 5400));
     $cache = new \Phalcon\Cache\Backend\File($frontCache, array("cacheDir" => __DIR__ . "/../cache/"));
     //检查基础token是否存在
     $basic_token = $cache->get('basic_token');
     if (!$basic_token) {
         $basic = json_decode($weixin->basicToken(), true);
         $cache->save('basic_token', $basic['access_token']);
         $basic_token = $basic['access_token'];
     }
     return $basic_token;
 }
<?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;
}