phalcon: 缓存片段,文件缓存,memcache缓存
生活随笔
收集整理的這篇文章主要介紹了
phalcon: 缓存片段,文件缓存,memcache缓存
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
幾種緩存,需要用到前端配置,加后端實(shí)例配合著用
片段緩存:
public function indexAction(){//渲染頁面$this->view->setTemplateAfter('common');//緩存片段//前端配置$frontcache = new \Phalcon\Cache\Frontend\Output(array("lifetime" => 86400));
//后端處理$cache = new \Phalcon\Cache\Backend\File($frontcache, array("cacheDir" => "../app/cache/"));//查詢片段是否存在$content = $cache->start("index.pthm");if ($content == null) {echo date("r");$this->view->pick("index/index");//緩存片段$cache->save();} else {echo $content;}}
文件緩存:
public function lastAction(){//數(shù)據(jù)緩存//前端配置$frontcache = new \Phalcon\Cache\Frontend\Data(array("lifetime"=>86400));//后端實(shí)例$cache = new \Phalcon\Cache\Backend\File($frontcache, array('cacheDir'=>'../app/cache/'));$cacheKey = 'customer_10';$customer = $cache->get($cacheKey);//在3600有效期內(nèi)讀取緩存// $customer = $cache->get($cacheKey, 3600);if($customer == null){$customer = \SysCustomer::find(array("limit"=>10))->toArray();//保存緩存$cache->save($cacheKey, $customer);//單獨(dú)設(shè)置緩存的有效期// $cache->save($cacheKey, $customer,3600);}var_dump($customer);}
注意:MainTask.php
如果你是命令行運(yùn)行MainTask.php,在用文件緩存的時(shí)候,如果設(shè)置的是cacheDIR='/',以windows為例,我的php盤是f:\,那么生成的緩存文件在 f:\ 根目錄下
?
?
?
?
?
?
memcache緩存,本例是windows下:
public function memcacheAction(){//memcache緩存//前端配置有效期$frontcache = new \Phalcon\Cache\Frontend\Data(array("lifetime"=>86400));//后端實(shí)例化$memcache = new \Phalcon\Cache\Backend\Memcache($frontcache, array("servers" => array(array("host" => "127.0.0.1","port" => "11211","weight" => "1"))));$mydata = $memcache->get('my-data');if($mydata == null) {$mydata = array(1,2,3,4,5);//保存緩存$memcache->save('my-data', $mydata);echo "--";}var_dump($mydata);}
?
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的phalcon: 缓存片段,文件缓存,memcache缓存的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: DOM 事件深入浅出(二)
- 下一篇: 安装pip