TraceableAdapter.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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_class($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] = \is_object($value) ? \get_class($value) : \gettype($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. public function hasItem($key)
  81. {
  82. $event = $this->start(__FUNCTION__);
  83. try {
  84. return $event->result[$key] = $this->pool->hasItem($key);
  85. } finally {
  86. $event->end = microtime(true);
  87. }
  88. }
  89. /**
  90. * {@inheritdoc}
  91. */
  92. public function deleteItem($key)
  93. {
  94. $event = $this->start(__FUNCTION__);
  95. try {
  96. return $event->result[$key] = $this->pool->deleteItem($key);
  97. } finally {
  98. $event->end = microtime(true);
  99. }
  100. }
  101. /**
  102. * {@inheritdoc}
  103. */
  104. public function save(CacheItemInterface $item)
  105. {
  106. $event = $this->start(__FUNCTION__);
  107. try {
  108. return $event->result[$item->getKey()] = $this->pool->save($item);
  109. } finally {
  110. $event->end = microtime(true);
  111. }
  112. }
  113. /**
  114. * {@inheritdoc}
  115. */
  116. public function saveDeferred(CacheItemInterface $item)
  117. {
  118. $event = $this->start(__FUNCTION__);
  119. try {
  120. return $event->result[$item->getKey()] = $this->pool->saveDeferred($item);
  121. } finally {
  122. $event->end = microtime(true);
  123. }
  124. }
  125. /**
  126. * {@inheritdoc}
  127. */
  128. public function getItems(array $keys = [])
  129. {
  130. $event = $this->start(__FUNCTION__);
  131. try {
  132. $result = $this->pool->getItems($keys);
  133. } finally {
  134. $event->end = microtime(true);
  135. }
  136. $f = function () use ($result, $event) {
  137. $event->result = [];
  138. foreach ($result as $key => $item) {
  139. if ($event->result[$key] = $item->isHit()) {
  140. ++$event->hits;
  141. } else {
  142. ++$event->misses;
  143. }
  144. yield $key => $item;
  145. }
  146. };
  147. return $f();
  148. }
  149. /**
  150. * {@inheritdoc}
  151. */
  152. public function clear()
  153. {
  154. $event = $this->start(__FUNCTION__);
  155. try {
  156. return $event->result = $this->pool->clear();
  157. } finally {
  158. $event->end = microtime(true);
  159. }
  160. }
  161. /**
  162. * {@inheritdoc}
  163. */
  164. public function deleteItems(array $keys)
  165. {
  166. $event = $this->start(__FUNCTION__);
  167. $event->result['keys'] = $keys;
  168. try {
  169. return $event->result['result'] = $this->pool->deleteItems($keys);
  170. } finally {
  171. $event->end = microtime(true);
  172. }
  173. }
  174. /**
  175. * {@inheritdoc}
  176. */
  177. public function commit()
  178. {
  179. $event = $this->start(__FUNCTION__);
  180. try {
  181. return $event->result = $this->pool->commit();
  182. } finally {
  183. $event->end = microtime(true);
  184. }
  185. }
  186. /**
  187. * {@inheritdoc}
  188. */
  189. public function prune()
  190. {
  191. if (!$this->pool instanceof PruneableInterface) {
  192. return false;
  193. }
  194. $event = $this->start(__FUNCTION__);
  195. try {
  196. return $event->result = $this->pool->prune();
  197. } finally {
  198. $event->end = microtime(true);
  199. }
  200. }
  201. /**
  202. * {@inheritdoc}
  203. */
  204. public function reset()
  205. {
  206. if (!$this->pool instanceof ResetInterface) {
  207. return;
  208. }
  209. $event = $this->start(__FUNCTION__);
  210. try {
  211. $this->pool->reset();
  212. } finally {
  213. $event->end = microtime(true);
  214. }
  215. }
  216. /**
  217. * {@inheritdoc}
  218. */
  219. public function delete(string $key): bool
  220. {
  221. $event = $this->start(__FUNCTION__);
  222. try {
  223. return $event->result[$key] = $this->pool->deleteItem($key);
  224. } finally {
  225. $event->end = microtime(true);
  226. }
  227. }
  228. public function getCalls()
  229. {
  230. return $this->calls;
  231. }
  232. public function clearCalls()
  233. {
  234. $this->calls = [];
  235. }
  236. protected function start($name)
  237. {
  238. $this->calls[] = $event = new TraceableAdapterEvent();
  239. $event->name = $name;
  240. $event->start = microtime(true);
  241. return $event;
  242. }
  243. }
  244. class TraceableAdapterEvent
  245. {
  246. public $name;
  247. public $start;
  248. public $end;
  249. public $result;
  250. public $hits = 0;
  251. public $misses = 0;
  252. }