public function testDataFileCacheIncrement() { $frontCache = new Phalcon\Cache\Frontend\Data(); $cache = new Phalcon\Cache\Backend\File($frontCache, array('cacheDir' => 'unit-tests/cache/')); $cache->delete('foo'); $cache->save('foo', "1"); $this->assertEquals(2, $cache->increment('foo')); $this->assertEquals($cache->get('foo'), 2); $this->assertEquals($cache->increment('foo', 5), 7); }
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); } } }
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')); }
<?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"; }
/** * 获取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; }