Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / memcache / src / Lock / MemcacheLockBackend.php
1 <?php
2
3 namespace Drupal\memcache\Lock;
4
5 use Drupal\Core\Lock\LockBackendAbstract;
6 use Drupal\memcache\DrupalMemcacheInterface;
7
8 /**
9  * Defines a Memcache lock backend.
10  */
11 class MemcacheLockBackend extends LockBackendAbstract {
12
13   /**
14    * An array of currently acquired locks.
15    *
16    * @var array
17    */
18   protected $locks = [];
19
20   /**
21    * The bin name for this lock.
22    *
23    * @var string
24    */
25   protected $bin;
26
27   /**
28    * The memcache wrapper object.
29    *
30    * @var \Drupal\memcache\DrupalMemcacheInterface
31    */
32   protected $memcache;
33
34   /**
35    * Constructs a new MemcacheLockBackend.
36    *
37    * @param string $bin
38    *   The bin name for this lock.
39    * @param \Drupal\memcache\DrupalMemcacheInterface $memcache
40    *   The memcache wrapper object.
41    */
42   public function __construct($bin, DrupalMemcacheInterface $memcache) {
43     $this->bin = $bin;
44     $this->memcache = $memcache;
45
46     // __destruct() is causing problems with garbage collections, register a
47     // shutdown function instead.
48     drupal_register_shutdown_function([$this, 'releaseAll']);
49   }
50
51   /**
52    * {@inheritdoc}
53    */
54   public function acquire($name, $timeout = 30.0) {
55     // Ensure that the timeout is at least 1 sec. This is a limitation imposed
56     // by memcached.
57     $timeout = (int) max($timeout, 1);
58
59     $lock_id = $this->getLockId();
60
61     if (isset($this->locks[$name])) {
62       // Try to extend the expiration of a lock we already acquired.
63       $success = !$this->lockMayBeAvailable($name) && $this->memcache->set($name, $lock_id, $timeout);
64
65       if (!$success) {
66         // The lock was broken.
67         unset($this->locks[$name]);
68       }
69
70       return $success;
71     }
72     else {
73       if ($this->lockMayBeAvailable($name)) {
74         $success = $this->memcache->add($name, $lock_id, $timeout);
75
76         if (!$success) {
77           return FALSE;
78         }
79
80         // We track all acquired locks in the global variable, if successful.
81         $this->locks[$name] = TRUE;
82       }
83       else {
84         return FALSE;
85       }
86     }
87
88     return isset($this->locks[$name]);
89   }
90
91   /**
92    * {@inheritdoc}
93    */
94   public function lockMayBeAvailable($name) {
95     return !$this->memcache->get($name);
96   }
97
98   /**
99    * {@inheritdoc}
100    */
101   public function release($name) {
102     $this->memcache->delete($name);
103     // We unset unconditionally since caller assumes lock is released anyway.
104     unset($this->locks[$name]);
105   }
106
107   /**
108    * {@inheritdoc}
109    */
110   public function releaseAll($lock_id = NULL) {
111     if (empty($lock_id)) {
112       $lock_id = $this->getLockId();
113     }
114
115     foreach ($this->locks as $name => $id) {
116       $value = $this->memcache->get($name);
117
118       if ($value == $lock_id) {
119         $this->memcache->delete($name);
120       }
121     }
122
123     $this->locks = [];
124   }
125
126 }