Igor Savinkin
Igor Savinkin

Reputation: 6277

Caching in yii for goods site

But i'm new to the caching technique. After reading docs (and following), and some examples i still of little clue how to use it.

I need to apply caching for the CMS/CRM site we developed. The application/website is a mix of web shop and client order processing, how to make it optimized? What cache components is optimal to use?

The typical user request (the main concern for caching) would be to assortment/goods grid page with ability to filter goods by category, make, manufacturer, article and so on.

My concept is that it's not possible to predict each user's behaviour as to what kind of options/filters he'll impose on given data. So how could caching be applied? Please, correct me if it's not so. Users might be logged in or not.

I've set in main.php the cache setting in components:

'components'=>array(
    'cache'=>array(
        'class'=>'system.caching.COutputCache',
             'connectionID'=>'db',
             'autoCreateCacheTable'=>false,
             'cacheTableName'=>'cache',
            ),
 ...

I've added caching setting into filters() of AssortmentController:

public function filters()
{
    return array(
        'accessControl', // perform access control for CRUD operations
        'postOnly + delete', // we only allow deletion via POST request
        array(
            'COutputCache',
            'duration'=>1000,
            'varyByParam'=>array('id'),
        ),
    );
} 
  1. Am i missing something apart from these two pieces of code? Should i use beginCache and endCache somewhere in view file or in layout file, where if so?
  2. But suppose i want to use also CFileCache? Should/could i define it in components/cache array along with COutputCache that is already defined?
  3. In this docs COutputCache is not mentioned, how to properly configure it?
  4. But still there might be more GET parameters (apart from id) like Assortment[title], Assortment[article] , Assortment[groupCategory]? Should i add them as well?

    'varyByParam'=>array('id', 'Assortment[title]', 'Assortment[article]' , 'Assortment[groupCategory]')

Any corrections, docs' links, guidelines would be appreciable.

Upvotes: 0

Views: 145

Answers (1)

sabkaraja
sabkaraja

Reputation: 342

  1. You dont need to do anything additional to begin caching. From the question, it looks like you are using DbCaching - so you should be able to see records in the cache table. BeginCache/EndCache can be helpful if you want to cache only parts of the view / output.

To create cache for particular views add this: 'COutputCache + view', (this means only 'view' action will be cached)

  1. You may use multiple Cache either by defining a cacheId and referring it to it in beginCache $this->beginCache('name', array('cacheID' => 'filecache')).

  2. COutputCache is referred here (http://www.yiiframework.com/doc/api/1.1/COutputCache).

  3. Only the params inside the varyByParams will be considered for cache key. /products/view/1/some/param AND /products/view/1/another/param will be served from the same cache

If you have additional params that will add / be part of the caching you need to add them.

Upvotes: 1

Related Questions