Just visited drupal.org and got the following error displayed at the top of the page:

user error: Duplicate entry 'filter:1:f072dc44d2a94b4022449416af35d169' for key 1
query: INSERT INTO cache (cid, data, created, expire, headers) VALUES('filter:1:f072dc44d2a94b4022449416af35d169', '<p><a href=\"http://www.oscom.org/events/oscom4/\">OSCOM 4</a> is the fourth conference of OSCOM (The international association for Open Source Content Management), where all kinds of content management framework authors and users meet. This fall the conference also features an Apache Track, which means quite a few sessions on Apache Software Foundation projects, like PHP, Jakarta, and the Apache HTTP server. For the first time, Drupal will also be represented on the conference, with an introductory session.</p>
', 1095070810, 1, '') in /var/www/drupal.org/includes/database.mysql.inc on line 12

Comments

Dru’s picture

Title: Cache bug in new drupal.org » Cache bug in new drupal.org - with proposed solution

I found out that this error can happen frequently when you have calls to cache_set, with the same data in a time period of less than 1 second.
The cache_set uses UPDATE (line 149 in bootstrap.inc) to change the data in the DB, and if the number of affected rows is 0 (zero), it uses INSERT to add a new cache record.
But when the MySQL encounters an UPDATE that has exactly the same data as the data already in the DB it returns 0 for number of affected rows.
So when Drupal does an UPDATE with data equal to the data in the DB, within the same second as the last update (so the created timestamp is also the same). Eventhough there is already a record in the DB it will receive 0 for the number of rows updated, which in turn makes Drupal try to insert a new record, and results in the error of duplicate records with same key.

I solved that by using a timestamp of seconds and microseonds for the created time which requires the following steps-

1. setting the 'created' field in the 'cache' to double
2. changing the cache_set code in bootstrap.inc to -

function cache_set($cid, $data, $expire = CACHE_PERMANENT, $headers = NULL) {
$data = db_encode_blob($data);
$time = gettimeofday();
db_query("UPDATE {cache} SET data = '%s', created = %d.%06d, expire = %d, headers = '%s' WHERE cid = '%s'", $data, $time['sec'], $time['usec'], $expire, $headers, $cid);
if (!db_affected_rows()) {
db_query("INSERT INTO {cache} (cid, data, created, expire, headers) VALUES ('%s', '%s', %d.%06d, %d, '%s')", $cid, $data, $time['sec'], $time['usec'], $expire, $headers);
}
}

I'll be happy if someone will check this fix, and puts it into cvs.
Thanks,
Dror

dries’s picture

This has been fixed in HEAD. Thanks.

Anonymous’s picture

wanghl165’s picture

Version: » 4.6.0

I just made this change , Thanks.

akuma’s picture

1. setting the 'created' field in the 'cache' to double

i struggled with this, so i thought i'd post an elaboration for those of us who aren't quite so experienced with mysql:

mysql> alter table `cache` change `created` `created` double() default '0' not null

insomoz’s picture

Status: Closed (fixed) » Active

I tried the mysql alter but no luck
I get thousands of these errors a day on drupal
Duplicate entry '%s' for key 1 query: INSERT INTO cache (cid, data, created, expire, headers) VALUES

greggles’s picture

Status: Active » Closed (fixed)

insomoz - Dries said it's fixed - please don't reopen the bug.