| 1 |
<?php |
| 2 |
|
| 3 |
namespace TablePress\PhpOffice\PhpSpreadsheet\Collection\Memory; |
| 4 |
|
| 5 |
use TablePress\Psr\SimpleCache\CacheInterface; |
| 6 |
|
| 7 |
/** |
| 8 |
* This is the default implementation for in-memory cell collection. |
| 9 |
* |
| 10 |
* Alternative implementation should leverage off-memory, non-volatile storage |
| 11 |
* to reduce overall memory usage. |
| 12 |
* |
| 13 |
* Either SimpleCache1 or SimpleCache3, but not both, may be used. |
| 14 |
* For code coverage testing, it will always be SimpleCache3. |
| 15 |
* |
| 16 |
* @codeCoverageIgnore |
| 17 |
*/ |
| 18 |
class SimpleCache1 implements CacheInterface |
| 19 |
{ |
| 20 |
/** |
| 21 |
* @var array Cell Cache |
| 22 |
*/ |
| 23 |
private array $cache = []; |
| 24 |
|
| 25 |
public function clear(): bool |
| 26 |
{ |
| 27 |
$this->cache = []; |
| 28 |
|
| 29 |
return true; |
| 30 |
} |
| 31 |
|
| 32 |
public function delete($key): bool |
| 33 |
{ |
| 34 |
unset($this->cache[$key]); |
| 35 |
|
| 36 |
return true; |
| 37 |
} |
| 38 |
|
| 39 |
public function deleteMultiple($keys): bool |
| 40 |
{ |
| 41 |
foreach ($keys as $key) { |
| 42 |
$this->delete($key); |
| 43 |
} |
| 44 |
|
| 45 |
return true; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* @return mixed |
| 50 |
*/ |
| 51 |
public function get($key, $default = null) |
| 52 |
{ |
| 53 |
if ($this->has($key)) { |
| 54 |
return $this->cache[$key]; |
| 55 |
} |
| 56 |
|
| 57 |
return $default; |
| 58 |
} |
| 59 |
|
| 60 |
public function getMultiple($keys, $default = null): iterable |
| 61 |
{ |
| 62 |
$results = []; |
| 63 |
foreach ($keys as $key) { |
| 64 |
$results[$key] = $this->get($key, $default); |
| 65 |
} |
| 66 |
|
| 67 |
return $results; |
| 68 |
} |
| 69 |
|
| 70 |
public function has($key): bool |
| 71 |
{ |
| 72 |
return array_key_exists($key, $this->cache); |
| 73 |
} |
| 74 |
|
| 75 |
public function set($key, $value, $ttl = null): bool |
| 76 |
{ |
| 77 |
$this->cache[$key] = $value; |
| 78 |
|
| 79 |
return true; |
| 80 |
} |
| 81 |
|
| 82 |
public function setMultiple($values, $ttl = null): bool |
| 83 |
{ |
| 84 |
foreach ($values as $key => $value) { |
| 85 |
$this->set($key, $value); |
| 86 |
} |
| 87 |
|
| 88 |
return true; |
| 89 |
} |
| 90 |
} |
| 91 |
|