Home | 簡體中文 | 繁體中文 | 雜文 | 打賞(Donations) | ITEYE 博客 | OSChina 博客 | Facebook | Linkedin | 知乎專欄 | Search | Email

9.4. cache

9.4.1. Redis 緩存

配置 Redis 緩存

			
	//Set the models cache service
	$di->set('cache', function() use ($config) {

		//Cache data for one day by default
		$frontCache = new \Phalcon\Cache\Frontend\Data(array(
				"lifetime" => 86400
		));

		//Create the Cache setting redis connection options
		$cache = new Phalcon\Cache\Backend\Redis($frontCache, array(
				'host' => $config->redis->host,
				'port' => $config->redis->port,
				'auth' => $config->redis->auth,
				'persistent' => true,
				/*'statsKey' => 'info',*/
				'index' => 1 /*選擇redis資料庫*/

		));

		return $cache;
	});
			
			

9.4.2. 多種緩存混合使用

多個緩存同事使用,我們借助 $cache = new stdClass(); 將多種緩存 $cache->redis 與 $cache->file 同時返回

			
	//Set the models cache service
	$di->set('cache', function() use ($config) {

		//Cache data for one day by default
		$frontCache = new \Phalcon\Cache\Frontend\Data(array(
				"lifetime" => 86400
		));

		//Create the Cache setting redis connection options
		$redis = new Phalcon\Cache\Backend\Redis($frontCache, array(
				'host' => $config->redis->host,
				'port' => $config->redis->port,
// 				'auth' => $config->redis->auth,
//				'persistent' => true,
// 				'statsKey' => 'info',
				'index' => 1

		));

		$frontCache = new \Phalcon\Cache\Frontend\Data(array(
				"lifetime" => 86400
		));
		$file = new \Phalcon\Cache\Backend\File($frontCache, array(
				"cacheDir" => "../app/cache/"
		));

		$cache = new stdClass();
		$cache->redis = $redis;
		$cache->file = $file;
		//$cache->mem = $mem;

		return $cache;
	});