|
11 | 11 |
|
12 | 12 | namespace Cache\CacheBundle\Cache; |
13 | 13 |
|
14 | | -use Cache\Taggable\TaggablePoolInterface; |
15 | | -use Psr\Cache\CacheItemInterface; |
16 | | -use Psr\Cache\CacheItemPoolInterface; |
| 14 | +use Monolog\Logger; |
| 15 | +use Psr\Log\LoggerInterface; |
17 | 16 |
|
18 | 17 | /** |
19 | | - * @author Aaron Scherer <aequasi@gmail.com> |
| 18 | + * Logg all calls to the cache. |
| 19 | + * |
| 20 | + * @author Tobias Nyholm <tobias.nyholm@gmail.com> |
20 | 21 | */ |
21 | | -class LoggingCachePool implements CacheItemPoolInterface, TaggablePoolInterface |
| 22 | +class LoggingCachePool extends RecordingCachePool |
22 | 23 | { |
23 | 24 | /** |
24 | | - * @type array |
| 25 | + * @type Logger |
25 | 26 | */ |
26 | | - private $calls = []; |
| 27 | + private $logger; |
27 | 28 |
|
28 | 29 | /** |
29 | | - * @type CacheItemPoolInterface |
| 30 | + * @type string |
30 | 31 | */ |
31 | | - private $cachePool; |
| 32 | + private $name; |
32 | 33 |
|
33 | 34 | /** |
34 | | - * LoggingCachePool constructor. |
35 | | - * |
36 | | - * @param CacheItemPoolInterface $cachePool |
| 35 | + * @type string |
37 | 36 | */ |
38 | | - public function __construct(CacheItemPoolInterface $cachePool) |
39 | | - { |
40 | | - $this->cachePool = $cachePool; |
41 | | - } |
| 37 | + private $level = 'info'; |
42 | 38 |
|
43 | 39 | /** |
44 | | - * @param string $name |
45 | | - * @param array $arguments |
| 40 | + * @param LoggerInterface $logger |
46 | 41 | * |
47 | | - * @return object |
| 42 | + * @return LoggingCachePool |
48 | 43 | */ |
49 | | - private function timeCall($name, array $arguments = null) |
50 | | - { |
51 | | - $start = microtime(true); |
52 | | - $result = call_user_func_array([$this->cachePool, $name], $arguments); |
53 | | - $time = microtime(true) - $start; |
54 | | - |
55 | | - $object = (object) compact('name', 'arguments', 'start', 'time', 'result'); |
56 | | - |
57 | | - return $object; |
58 | | - } |
59 | | - |
60 | | - public function getItem($key, array $tags = []) |
61 | | - { |
62 | | - $call = $this->timeCall(__FUNCTION__, [$key, $tags]); |
63 | | - $result = $call->result; |
64 | | - $call->isHit = $result->isHit(); |
65 | | - |
66 | | - // Display the result in a good way depending on the data type |
67 | | - if ($call->isHit) { |
68 | | - $call->result = $this->getValueRepresentation($result->get()); |
69 | | - } else { |
70 | | - $call->result = null; |
71 | | - } |
72 | | - |
73 | | - $this->calls[] = $call; |
74 | | - |
75 | | - return $result; |
76 | | - } |
77 | | - |
78 | | - public function hasItem($key, array $tags = []) |
79 | | - { |
80 | | - $call = $this->timeCall(__FUNCTION__, [$key, $tags]); |
81 | | - $this->calls[] = $call; |
82 | | - |
83 | | - return $call->result; |
84 | | - } |
85 | | - |
86 | | - public function deleteItem($key, array $tags = []) |
87 | | - { |
88 | | - $call = $this->timeCall(__FUNCTION__, [$key, $tags]); |
89 | | - $this->calls[] = $call; |
90 | | - |
91 | | - return $call->result; |
92 | | - } |
93 | | - |
94 | | - public function save(CacheItemInterface $item) |
95 | | - { |
96 | | - $key = $item->getKey(); |
97 | | - $value = $this->getValueRepresentation($item->get()); |
98 | | - |
99 | | - $call = $this->timeCall(__FUNCTION__, [$item]); |
100 | | - $call->arguments = ['<CacheItem>', $key, $value]; |
101 | | - $this->calls[] = $call; |
102 | | - |
103 | | - return $call->result; |
104 | | - } |
105 | | - |
106 | | - public function getItems(array $keys = [], array $tags = []) |
107 | | - { |
108 | | - $call = $this->timeCall(__FUNCTION__, [$keys, $tags]); |
109 | | - $result = $call->result; |
110 | | - $call->result = sprintf('<DATA:%s>', gettype($result)); |
111 | | - |
112 | | - $this->calls[] = $call; |
113 | | - |
114 | | - return $result; |
115 | | - } |
116 | | - |
117 | | - public function clear(array $tags = []) |
118 | | - { |
119 | | - $call = $this->timeCall(__FUNCTION__, [$tags]); |
120 | | - $this->calls[] = $call; |
121 | | - |
122 | | - return $call->result; |
123 | | - } |
124 | | - |
125 | | - public function deleteItems(array $keys, array $tags = []) |
126 | | - { |
127 | | - $call = $this->timeCall(__FUNCTION__, [$keys, $tags]); |
128 | | - $this->calls[] = $call; |
129 | | - |
130 | | - return $call->result; |
131 | | - } |
132 | | - |
133 | | - public function saveDeferred(CacheItemInterface $item) |
| 44 | + public function setLogger($logger) |
134 | 45 | { |
135 | | - $itemClone = clone $item; |
136 | | - $itemClone->set(sprintf('<DATA:%s', gettype($item->get()))); |
137 | | - |
138 | | - $call = $this->timeCall(__FUNCTION__, [$item]); |
139 | | - $call->arguments = ['<CacheItem>', $itemClone]; |
140 | | - $this->calls[] = $call; |
| 46 | + $this->logger = $logger; |
141 | 47 |
|
142 | | - return $call->result; |
| 48 | + return $this; |
143 | 49 | } |
144 | 50 |
|
145 | | - public function commit() |
| 51 | + /** |
| 52 | + * @param string $name |
| 53 | + * |
| 54 | + * @return LoggingCachePool |
| 55 | + */ |
| 56 | + public function setName($name) |
146 | 57 | { |
147 | | - $call = $this->timeCall(__FUNCTION__); |
148 | | - $this->calls[] = $call; |
| 58 | + $this->name = $name; |
149 | 59 |
|
150 | | - return $call->result; |
| 60 | + return $this; |
151 | 61 | } |
152 | 62 |
|
153 | 63 | /** |
154 | | - * @return array |
| 64 | + * @param string $level |
| 65 | + * |
| 66 | + * @return LoggingCachePool |
155 | 67 | */ |
156 | | - public function getCalls() |
| 68 | + public function setLevel($level) |
157 | 69 | { |
158 | | - return $this->calls; |
| 70 | + $this->level = $level; |
| 71 | + |
| 72 | + return $this; |
159 | 73 | } |
160 | 74 |
|
161 | 75 | /** |
162 | | - * Get a string to represent the value. |
163 | | - * |
164 | | - * @param mixed $value |
165 | | - * |
166 | | - * @return string |
| 76 | + * @param $call |
167 | 77 | */ |
168 | | - private function getValueRepresentation($value) |
| 78 | + protected function addCall($call) |
169 | 79 | { |
170 | | - $type = gettype($value); |
171 | | - if (in_array($type, ['boolean', 'integer', 'double', 'string', 'NULL'])) { |
172 | | - $rep = $value; |
173 | | - } elseif ($type === 'array') { |
174 | | - $rep = json_encode($value); |
175 | | - } elseif ($type === 'object') { |
176 | | - $rep = get_class($value); |
177 | | - } else { |
178 | | - $rep = sprintf('<DATA:%s>', $type); |
179 | | - } |
180 | | - |
181 | | - return $rep; |
| 80 | + $data = [ |
| 81 | + 'name' => $this->name, |
| 82 | + 'method' => $call->name, |
| 83 | + 'arguments' => json_encode($call->arguments), |
| 84 | + 'hit' => isset($call->isHit) ? $call->isHit ? 'True' : 'False' : 'Invalid', |
| 85 | + 'time' => round($call->time * 1000, 2), |
| 86 | + 'result' => $call->result, |
| 87 | + ]; |
| 88 | + |
| 89 | + $this->logger->log( |
| 90 | + $this->level, |
| 91 | + sprintf('[Cache] Provider: %s. Method: %s(%s). Hit: %s. Time: %sms. Result: %s', |
| 92 | + $data['name'], |
| 93 | + $data['method'], |
| 94 | + $data['arguments'], |
| 95 | + $data['hit'], |
| 96 | + $data['time'], |
| 97 | + $data['result'] |
| 98 | + ), |
| 99 | + $data |
| 100 | + ); |
182 | 101 | } |
183 | 102 | } |
0 commit comments