3 namespace Friendica\Util\Lock;
5 use Friendica\Core\Cache;
8 class MemcacheLockDriver implements ILockDriver
12 * @brief Sets a lock for a given name
14 * @param string $key The Name of the lock
15 * @param integer $timeout Seconds until we give up
17 * @return boolean Was the lock successful?
19 public function acquireLock($key, $timeout = 120)
24 $cachekey = get_app()->get_hostname() . ";lock:" . $key;
27 // We only lock to be sure that nothing happens at exactly the same time
29 $lock = Cache::get($cachekey);
31 if (!is_bool($lock)) {
34 // When the process id isn't used anymore, we can safely claim the lock for us.
35 // Or we do want to lock something that was already locked by us.
36 if (!posix_kill($pid, 0) || ($pid == getmypid())) {
41 Cache::set($cachekey, getmypid(), 300);
47 if (!$got_lock && ($timeout > 0)) {
48 usleep(rand(10000, 200000));
50 } while (!$got_lock && ((time() - $start) < $timeout));
56 * @brief Removes a lock if it was set by us
58 * @param string $key Name of the lock
62 public function releaseLock($key)
64 $cachekey = get_app()->get_hostname() . ";lock:" . $key;
65 $lock = Cache::get($cachekey);
67 if (!is_bool($lock)) {
68 if ((int)$lock == getmypid()) {
69 Cache::delete($cachekey);
77 * @brief Removes all lock that were set by us
81 public function releaseAll()
83 // We cannot delete all cache entries, but this doesn't matter with memcache