TraceableAdapter.php 8.7 KB

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