1 | <?php |
||
2 | namespace Elgg\Cache\Pool; |
||
3 | |||
4 | use Elgg\Cache\Pool; |
||
5 | use Stash; |
||
6 | use InvalidArgumentException; |
||
7 | |||
8 | /** |
||
9 | * An in-memory implementation of a cache pool. |
||
10 | * |
||
11 | * NB: Data put into this cache is not persisted between requests. |
||
12 | * |
||
13 | * WARNING: API IN FLUX. DO NOT USE DIRECTLY. |
||
14 | * |
||
15 | * @since 1.10.0 |
||
16 | * |
||
17 | * @access private |
||
18 | */ |
||
19 | final class InMemory implements Pool { |
||
20 | /** |
||
21 | * @var array |
||
22 | */ |
||
23 | private $values = []; |
||
24 | |||
25 | /** @inheritDoc */ |
||
26 | 7 | public function get($key, callable $callback = null, $default = null) { |
|
27 | 7 | if (!is_string($key) && !is_int($key)) { |
|
28 | throw new InvalidArgumentException('key must be string or integer'); |
||
29 | } |
||
30 | |||
31 | 7 | if (!array_key_exists($key, $this->values)) { |
|
32 | 7 | if (!$callback) { |
|
33 | return $default; |
||
34 | } |
||
35 | 7 | $this->values[$key] = call_user_func($callback); |
|
36 | } |
||
37 | 7 | return $this->values[$key]; |
|
38 | } |
||
39 | |||
40 | /** @inheritDoc */ |
||
41 | 1 | View Code Duplication | public function invalidate($key) { |
0 ignored issues
–
show
|
|||
42 | 1 | if (!is_string($key) && !is_int($key)) { |
|
43 | throw new InvalidArgumentException('key must be string or integer'); |
||
44 | } |
||
45 | |||
46 | 1 | unset($this->values[$key]); |
|
47 | 1 | } |
|
48 | |||
49 | /** @inheritDoc */ |
||
50 | 11 | View Code Duplication | public function put($key, $value) { |
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
51 | 11 | if (!is_string($key) && !is_int($key)) { |
|
52 | throw new InvalidArgumentException('key must be string or integer'); |
||
53 | } |
||
54 | |||
55 | 11 | $this->values[$key] = $value; |
|
56 | 11 | } |
|
57 | } |
||
58 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.