get id of saved record in cache table

hi.

i have a query like this


$record=books::model()->cache(10000)->find('id=:id',array(":id"=>$id));

i have 2 questions.

1)how to get id of saved record in yiicache table?

2)how do i know if cache expired in query and result is not from cache ?

As far as I know there is no elegant way to do that with CActiveRecord but the question is: Why do you need that? If you want to be sure your book is always in cache you could set the first parameter to an insane number. If you need to invalidate the cache (like when a book was updated etc.) you can use this extension as a cache dependency: http://www.yiiframework.com/extension/flushable

If - for some reason - you really need to know if the row is in the cache or not you will have to know the cache key. Yii generates a pretty long one to avoid collisions but nobody prevents you from using your own like


Yii::app()->db->cache->set('mybookKey',$book,1000);

if you want to check if a model is in cache use


if(($cachedBook=Yii::app()->cache->get('mybookKey'))==false)

{

    //NOT IN CACHE

    $book=Book::model()->find();

    Yii::app()->db->cache->set('mybookKey',$book,1000);

    return $book;

}

else //IN CACHE

    return $cachedBook;

thank u for solution. i test it. but in your way i have to save values by my own.

i think there should be a way to get generated id by yii.

any idea?

There is no way that I know of. Look at CDbCommands queryInternal method to see how the cache key is defined: https://github.com/yiisoft/yii/blob/1.1.11/framework/db/CDbCommand.php#L481

It basically is yii.dbqueryCONNECTIONSTRING:USERNAME:SQLQUERY:PARAMS

If you enable tracing in your log routes you should see the autogenerated cache key