PluginProbe ʕ •ᴥ•ʔ
ShareThis Dashboard for Google Analytics / 3.2.4
ShareThis Dashboard for Google Analytics v3.2.4
3.3.2 trunk 1.0.7 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.1 2.1.2 2.1.3 2.1.4 2.1.5 2.2.5 2.3.5 2.3.6 2.3.7 2.3.8 2.4.0 2.4.1 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 3.0.0 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.2.0 3.2.1 3.2.2 3.2.3 3.2.4 3.3.0 3.3.1
googleanalytics / lib / analytics-admin / vendor / google / auth / src / Cache / SysVCacheItemPool.php
googleanalytics / lib / analytics-admin / vendor / google / auth / src / Cache Last commit date
InvalidArgumentException.php 3 years ago Item.php 3 years ago MemoryCacheItemPool.php 3 years ago SysVCacheItemPool.php 3 years ago TypedItem.php 3 years ago
SysVCacheItemPool.php
250 lines
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 /**
40 * @var int
41 */
42 private $sysvKey;
43
44 /**
45 * @var CacheItemInterface[]
46 */
47 private $items;
48
49 /**
50 * @var CacheItemInterface[]
51 */
52 private $deferredItems;
53
54 /**
55 * @var array<mixed>
56 */
57 private $options;
58
59 /**
60 * @var bool
61 */
62 private $hasLoadedItems = false;
63
64 /**
65 * Create a SystemV shared memory based CacheItemPool.
66 *
67 * @param array<mixed> $options {
68 * [optional] Configuration options.
69 *
70 * @type int $variableKey The variable key for getting the data from the shared memory. **Defaults to** 1.
71 * @type string $proj The project identifier for ftok. This needs to be a one character string.
72 * **Defaults to** 'A'.
73 * @type int $memsize The memory size in bytes for shm_attach. **Defaults to** 10000.
74 * @type int $perm The permission for shm_attach. **Defaults to** 0600.
75 * }
76 */
77 public function __construct($options = [])
78 {
79 if (! extension_loaded('sysvshm')) {
80 throw new \RuntimeException(
81 'sysvshm extension is required to use this ItemPool'
82 );
83 }
84 $this->options = $options + [
85 'variableKey' => self::VAR_KEY,
86 'proj' => self::DEFAULT_PROJ,
87 'memsize' => self::DEFAULT_MEMSIZE,
88 'perm' => self::DEFAULT_PERM
89 ];
90 $this->items = [];
91 $this->deferredItems = [];
92 $this->sysvKey = ftok(__FILE__, $this->options['proj']);
93 }
94
95 /**
96 * @param mixed $key
97 * @return CacheItemInterface
98 */
99 public function getItem($key): CacheItemInterface
100 {
101 $this->loadItems();
102 return current($this->getItems([$key])); // @phpstan-ignore-line
103 }
104
105 /**
106 * @param array<mixed> $keys
107 * @return iterable<CacheItemInterface>
108 */
109 public function getItems(array $keys = []): iterable
110 {
111 $this->loadItems();
112 $items = [];
113 $itemClass = \PHP_VERSION_ID >= 80000 ? TypedItem::class : Item::class;
114 foreach ($keys as $key) {
115 $items[$key] = $this->hasItem($key) ?
116 clone $this->items[$key] :
117 new $itemClass($key);
118 }
119 return $items;
120 }
121
122 /**
123 * {@inheritdoc}
124 */
125 public function hasItem($key): bool
126 {
127 $this->loadItems();
128 return isset($this->items[$key]) && $this->items[$key]->isHit();
129 }
130
131 /**
132 * {@inheritdoc}
133 */
134 public function clear(): bool
135 {
136 $this->items = [];
137 $this->deferredItems = [];
138 return $this->saveCurrentItems();
139 }
140
141 /**
142 * {@inheritdoc}
143 */
144 public function deleteItem($key): bool
145 {
146 return $this->deleteItems([$key]);
147 }
148
149 /**
150 * {@inheritdoc}
151 */
152 public function deleteItems(array $keys): bool
153 {
154 if (!$this->hasLoadedItems) {
155 $this->loadItems();
156 }
157
158 foreach ($keys as $key) {
159 unset($this->items[$key]);
160 }
161 return $this->saveCurrentItems();
162 }
163
164 /**
165 * {@inheritdoc}
166 */
167 public function save(CacheItemInterface $item): bool
168 {
169 if (!$this->hasLoadedItems) {
170 $this->loadItems();
171 }
172
173 $this->items[$item->getKey()] = $item;
174 return $this->saveCurrentItems();
175 }
176
177 /**
178 * {@inheritdoc}
179 */
180 public function saveDeferred(CacheItemInterface $item): bool
181 {
182 $this->deferredItems[$item->getKey()] = $item;
183 return true;
184 }
185
186 /**
187 * {@inheritdoc}
188 */
189 public function commit(): bool
190 {
191 foreach ($this->deferredItems as $item) {
192 if ($this->save($item) === false) {
193 return false;
194 }
195 }
196 $this->deferredItems = [];
197 return true;
198 }
199
200 /**
201 * Save the current items.
202 *
203 * @return bool true when success, false upon failure
204 */
205 private function saveCurrentItems()
206 {
207 $shmid = shm_attach(
208 $this->sysvKey,
209 $this->options['memsize'],
210 $this->options['perm']
211 );
212 if ($shmid !== false) {
213 $ret = shm_put_var(
214 $shmid,
215 $this->options['variableKey'],
216 $this->items
217 );
218 shm_detach($shmid);
219 return $ret;
220 }
221 return false;
222 }
223
224 /**
225 * Load the items from the shared memory.
226 *
227 * @return bool true when success, false upon failure
228 */
229 private function loadItems()
230 {
231 $shmid = shm_attach(
232 $this->sysvKey,
233 $this->options['memsize'],
234 $this->options['perm']
235 );
236 if ($shmid !== false) {
237 $data = @shm_get_var($shmid, $this->options['variableKey']);
238 if (!empty($data)) {
239 $this->items = $data;
240 } else {
241 $this->items = [];
242 }
243 shm_detach($shmid);
244 $this->hasLoadedItems = true;
245 return true;
246 }
247 return false;
248 }
249 }
250