1

In my project we display custom widgets on our customers pages. The widgets themselves do not change very often, so I feel view caching could be extremely useful here.

But every widget is different based on which company in our system is requesting it.

My question is using the cache helper...or any other method, can I cache the widget based on the company 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 */) );

    }
}

Is it possible to cache the index view based on the company id so that:

/widget/index/1

is served one copy from cache, but:

/widget/index/2

will get a different copy from the cache?

We are currently running on cake 2.3 and php5.3 we have plans to move to cake2.4 and php 5.5 if that would offer us any help.

Neil Holcomb
  • 518
  • 4
  • 11

1 Answers1

0

I would do something like this:

Controller:

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 */) );

}

In model:

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;
   }
}

Is this what you want?

Harris
  • 1,128
  • 4
  • 17
  • 41
  • I don't want to cache the modal layer...I want to cache the view layer. The view will be different for each company that access it, so the Cache Helper does not seem like it would work. Thats why I was hoping for a way to grab the out put of the view and cache it, with a key being the company ID...then next time through I could look for this cache and server that before I even invoke the model layer. – Neil Holcomb Jan 13 '14 at 22:54