Yii Framework Forum: Query cache and AR - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Query cache and AR Non-existant records are not cached Rate Topic: -----

#1 User is offline   pavlepredic 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 39
  • Joined: 30-August 11

Posted 08 May 2012 - 08:06 AM

Try this:

User::model()->cache(1000)->findByPk($nonExistantPk)


Assuming that there is no User with that PK, the result will be NULL. Next time you perform the same search, you would expect the result to be fetched from cache, but it does NOT. Yii attempts to get it from cache, but ends up performing the same query again. In my opinion, this is not the desired behavior.

The problem is this line (CDbCommand::queryInternal()) :

if(($result=$cache->get($cacheKey))!==false)
{
	Yii::trace('Query result found in cache','system.db.CDbCommand');
	return $result;
}


The real issue here is that CCache does not differentiate between a missing value and an existing value that evaluates to FALSE. This makes it impossible to store FALSE to cache, which causes query caching to fail under some circumstances.

The solution (I think) is to:
1) Add a method to CCache for checking if a key exists in cache (note that offsetExists() will also fail here)
2) Modify CDbCommand::queryInternal() so that it first checks if cache contains the key, and then returns it even if it is false.
0

#2 User is offline   pavlepredic 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 39
  • Joined: 30-August 11

Posted 14 September 2012 - 06:14 AM

Ended up doing this myself. Created a pull request: https://github.com/y...pull/1403/files
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users