]> git.mxchange.org Git - friendica.git/blob - src/Util/Lock/MemcacheLockDriver.php
minor changes
[friendica.git] / src / Util / Lock / MemcacheLockDriver.php
1 <?php
2
3 namespace Friendica\Util\Lock;
4
5 use Friendica\Core\Cache;
6 use dba;
7
8 class MemcacheLockDriver implements ILockDriver
9 {
10         /**
11          *
12          * @brief Sets a lock for a given name
13          *
14          * @param string $key The Name of the lock
15          * @param integer $timeout Seconds until we give up
16          *
17          * @return boolean Was the lock successful?
18          */
19         public function acquireLock($key, $timeout = 120)
20         {
21                 $got_lock = false;
22                 $start = time();
23
24                 $cachekey = get_app()->get_hostname() . ";lock:" . $key;
25
26                 do {
27                         // We only lock to be sure that nothing happens at exactly the same time
28                         dba::lock('locks');
29                         $lock = Cache::get($cachekey);
30
31                         if (!is_bool($lock)) {
32                                 $pid = (int)$lock;
33
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())) {
37                                         $lock = false;
38                                 }
39                         }
40                         if (is_bool($lock)) {
41                                 Cache::set($cachekey, getmypid(), 300);
42                                 $got_lock = true;
43                         }
44
45                         dba::unlock();
46
47                         if (!$got_lock && ($timeout > 0)) {
48                                 usleep(rand(10000, 200000));
49                         }
50                 } while (!$got_lock && ((time() - $start) < $timeout));
51
52                 return $got_lock;
53         }
54
55         /**
56          * @brief Removes a lock if it was set by us
57          *
58          * @param string $key Name of the lock
59          *
60          * @return mixed
61          */
62         public function releaseLock($key)
63         {
64                 $cachekey = get_app()->get_hostname() . ";lock:" . $key;
65                 $lock = Cache::get($cachekey);
66
67                 if (!is_bool($lock)) {
68                         if ((int)$lock == getmypid()) {
69                                 Cache::delete($cachekey);
70                         }
71                 }
72
73                 return;
74         }
75
76         /**
77          * @brief Removes all lock that were set by us
78          *
79          * @return void
80          */
81         public function releaseAll()
82         {
83                 // We cannot delete all cache entries, but this doesn't matter with memcache
84                 return;
85         }
86 }