/** * Get exclusive mutex for key. Key will be still accessible to read and write, but * another process can exclude dog-pile effect, if before updating the key he will try to get this mutex. * @param mixed $key * @param mixed $auto_unlocker_variable - pass empty, just declared variable * @return bool */ public function lock_key($key, &$auto_unlocker_variable) { $r = $this->redis->SetNX($this->lock_key_prefix . $key, 1); if (!$r) { return false; } $this->redis->Expire($this->lock_key_prefix . $key, $this->key_lock_time); $auto_unlocker_variable = new KeyAutoUnlocker(array($this, 'unlock_key')); $auto_unlocker_variable->setKey($key); return true; }
/** * Get exclusive mutex for key. Key will be still accessible to read and write, but * another process can exclude dog-pile effect, if before updating the key he will try to get this mutex. * Example: * Process 1 reads key simultaneously with Process 2. * Value of this key are too old, so Process 1 going to refresh it. Simultaneously with Process 2. * But both of them trying to lock_key, and Process 1 only will refresh value of key (taking it from database, e.g.), * and Process 2 can decide, what he want to do - use old value and not spent time to database, or something else. * @param mixed $key * @param mixed $auto_unlocker_variable - pass empty, just declared variable * @return bool */ public function lock_key($key, &$auto_unlocker_variable) { $r = apc_add($this->lock_key_prefix . $key, 1, $this->key_lock_time); if (!$r) { return false; } $auto_unlocker_variable = new KeyAutoUnlocker(array($this, 'unlock_key')); $auto_unlocker_variable->setKey($key); return true; }