Branch: Tag:

2013-01-16

2013-01-16 14:59:51 by Marcus Wellhardh <wellhardh@roxen.com>

Optimized the image cache store() function slightly by avoiding
unnecessary database inserts.

A write operation in the database will lock the table and
potentially generate an image cache stall in other handler
threads. Therefore do a select first to determine if the insert
is needed. This should speed up restarted servers with an
unpopulated uid_cache.

6:   // Per Hedbor, Henrik Grubbström, Pontus Hagland, David Hedbor and others.   // ABS and suicide systems contributed freely by Francesco Chemolli    - constant cvs_version="$Id: roxen.pike,v 1.1126 2012/12/12 15:03:39 grubba Exp $"; + constant cvs_version="$Id$";      //! @appears roxen   //!
4387:    if( zero_type( uid_cache[ ci ] ) )    {    uid_cache[ci] = user; -  if( catch(QUERY("INSERT INTO "+name+" " -  "(id,uid,atime) VALUES (%s,%s,UNIX_TIMESTAMP())", -  ci, user||"")) ) -  QUERY( "UPDATE "+name+" SET uid=%s WHERE id=%s", -  user||"", ci ); +  // Make sure to only update the entry if it does not already +  // exists or has wrong uid. Allways updating the table will +  // casue mysql to lock the table and cause a potential gobal +  // ImageCache stall. +  string uid = user || ""; +  array q = QUERY("SELECT uid from "+name+" where id=%s", ci); +  if(!sizeof(q) || (sizeof(q) && q[0]->uid != uid)) { +  QUERY("INSERT INTO "+name+" " +  "(id,uid,atime) VALUES (%s,%s,UNIX_TIMESTAMP()) " +  "ON DUPLICATE KEY UPDATE uid=%s", +  ci, uid, uid);    } -  +  }      #ifndef NO_ARG_CACHE_SB_REPLICATE    if(id->misc->persistent_cache_crawler || id->misc->do_replicate_argcache) {