1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpWinTools\WmiScripting\Support\Cache; |
4
|
|
|
|
5
|
|
|
use Psr\SimpleCache\CacheInterface; |
6
|
|
|
use PhpWinTools\WmiScripting\Exceptions\CacheInvalidArgumentException; |
7
|
|
|
|
8
|
|
|
class ArrayDriver extends CacheDriver implements CacheInterface |
9
|
|
|
{ |
10
|
|
|
protected $store = []; |
11
|
|
|
|
12
|
|
|
public function get($key, $default = null) |
13
|
|
|
{ |
14
|
|
|
if ($this->has($key)) { |
15
|
|
|
return $this->store[$key]; |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
return is_callable($default) ? $default() : $default; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function set($key, $value, $ttl = null) |
22
|
|
|
{ |
23
|
|
|
$this->store[$this->validateKey($key)] = $value; |
24
|
|
|
|
25
|
|
|
return true; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function delete($key) |
29
|
|
|
{ |
30
|
|
|
if ($this->has($key)) { |
31
|
|
|
unset($this->store[$key]); |
32
|
|
|
return true; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
return false; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function clear() |
39
|
|
|
{ |
40
|
|
|
$this->store = []; |
41
|
|
|
|
42
|
|
|
return true; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function getMultiple($keys, $default = null) |
46
|
|
|
{ |
47
|
|
|
$result = []; |
48
|
|
|
|
49
|
|
|
foreach ($keys as $key) { |
50
|
|
|
$result[$key] = $this->get($key, $default); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $result; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function setMultiple($values, $ttl = null) |
57
|
|
|
{ |
58
|
|
|
foreach ($values as $key => $value) { |
59
|
|
|
$this->set($key, $value); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return true; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function deleteMultiple($keys) |
66
|
|
|
{ |
67
|
|
|
foreach ($keys as $key) { |
68
|
|
|
if ($this->delete($key)) { |
69
|
|
|
continue; |
70
|
|
|
} else { |
71
|
|
|
return false; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return true; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function has($key) |
79
|
|
|
{ |
80
|
|
|
return array_key_exists($this->validateKey($key), $this->store); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function exists($key) |
84
|
|
|
{ |
85
|
|
|
return $this->has($key); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function doesNotExist($key) |
89
|
|
|
{ |
90
|
|
|
return $this->exists($key) === false; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
protected function validateKey($key) |
94
|
|
|
{ |
95
|
|
|
if (is_string($key)) { |
96
|
|
|
return $key; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
throw new CacheInvalidArgumentException("{$key} is not a valid key"); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|