]> git.mxchange.org Git - friendica.git/blob - src/Core/Lock/CacheLockDriver.php
1bb768bd0f180b4abc09d52ad73afcf34cb1ea8f
[friendica.git] / src / Core / Lock / CacheLockDriver.php
1 <?php
2
3 namespace Friendica\Core\Lock;
4
5 use Friendica\Core\Cache\ICacheDriver;
6
7 class CacheLockDriver extends AbstractLockDriver
8 {
9         /**
10          * @var \Friendica\Core\Cache\ICacheDriver;
11          */
12         private $cache;
13
14         /**
15          * CacheLockDriver constructor.
16          *
17          * @param ICacheDriver $cache The CacheDriver for this type of lock
18          */
19         public function __construct(ICacheDriver $cache)
20         {
21                 $this->cache = $cache;
22         }
23
24         /**
25          *
26          * @brief Sets a lock for a given name
27          *
28          * @param string $key The Name of the lock
29          * @param integer $timeout Seconds until we give up
30          *
31          * @return boolean Was the lock successful?
32          */
33         public function acquireLock($key, $timeout = 120)
34         {
35                 $got_lock = false;
36                 $start = time();
37
38                 $cachekey = get_app()->get_hostname() . ";lock:" . $key;
39
40                 do {
41                         $lock = $this->cache->get($cachekey);
42
43                         if (!is_bool($lock)) {
44                                 $pid = (int)$lock;
45
46                                 // When the process id isn't used anymore, we can safely claim the lock for us.
47                                 // Or we do want to lock something that was already locked by us.
48                                 if (!posix_kill($pid, 0) || ($pid == getmypid())) {
49                                         $lock = false;
50                                 }
51                         }
52                         if (is_bool($lock)) {
53                                 $this->cache->set($cachekey, getmypid(), 300);
54                                 $got_lock = true;
55                         }
56
57                         if (!$got_lock && ($timeout > 0)) {
58                                 usleep(rand(10000, 200000));
59                         }
60                 } while (!$got_lock && ((time() - $start) < $timeout));
61
62                 $this->markAcquire($key);
63
64                 return $got_lock;
65         }
66
67         /**
68          * @brief Removes a lock if it was set by us
69          *
70          * @param string $key Name of the lock
71          *
72          * @return mixed
73          */
74         public function releaseLock($key)
75         {
76                 $cachekey = get_app()->get_hostname() . ";lock:" . $key;
77                 $lock = $this->cache->get($cachekey);
78
79                 if (!is_bool($lock)) {
80                         if ((int)$lock == getmypid()) {
81                                 $this->cache->delete($cachekey);
82                         }
83                 }
84
85                 $this->markRelease($key);
86
87                 return;
88         }
89 }