phalcon: 缓存片段,文件缓存,memcache缓存

几种缓存,需要用到前端配置,加后端实例配合着用

片段缓存:

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()
    {
        //数据缓存
        //前端配置
        $frontcache = new \Phalcon\Cache\Frontend\Data(array(
            "lifetime"=>86400
        ));
        //后端实例
        $cache = new \Phalcon\Cache\Backend\File($frontcache, array(
            'cacheDir'=>'../app/cache/'
        ));
        $cacheKey = 'customer_10';
        $customer = $cache->get($cacheKey);
        //在3600有效期内读取缓存
       // $customer = $cache->get($cacheKey, 3600);
        if($customer == null)
        {
            $customer = \SysCustomer::find(array(
                "limit"=>10
            ))->toArray();
            //保存缓存
            $cache->save($cacheKey, $customer);
            //单独设置缓存的有效期
            // $cache->save($cacheKey, $customer,3600);
        }
        var_dump($customer);
    }

  

注意:MainTask.php

如果你是命令行运行MainTask.php,在用文件缓存的时候,如果设置的是cacheDIR='/',以windows为例,我的php盘是f:\,那么生成的缓存文件在 f:\ 根目录下

 

 

 

 

 

 

memcache缓存,本例是windows下:

public function memcacheAction()
    {
        //memcache缓存
        //前端配置有效期
        $frontcache = new \Phalcon\Cache\Frontend\Data(array(
            "lifetime"=>86400
        ));
        //后端实例化
        $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);





    }

  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值