--- Memcached.php 2009-02-05 16:14:22.000000000 -0800 +++ Memcached_Modified.php 2009-02-05 16:26:59.000000000 -0800 @@ -190,10 +190,8 @@ } else { $flag = 0; } - if ($this->test($id)) { - // because set and replace seems to have different behaviour - $result = $this->_memcache->replace($id, array($data, time(), $lifetime), $flag, $lifetime); - } else { + // set is required if the key already exists, but add is faster if it doesn't + if (! $result = $this->_memcache->add($id, array($data, time(), $lifetime), $flag, $lifetime)) { $result = $this->_memcache->set($id, array($data, time(), $lifetime), $flag, $lifetime); } if (count($tags) > 0) { @@ -437,11 +435,12 @@ if ($newLifetime <=0) { return false; } - if ($this->test($id)) { - // because set and replace seems to have different behaviour - $result = $this->_memcache->replace($id, array($data, time(), $newLifetime), $flag, $newLifetime); - } else { - $result = $this->_memcache->set($id, array($data, time(), $newLifetime), $flag, $newLifetime); + /* + * This add call will only succeed if the key has fallen out between the get call above + * and here but the speed benefit of add is useful enough to run it regardless. + */ + if (! $result = $this->_memcache->add($id, array($data, time(), $lifetime), $flag, $lifetime)) { + $result = $this->_memcache->set($id, array($data, time(), $lifetime), $flag, $lifetime); } return true; }