TraceableAdapter.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Cache\Adapter;
  11. use Psr\Cache\CacheItemInterface;
  12. use Symfony\Component\Cache\CacheItem;
  13. use Symfony\Component\Cache\PruneableInterface;
  14. use Symfony\Component\Cache\ResettableInterface;
  15. use Symfony\Contracts\Cache\CacheInterface;
  16. use Symfony\Contracts\Service\ResetInterface;
  17. /**
  18. * An adapter that collects data about all cache calls.
  19. *
  20. * @author Aaron Scherer <aequasi@gmail.com>
  21. * @author Tobias Nyholm <tobias.nyholm@gmail.com>
  22. * @author Nicolas Grekas <p@tchwork.com>
  23. */
  24. class TraceableAdapter implements AdapterInterface, CacheInterface, PruneableInterface, ResettableInterface
  25. {
  26. protected $pool;
  27. private $calls = [];
  28. public function __construct(AdapterInterface $pool)
  29. {
  30. $this->pool = $pool;
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function get(string $key, callable $callback, float $beta = null, array &$metadata = null)
  36. {
  37. if (!$this->pool instanceof CacheInterface) {
  38. throw new \BadMethodCallException(sprintf('Cannot call "%s::get()": this class doesn\'t implement "%s".', get_debug_type($this->pool), CacheInterface::class));
  39. }
  40. $isHit = true;
  41. $callback = function (CacheItem $item, bool &$save) use ($callback, &$isHit) {
  42. $isHit = $item->isHit();
  43. return $callback($item, $save);
  44. };
  45. $event = $this->start(__FUNCTION__);
  46. try {
  47. $value = $this->pool->get($key, $callback, $beta, $metadata);
  48. $event->result[$key] = get_debug_type($value);
  49. } finally {
  50. $event->end = microtime(true);
  51. }
  52. if ($isHit) {
  53. ++$event->hits;
  54. } else {
  55. ++$event->misses;
  56. }
  57. return $value;
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. public function getItem($key)
  63. {
  64. $event = $this->start(__FUNCTION__);
  65. try {
  66. $item = $this->pool->getItem($key);
  67. } finally {
  68. $event->end = microtime(true);
  69. }
  70. if ($event->result[$key] = $item->isHit()) {
  71. ++$event->hits;
  72. } else {
  73. ++$event->misses;
  74. }
  75. return $item;
  76. }
  77. /**
  78. * {@inheritdoc}
  79. *
  80. * @return bool
  81. */
  82. public function hasItem($key)
  83. {
  84. $event = $this->start(__FUNCTION__);
  85. try {
  86. return $event->result[$key] = $this->pool->hasItem($key);
  87. } finally {
  88. $event->end = microtime(true);
  89. }
  90. }
  91. /**
  92. * {@inheritdoc}
  93. *
  94. * @return bool
  95. */
  96. public function deleteItem($key)
  97. {
  98. $event = $this->start(__FUNCTION__);
  99. try {
  100. return $event->result[$key] = $this->pool->deleteItem($key);
  101. } finally {
  102. $event->end = microtime(true);
  103. }
  104. }
  105. /**
  106. * {@inheritdoc}
  107. *
  108. * @return bool
  109. */
  110. public function save(CacheItemInterface $item)
  111. {
  112. $event = $this->start(__FUNCTION__);
  113. try {
  114. return $event->result[$item->getKey()] = $this->pool->save($item);
  115. } finally {
  116. $event->end = microtime(true);
  117. }
  118. }
  119. /**
  120. * {@inheritdoc}
  121. *
  122. * @return bool
  123. */
  124. public function saveDeferred(CacheItemInterface $item)
  125. {
  126. $event = $this->start(__FUNCTION__);
  127. try {
  128. return $event->result[$item->getKey()] = $this->pool->saveDeferred($item);
  129. } finally {
  130. $event->end = microtime(true);
  131. }
  132. }
  133. /**
  134. * {@inheritdoc}
  135. */
  136. public function getItems(array $keys = [])
  137. {
  138. $event = $this->start(__FUNCTION__);
  139. try {
  140. $result = $this->pool->getItems($keys);
  141. } finally {
  142. $event->end = microtime(true);
  143. }
  144. $f = function () use ($result, $event) {
  145. $event->result = [];
  146. foreach ($result as $key => $item) {
  147. if ($event->result[$key] = $item->isHit()) {
  148. ++$event->hits;
  149. } else {
  150. ++$event->misses;
  151. }
  152. yield $key => $item;
  153. }
  154. };
  155. return $f();
  156. }
  157. /**
  158. * {@inheritdoc}
  159. *
  160. * @return bool
  161. */
  162. public function clear(string $prefix = '')
  163. {
  164. $event = $this->start(__FUNCTION__);
  165. try {
  166. if ($this->pool instanceof AdapterInterface) {
  167. return $event->result = $this->pool->clear($prefix);
  168. }
  169. return $event->result = $this->pool->clear();
  170. } finally {
  171. $event->end = microtime(true);
  172. }
  173. }
  174. /**
  175. * {@inheritdoc}
  176. *
  177. * @return bool
  178. */
  179. public function deleteItems(array $keys)
  180. {
  181. $event = $this->start(__FUNCTION__);
  182. $event->result['keys'] = $keys;
  183. try {
  184. return $event->result['result'] = $this->pool->deleteItems($keys);
  185. } finally {
  186. $event->end = microtime(true);
  187. }
  188. }
  189. /**
  190. * {@inheritdoc}
  191. *
  192. * @return bool
  193. */
  194. public function commit()
  195. {
  196. $event = $this->start(__FUNCTION__);
  197. try {
  198. return $event->result = $this->pool->commit();
  199. } finally {
  200. $event->end = microtime(true);
  201. }
  202. }
  203. /**
  204. * {@inheritdoc}
  205. */
  206. public function prune()
  207. {
  208. if (!$this->pool instanceof PruneableInterface) {
  209. return false;
  210. }
  211. $event = $this->start(__FUNCTION__);
  212. try {
  213. return $event->result = $this->pool->prune();
  214. } finally {
  215. $event->end = microtime(true);
  216. }
  217. }
  218. /**
  219. * {@inheritdoc}
  220. */
  221. public function reset()
  222. {
  223. if ($this->pool instanceof ResetInterface) {
  224. $this->pool->reset();
  225. }
  226. $this->clearCalls();
  227. }
  228. /**
  229. * {@inheritdoc}
  230. */
  231. public function delete(string $key): bool
  232. {
  233. $event = $this->start(__FUNCTION__);
  234. try {
  235. return $event->result[$key] = $this->pool->deleteItem($key);
  236. } finally {
  237. $event->end = microtime(true);
  238. }
  239. }
  240. public function getCalls()
  241. {
  242. return $this->calls;
  243. }
  244. public function clearCalls()
  245. {
  246. $this->calls = [];
  247. }
  248. protected function start(string $name)
  249. {
  250. $this->calls[] = $event = new TraceableAdapterEvent();
  251. $event->name = $name;
  252. $event->start = microtime(true);
  253. return $event;
  254. }
  255. }
  256. class TraceableAdapterEvent
  257. {
  258. public $name;
  259. public $start;
  260. public $end;
  261. public $result;
  262. public $hits = 0;
  263. public $misses = 0;
  264. }