Atomic replace and append operations

Richard Cameron camster at citeulike.org
Sun Mar 20 03:01:21 PST 2005


> [T]he biggest issue I've got at the moment is that I can take a clean, 
> unhacked copy of memcached, run it up, and say:
>
> [set x "hello"]
> [delete x]
>
> very quickly followed by
>
> [add x "world"]
>
> I get a "NOT_STORED" error back.

I think I've tracked this down. What seems to be happening was that any 
immediate delete operation wouldn't take effect until the next 
deleteevent simply because it's exptime was being set to "never expire" 
rather than "in the past". This meant that there was a (maximum) five 
second period where the data had technically been deleted, but it was 
still lingering on in the system.

The following patch appears to sort it out. It seems fine, but I'd be 
grateful if anyone who knows this code a little better can confirm that 
it won't have any adverse effects on the item memory management system.


Index: memcached.c
===================================================================
--- memcached.c (revision 2217)
+++ memcached.c (working copy)
@@ -705,9 +705,17 @@
              out_string(c, "NOT_FOUND");
              return;
          }
-
-        exptime = realtime(exptime);
-
+
+        if (exptime==0) {
+            /* If we want to expire immediately then don't have
+               realtime() set exptime to 0, as this won't be picked up
+               by the expiry test later. We'll use 1 as a suitable
+               time in the past. */
+            exptime = 1;
+        } else {
+            exptime = realtime(exptime);
+        }
+
          it->refcount++;
          /* use its expiration time as its deletion time now */
          it->exptime = exptime;



More information about the memcached mailing list