1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of BiuradPHP opensource projects. |
7
|
|
|
* |
8
|
|
|
* PHP version 7.1 and above required |
9
|
|
|
* |
10
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
11
|
|
|
* @copyright 2019 Biurad Group (https://biurad.com/) |
12
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause License |
13
|
|
|
* |
14
|
|
|
* For the full copyright and license information, please view the LICENSE |
15
|
|
|
* file that was distributed with this source code. |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace Biurad\Cache; |
19
|
|
|
|
20
|
|
|
use Doctrine\Common\Cache\Cache as DoctrineCache; |
21
|
|
|
use Doctrine\Common\Cache\CacheProvider; |
22
|
|
|
use Doctrine\Common\Cache\FlushableCache; |
23
|
|
|
|
24
|
|
|
final class LoggableStorage extends CacheProvider |
25
|
|
|
{ |
26
|
|
|
public const KEY_METHOD = 'method'; |
27
|
|
|
|
28
|
|
|
public const KEY_KEY = 'key'; |
29
|
|
|
|
30
|
|
|
public const KEY_DATA = 'data'; |
31
|
|
|
|
32
|
|
|
public const KEY_OPTIONS = 'options'; |
33
|
|
|
|
34
|
|
|
/** @var DoctrineCache */ |
35
|
|
|
private $storage; |
36
|
|
|
|
37
|
|
|
/** @var mixed[] */ |
38
|
|
|
private $options = [ |
39
|
|
|
'maxCalls' => 100, |
40
|
|
|
]; |
41
|
|
|
|
42
|
|
|
/** @var mixed[] */ |
43
|
|
|
private $calls = []; |
44
|
|
|
|
45
|
123 |
|
public function __construct(DoctrineCache $storage) |
46
|
|
|
{ |
47
|
123 |
|
$this->storage = $storage; |
48
|
123 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param mixed[] $options |
52
|
|
|
*/ |
53
|
|
|
public function setOptions(array $options): void |
54
|
|
|
{ |
55
|
|
|
$this->options = \array_merge($this->options, $options); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return mixed[] |
60
|
|
|
*/ |
61
|
|
|
public function getCalls(): array |
62
|
|
|
{ |
63
|
|
|
return $this->calls; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
71 |
|
protected function doFetch($id) |
70
|
|
|
{ |
71
|
71 |
|
$data = $this->storage->fetch($id); |
72
|
|
|
|
73
|
71 |
|
$this->addLog(__FUNCTION__, $id, $data); |
74
|
|
|
|
75
|
71 |
|
return $data; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritdoc} |
80
|
|
|
*/ |
81
|
54 |
|
protected function doContains($key) |
82
|
|
|
{ |
83
|
54 |
|
return $this->storage->contains($key); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* {@inheritdoc} |
88
|
|
|
*/ |
89
|
33 |
|
protected function doSave($id, $data, $lifeTime = 0) |
90
|
|
|
{ |
91
|
33 |
|
$this->addLog(__FUNCTION__, $id, $data, ['lifetime' => $lifeTime]); |
92
|
|
|
|
93
|
33 |
|
return $this->storage->save($id, $data, $lifeTime); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritdoc} |
98
|
|
|
*/ |
99
|
25 |
|
protected function doDelete($id) |
100
|
|
|
{ |
101
|
25 |
|
$this->addLog(__FUNCTION__, $id); |
102
|
|
|
|
103
|
25 |
|
return $this->storage->delete($id); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritdoc} |
108
|
|
|
*/ |
109
|
122 |
|
protected function doFlush() |
110
|
|
|
{ |
111
|
122 |
|
if (!$this->storage instanceof FlushableCache) { |
112
|
|
|
return false; |
113
|
|
|
} |
114
|
|
|
|
115
|
122 |
|
return $this->storage->flushAll(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* {@inheritdoc} |
120
|
|
|
*/ |
121
|
|
|
protected function doGetStats() |
122
|
|
|
{ |
123
|
|
|
return $this->storage->getStats(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param string $method |
128
|
|
|
* @param mixed $key |
129
|
|
|
* @param mixed $data |
130
|
|
|
* @param mixed[] $options |
131
|
|
|
*/ |
132
|
71 |
|
private function addLog(string $method, $key, $data = null, array $options = []): void |
133
|
|
|
{ |
134
|
71 |
|
$this->calls[] = [ |
135
|
71 |
|
self::KEY_METHOD => $method, |
136
|
71 |
|
self::KEY_KEY => (string) $key, |
137
|
71 |
|
self::KEY_DATA => $data, |
138
|
71 |
|
self::KEY_OPTIONS => $options, |
139
|
|
|
]; |
140
|
|
|
|
141
|
71 |
|
if (\count($this->calls) > $this->options['maxCalls']) { |
142
|
|
|
\array_shift($this->calls); |
143
|
|
|
} |
144
|
71 |
|
} |
145
|
|
|
} |
146
|
|
|
|