Fixing redis cachekey
[friendica.git/.git] / src / Core / Lock / AbstractLockDriver.php
1 <?php
2
3 namespace Friendica\Core\Lock;
4 use Friendica\BaseObject;
5
6 /**
7  * Class AbstractLockDriver
8  *
9  * @package Friendica\Core\Lock
10  *
11  * Basic class for Locking with common functions (local acquired locks, releaseAll, ..)
12  */
13 abstract class AbstractLockDriver extends BaseObject implements ILockDriver
14 {
15         /**
16          * @var array The local acquired locks
17          */
18         protected $acquiredLocks = [];
19
20         /**
21          * Check if we've locally acquired a lock
22          *
23          * @param string key The Name of the lock
24          * @return bool      Returns true if the lock is set
25          */
26         protected function hasAcquiredLock($key)
27         {
28                 return isset($this->acquireLock[$key]) && $this->acquiredLocks[$key] === true;
29         }
30
31         /**
32          * Mark a locally acquired lock
33          *
34          * @param string $key The Name of the lock
35          */
36         protected function markAcquire($key)
37         {
38                 $this->acquiredLocks[$key] = true;
39         }
40
41         /**
42          * Mark a release of a locally acquired lock
43          *
44          * @param string $key The Name of the lock
45          */
46         protected function markRelease($key)
47         {
48                 unset($this->acquiredLocks[$key]);
49         }
50
51         /**
52          * Releases all lock that were set by us
53          *
54          * @return boolean Was the unlock of all locks successful?
55          */
56         public function releaseAll()
57         {
58                 $return = true;
59
60                 foreach ($this->acquiredLocks as $acquiredLock => $hasLock) {
61                         if (!$this->releaseLock($acquiredLock)) {
62                                 $return = false;
63                         }
64                 }
65
66                 return $return;
67         }
68 }