TraceableAdapter.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. private array $calls = [];
  27. public function __construct(
  28. protected AdapterInterface $pool,
  29. ) {
  30. }
  31. public function get(string $key, callable $callback, ?float $beta = null, ?array &$metadata = null): mixed
  32. {
  33. if (!$this->pool instanceof CacheInterface) {
  34. throw new \BadMethodCallException(\sprintf('Cannot call "%s::get()": this class doesn\'t implement "%s".', get_debug_type($this->pool), CacheInterface::class));
  35. }
  36. $isHit = true;
  37. $callback = function (CacheItem $item, bool &$save) use ($callback, &$isHit) {
  38. $isHit = $item->isHit();
  39. return $callback($item, $save);
  40. };
  41. $event = $this->start(__FUNCTION__);
  42. try {
  43. $value = $this->pool->get($key, $callback, $beta, $metadata);
  44. $event->result[$key] = get_debug_type($value);
  45. } finally {
  46. $event->end = microtime(true);
  47. }
  48. if ($isHit) {
  49. ++$event->hits;
  50. } else {
  51. ++$event->misses;
  52. }
  53. return $value;
  54. }
  55. public function getItem(mixed $key): CacheItem
  56. {
  57. $event = $this->start(__FUNCTION__);
  58. try {
  59. $item = $this->pool->getItem($key);
  60. } finally {
  61. $event->end = microtime(true);
  62. }
  63. if ($event->result[$key] = $item->isHit()) {
  64. ++$event->hits;
  65. } else {
  66. ++$event->misses;
  67. }
  68. return $item;
  69. }
  70. public function hasItem(mixed $key): bool
  71. {
  72. $event = $this->start(__FUNCTION__);
  73. try {
  74. return $event->result[$key] = $this->pool->hasItem($key);
  75. } finally {
  76. $event->end = microtime(true);
  77. }
  78. }
  79. public function deleteItem(mixed $key): bool
  80. {
  81. $event = $this->start(__FUNCTION__);
  82. try {
  83. return $event->result[$key] = $this->pool->deleteItem($key);
  84. } finally {
  85. $event->end = microtime(true);
  86. }
  87. }
  88. public function save(CacheItemInterface $item): bool
  89. {
  90. $event = $this->start(__FUNCTION__);
  91. try {
  92. return $event->result[$item->getKey()] = $this->pool->save($item);
  93. } finally {
  94. $event->end = microtime(true);
  95. }
  96. }
  97. public function saveDeferred(CacheItemInterface $item): bool
  98. {
  99. $event = $this->start(__FUNCTION__);
  100. try {
  101. return $event->result[$item->getKey()] = $this->pool->saveDeferred($item);
  102. } finally {
  103. $event->end = microtime(true);
  104. }
  105. }
  106. public function getItems(array $keys = []): iterable
  107. {
  108. $event = $this->start(__FUNCTION__);
  109. try {
  110. $result = $this->pool->getItems($keys);
  111. } finally {
  112. $event->end = microtime(true);
  113. }
  114. $f = function () use ($result, $event) {
  115. $event->result = [];
  116. foreach ($result as $key => $item) {
  117. if ($event->result[$key] = $item->isHit()) {
  118. ++$event->hits;
  119. } else {
  120. ++$event->misses;
  121. }
  122. yield $key => $item;
  123. }
  124. };
  125. return $f();
  126. }
  127. public function clear(string $prefix = ''): bool
  128. {
  129. $event = $this->start(__FUNCTION__);
  130. try {
  131. if ($this->pool instanceof AdapterInterface) {
  132. return $event->result = $this->pool->clear($prefix);
  133. }
  134. return $event->result = $this->pool->clear();
  135. } finally {
  136. $event->end = microtime(true);
  137. }
  138. }
  139. public function deleteItems(array $keys): bool
  140. {
  141. $event = $this->start(__FUNCTION__);
  142. $event->result['keys'] = $keys;
  143. try {
  144. return $event->result['result'] = $this->pool->deleteItems($keys);
  145. } finally {
  146. $event->end = microtime(true);
  147. }
  148. }
  149. public function commit(): bool
  150. {
  151. $event = $this->start(__FUNCTION__);
  152. try {
  153. return $event->result = $this->pool->commit();
  154. } finally {
  155. $event->end = microtime(true);
  156. }
  157. }
  158. public function prune(): bool
  159. {
  160. if (!$this->pool instanceof PruneableInterface) {
  161. return false;
  162. }
  163. $event = $this->start(__FUNCTION__);
  164. try {
  165. return $event->result = $this->pool->prune();
  166. } finally {
  167. $event->end = microtime(true);
  168. }
  169. }
  170. public function reset(): void
  171. {
  172. if ($this->pool instanceof ResetInterface) {
  173. $this->pool->reset();
  174. }
  175. $this->clearCalls();
  176. }
  177. public function delete(string $key): bool
  178. {
  179. $event = $this->start(__FUNCTION__);
  180. try {
  181. return $event->result[$key] = $this->pool->deleteItem($key);
  182. } finally {
  183. $event->end = microtime(true);
  184. }
  185. }
  186. public function getCalls(): array
  187. {
  188. return $this->calls;
  189. }
  190. public function clearCalls(): void
  191. {
  192. $this->calls = [];
  193. }
  194. public function getPool(): AdapterInterface
  195. {
  196. return $this->pool;
  197. }
  198. protected function start(string $name): TraceableAdapterEvent
  199. {
  200. $this->calls[] = $event = new TraceableAdapterEvent();
  201. $event->name = $name;
  202. $event->start = microtime(true);
  203. return $event;
  204. }
  205. }
  206. /**
  207. * @internal
  208. */
  209. class TraceableAdapterEvent
  210. {
  211. public string $name;
  212. public float $start;
  213. public float $end;
  214. public array|bool $result;
  215. public int $hits = 0;
  216. public int $misses = 0;
  217. }