PluginProbe
WP-Stateless – Google Cloud Storage / 3.0.3
WP-Stateless – Google Cloud Storage v3.0.3
4.4.3 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.3.0 2.3.1 2.3.2 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.2.0 3.2.1 3.2.2 All 62 releases
wp-stateless / lib / Google / vendor / google / auth / src / Cache / SysVCacheItemPool.php

SysVCacheItemPool.php in WP-Stateless – Google Cloud Storage 3.0.3, at lib/Google/vendor/google/auth/src/Cache/SysVCacheItemPool.php

242 lines 5.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Copyright 2018 Google Inc. All Rights Reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 namespace Google\Auth\Cache;
18
19 use Psr\Cache\CacheItemInterface;
20 use Psr\Cache\CacheItemPoolInterface;
21
22 /**
23 * SystemV shared memory based CacheItemPool implementation.
24 *
25 * This CacheItemPool implementation can be used among multiple processes, but
26 * it doesn't provide any locking mechanism. If multiple processes write to
27 * this ItemPool, you have to avoid race condition manually in your code.
28 */
29 class SysVCacheItemPool implements CacheItemPoolInterface
30 {
31 const VAR_KEY = 1;
32
33 const DEFAULT_PROJ = 'A';
34
35 const DEFAULT_MEMSIZE = 10000;
36
37 const DEFAULT_PERM = 0600;
38
39 /** @var int */
40 private $sysvKey;
41
42 /**
43 * @var CacheItemInterface[]
44 */
45 private $items;
46
47 /**
48 * @var CacheItemInterface[]
49 */
50 private $deferredItems;
51
52 /**
53 * @var array
54 */
55 private $options;
56
57 /*
58 * @var bool
59 */
60 private $hasLoadedItems = false;
61
62 /**
63 * Create a SystemV shared memory based CacheItemPool.
64 *
65 * @param array $options [optional] Configuration options.
66 * @param int $options.variableKey The variable key for getting the data from
67 * the shared memory. **Defaults to** 1.
68 * @param $options.proj string The project identifier for ftok. This needs to
69 * be a one character string. **Defaults to** 'A'.
70 * @param $options.memsize int The memory size in bytes for shm_attach.
71 * **Defaults to** 10000.
72 * @param $options.perm int The permission for shm_attach. **Defaults to**
73 * 0600.
74 */
75 public function __construct($options = [])
76 {
77 if (! extension_loaded('sysvshm')) {
78 throw new \RuntimeException(
79 'sysvshm extension is required to use this ItemPool'
80 );
81 }
82 $this->options = $options + [
83 'variableKey' => self::VAR_KEY,
84 'proj' => self::DEFAULT_PROJ,
85 'memsize' => self::DEFAULT_MEMSIZE,
86 'perm' => self::DEFAULT_PERM
87 ];
88 $this->items = [];
89 $this->deferredItems = [];
90 $this->sysvKey = ftok(__FILE__, $this->options['proj']);
91 }
92
93 public function getItem($key)
94 {
95 $this->loadItems();
96 return current($this->getItems([$key]));
97 }
98
99 /**
100 * {@inheritdoc}
101 */
102 public function getItems(array $keys = [])
103 {
104 $this->loadItems();
105 $items = [];
106 foreach ($keys as $key) {
107 $items[$key] = $this->hasItem($key) ?
108 clone $this->items[$key] :
109 new Item($key);
110 }
111 return $items;
112 }
113
114 /**
115 * {@inheritdoc}
116 */
117 public function hasItem($key)
118 {
119 $this->loadItems();
120 return isset($this->items[$key]) && $this->items[$key]->isHit();
121 }
122
123 /**
124 * {@inheritdoc}
125 */
126 public function clear()
127 {
128 $this->items = [];
129 $this->deferredItems = [];
130 return $this->saveCurrentItems();
131 }
132
133 /**
134 * {@inheritdoc}
135 */
136 public function deleteItem($key)
137 {
138 return $this->deleteItems([$key]);
139 }
140
141 /**
142 * {@inheritdoc}
143 */
144 public function deleteItems(array $keys)
145 {
146 if (!$this->hasLoadedItems) {
147 $this->loadItems();
148 }
149
150 foreach ($keys as $key) {
151 unset($this->items[$key]);
152 }
153 return $this->saveCurrentItems();
154 }
155
156 /**
157 * {@inheritdoc}
158 */
159 public function save(CacheItemInterface $item)
160 {
161 if (!$this->hasLoadedItems) {
162 $this->loadItems();
163 }
164
165 $this->items[$item->getKey()] = $item;
166 return $this->saveCurrentItems();
167 }
168
169 /**
170 * {@inheritdoc}
171 */
172 public function saveDeferred(CacheItemInterface $item)
173 {
174 $this->deferredItems[$item->getKey()] = $item;
175 return true;
176 }
177
178 /**
179 * {@inheritdoc}
180 */
181 public function commit()
182 {
183 foreach ($this->deferredItems as $item) {
184 if ($this->save($item) === false) {
185 return false;
186 }
187 }
188 $this->deferredItems = [];
189 return true;
190 }
191
192 /**
193 * Save the current items.
194 *
195 * @return bool true when success, false upon failure
196 */
197 private function saveCurrentItems()
198 {
199 $shmid = shm_attach(
200 $this->sysvKey,
201 $this->options['memsize'],
202 $this->options['perm']
203 );
204 if ($shmid !== false) {
205 $ret = shm_put_var(
206 $shmid,
207 $this->options['variableKey'],
208 $this->items
209 );
210 shm_detach($shmid);
211 return $ret;
212 }
213 return false;
214 }
215
216 /**
217 * Load the items from the shared memory.
218 *
219 * @return bool true when success, false upon failure
220 */
221 private function loadItems()
222 {
223 $shmid = shm_attach(
224 $this->sysvKey,
225 $this->options['memsize'],
226 $this->options['perm']
227 );
228 if ($shmid !== false) {
229 $data = @shm_get_var($shmid, $this->options['variableKey']);
230 if (!empty($data)) {
231 $this->items = $data;
232 } else {
233 $this->items = [];
234 }
235 shm_detach($shmid);
236 $this->hasLoadedItems = true;
237 return true;
238 }
239 return false;
240 }
241 }
242