TraceableAdapter.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. * @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. * @param string $prefix
  161. *
  162. * @return bool
  163. */
  164. public function clear(/* string $prefix = '' */)
  165. {
  166. $prefix = 0 < \func_num_args() ? (string) func_get_arg(0) : '';
  167. $event = $this->start(__FUNCTION__);
  168. try {
  169. if ($this->pool instanceof AdapterInterface) {
  170. return $event->result = $this->pool->clear($prefix);
  171. }
  172. return $event->result = $this->pool->clear();
  173. } finally {
  174. $event->end = microtime(true);
  175. }
  176. }
  177. /**
  178. * {@inheritdoc}
  179. *
  180. * @return bool
  181. */
  182. public function deleteItems(array $keys)
  183. {
  184. $event = $this->start(__FUNCTION__);
  185. $event->result['keys'] = $keys;
  186. try {
  187. return $event->result['result'] = $this->pool->deleteItems($keys);
  188. } finally {
  189. $event->end = microtime(true);
  190. }
  191. }
  192. /**
  193. * {@inheritdoc}
  194. *
  195. * @return bool
  196. */
  197. public function commit()
  198. {
  199. $event = $this->start(__FUNCTION__);
  200. try {
  201. return $event->result = $this->pool->commit();
  202. } finally {
  203. $event->end = microtime(true);
  204. }
  205. }
  206. /**
  207. * {@inheritdoc}
  208. */
  209. public function prune()
  210. {
  211. if (!$this->pool instanceof PruneableInterface) {
  212. return false;
  213. }
  214. $event = $this->start(__FUNCTION__);
  215. try {
  216. return $event->result = $this->pool->prune();
  217. } finally {
  218. $event->end = microtime(true);
  219. }
  220. }
  221. /**
  222. * {@inheritdoc}
  223. */
  224. public function reset()
  225. {
  226. if ($this->pool instanceof ResetInterface) {
  227. $this->pool->reset();
  228. }
  229. $this->clearCalls();
  230. }
  231. /**
  232. * {@inheritdoc}
  233. */
  234. public function delete(string $key): bool
  235. {
  236. $event = $this->start(__FUNCTION__);
  237. try {
  238. return $event->result[$key] = $this->pool->deleteItem($key);
  239. } finally {
  240. $event->end = microtime(true);
  241. }
  242. }
  243. public function getCalls()
  244. {
  245. return $this->calls;
  246. }
  247. public function clearCalls()
  248. {
  249. $this->calls = [];
  250. }
  251. protected function start($name)
  252. {
  253. $this->calls[] = $event = new TraceableAdapterEvent();
  254. $event->name = $name;
  255. $event->start = microtime(true);
  256. return $event;
  257. }
  258. }
  259. class TraceableAdapterEvent
  260. {
  261. public $name;
  262. public $start;
  263. public $end;
  264. public $result;
  265. public $hits = 0;
  266. public $misses = 0;
  267. }