diff --git a/memcache-lock.inc b/memcache-lock.inc
index 39d90ff..02bd9e8 100644
--- a/memcache-lock.inc
+++ b/memcache-lock.inc
@@ -31,13 +31,16 @@ function lock_acquire($name, $timeout = 30) {
   // Ensure that the timeout is at least 1 sec. This is a limitation
   // imposed by memcached.
   $timeout = (int) max($timeout, 1);
-
-  if (dmemcache_add($name, _lock_id(), $timeout, 'semaphore')) {
-    $locks[$name] = _lock_id();
+  $lock_id = _lock_id();
+  
+  if (dmemcache_add($name, $lock_id, $timeout, 'semaphore')) {
+    if($lock_id == dmemcache_get($name, 'semaphore')) {
+      $locks[$name] = $lock_id;
+    }
   }
-  elseif ($result = dmemcache_get($name, 'semaphore') && isset($locks[$name]) && $locks[$name] == _lock_id()) {
+  elseif ($result = dmemcache_get($name, 'semaphore') && isset($locks[$name]) && $locks[$name] == $lock_id) {
     // Only renew the lock if we already set it and it has not expired.
-    dmemcache_set($name, _lock_id(), $timeout, 'semaphore');
+    dmemcache_set($name, $lock_id, $timeout, 'semaphore');
   }
   else {
     // Failed to acquire the lock.  Unset the key from the $locks array even if
