Cdbcache Duplicate Primary Keys


We have a rather big CRM built on top of Yii. There’s one constant problem in application log to do with CDbCache:

2013/04/16 10:50:45 [error] [system.db.CDbCommand]

  CDbCommand::execute() failed: SQLSTATE[23000]: Integrity constraint

  violation: 1062 Duplicate entry '3c6dfaf87ff9f3fa382f52fc26b9b3cc' for key 'PRIMARY'. The SQL statement executed was: INSERT INTO YiiCache

  (id,expire,value) VALUES


By the looks of it, application tries to cache the same thing twice at the same time. The first save of cache succeeds, while the second one throws that error (only in application log, the actual application doesn’t crash). We use minScript (http://www.yiiframew…sion/minscript/) with database as a cache, so most of the values which are cached are css and js files, but things like table schema are sometimes being put twice as well.

PHP: 5.3.13

MySQL: 5.1.68

Yii: 1.1.13, but happened with previous versions also

Is this a bug, or is it an expected behaviour?

For reproduction, it is hard to do it, as it happens randomly, and I couldn’t make it throw this error when I wanted to.

This is a rather old topic, but I’m facing the same right now. Anyone experienced this and have a clue of how to fix it? Some background information: I’m just trying to cache the output of a CActiveRecord findAll method:

$c = new CDbCriteria();

$c->with = 'memberMetas';

$c->condition = 'dataset_id=:dataset_id and ts>=:start_ts and ts<:end_ts and `key`=:key and value=:value';

$c->params = self::dbParams($statistics);

$dependency = new CDbCacheDependency('SELECT MAX(ts) FROM sts_member');

$statistics->members = Member::model()->cache(1000, $dependency)->findAll($c);

The same error occurs as described by unique_yikes on the last line where I query the db.