Page Cache dependency / dependencies

Hi,

I call the cache in filters() in my Controller


public function filters()

{

    return array(

        array(

            'COutputCache',

            'duration'=>3600,

            'varyByParam'=>array('languages', 'value'),

        ),

    );

}

The site loads the content with the parameter language and value.

The Cache is working well, but I need the Id from the cached side. The site is static and can stay a long time in cached version until i make an update of the content.

Then I need the id from the site to delete the old site from the cache! But from where I get the id??

My Workflow is as follow:

  1. Write a new dynamic site and save the data in the database

  2. When the user calls the content, the site is cached in APC

  3. When I update the content, I need the id from the old cached site to delete it.

  4. Then I need a method to call the cache and get an cached site.

thank you for your help

Look at method getCacheKey of COutputCache class and you will identify how this id is generated.

Thank you for your answer.

But how do I use getCacheKey and how can I start to cache the updated data’s?

CacheKey is used to save and get data from cache. But I was not right in previous answer, the better way is using cache dependency. You may add field to your database table where you will store the time of last update and create dependency which uses this field. In this case the cache will be refreshed when the value of this field changes.

That is a very good idea.

Could you make an small tutorial for this?

I don’t know how to tell


public function filters()

{

    return array(

        array(

            'COutputCache',

            'duration'=>3600,

            'varyByParam'=>array('languages', 'value'),

        ),

    );

}

that it loads the content from the database?!

According to documentation it should be like this




public function filters()

{

	return array(

		array(

			'COutputCache',

			'duration' => 3600,

			'varyByParam' => array('languages', 'value'),

			'dependency' => array(

 				'class' => 'CDbCacheDependency',

 				'sql' => 'SELECT MAX(lastModified) FROM tableName',

			),

		),

	);

}



You may read this section to find out other cache dependencies.

thank you. Yes I readed the section but it is writen very poor.

What coloums do I need for my table?

id

lastModified

xxx???

In this example the column is lastModified.

but I need a coloumn for the cache id or for the varyByParam’ => array(‘languages’, ‘value’), ????

No. varyByParam is reffered to request’s GET params. What data do you store in database now?

the dynamic content is loaded from the table "stores".

stores

id

language

value


'dependency' => array(

   'class' => 'CDbCacheDependency',

   'sql' => 'SELECT MAX(lastModified) FROM tableName',



Now I understand, I have to alter the tabel stores and add a new coloum lastModified.

Can I start the Cache for every Id?

I’m only a little bit frustrated that I can’t calculate the CacheKey.

COutputCache store in cache whole page’s content and that’s why it can not be bound to the id of database table, because controller’s action is not bound to any model directly. You may store in cache not whole page but only the data you get from database.

This solution is a little bit slow.

I think the Caching-System from yii have to improve!

May be you must learn more. For example, CGlobalStateCacheDependency works fine for me. Thanks to this page cache scheme, my slow pages got x200 faster.

When you create/update/delete the new record, you should alter the global state using afterSave() or afterDelete() in your model.