memcache imlementation of D7 locking
yhager - October 6, 2009 - 05:39
| Project: | Memcache API and Integration |
| Version: | 6.x-1.x-dev |
| Component: | Code |
| Category: | feature request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | needs work |
Jump to:
Description
I've written this for version 5 (don't ask...) - but the code is version agnostic anyway.
#239798: Provide an interface for add would have been useful here...
| Attachment | Size |
|---|---|
| memcache-lock.inc_.txt | 3.69 KB |

#1
You have a race window between get and set. You must use add to begin with. That's atomic.
#2
but I only set if I have the lock, and I assume those two lines of code would take less than a full second.
#3
A full second! Some get thirty page loads a second...
#4
You mean I got that condition backwards. Good catch!
How about if I change it to:
<?php// renew if we have at least 1 second more. This is required to ensure atomic renew.
$success = ($result->lock_expire > $now + 1) && (bool)$mc->set($name, $lock, FALSE, $timeout);
?>
#5
set is not atomic. add is. Edit: sorry I mean, set is not what you want. Two processes can run set and succeed. But only one add can succeed.