1

在我的项目中,我们在客户页面上显示自定义小部件。小部件本身不会经常更改,所以我觉得视图缓存在这里非常有用。

但是每个小部件都是不同的,具体取决于我们系统中的哪个公司请求它。

我的问题是使用缓存助手...或任何其他方法,我可以根据公司 ID 缓存小部件吗?

<?php
App::uses('AppController', 'Controller');

class widgetController extends AppController {


    public $helpers = array( 'Cache' );

    public $cacheAction = array(
        'iframeForm' => 3600,
    );

    public $uses = array('Company');

    public function index( $company_id ) {

        //... Load up a ton of data

        $this->layout = 'widget';

        $this->set( compact(/* Set a ton of data */) );

    }
}

是否可以根据公司 ID 缓存索引视图,以便:

/widget/index/1

从缓存中提供一份副本,但是:

/widget/index/2

会从缓存中得到不同的副本吗?

我们目前在 cake 2.3 和 php5.3 上运行,如果这能为我们提供任何帮助,我们计划迁移到 cake2.4 和 php 5.5。

4

1 回答 1

0

我会做这样的事情:

控制器:

public function index( $company_id ) {

    //... Load up a ton of data
    $this->Model->getStuff($company_id);

    $this->layout = 'widget';

    $this->set( compact(/* Set a ton of data */) );

}

在模型中:

public function getStuff( $company_id ) {
    if(($modelData = Cache::read('modelDataCompanyID_'. $company_id)) == null)
    {
      $modelData = $this->find('all',array('conditions' => 
          array('Model.company_id' => $company_id)));
      Cache::write('modelDataCompanyID_'. $company_id, $modelData);
    }
    return $modeData;
   }
}

这是你想要的吗?

于 2014-01-13T18:38:52.843 回答