TraceableAdapter.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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\PruneableInterface;
  13. use Symfony\Component\Cache\ResettableInterface;
  14. /**
  15. * An adapter that collects data about all cache calls.
  16. *
  17. * @author Aaron Scherer <aequasi@gmail.com>
  18. * @author Tobias Nyholm <tobias.nyholm@gmail.com>
  19. * @author Nicolas Grekas <p@tchwork.com>
  20. */
  21. class TraceableAdapter implements AdapterInterface, PruneableInterface, ResettableInterface
  22. {
  23. protected $pool;
  24. private $calls = [];
  25. public function __construct(AdapterInterface $pool)
  26. {
  27. $this->pool = $pool;
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function getItem($key)
  33. {
  34. $event = $this->start(__FUNCTION__);
  35. try {
  36. $item = $this->pool->getItem($key);
  37. } finally {
  38. $event->end = microtime(true);
  39. }
  40. if ($event->result[$key] = $item->isHit()) {
  41. ++$event->hits;
  42. } else {
  43. ++$event->misses;
  44. }
  45. return $item;
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function hasItem($key)
  51. {
  52. $event = $this->start(__FUNCTION__);
  53. try {
  54. return $event->result[$key] = $this->pool->hasItem($key);
  55. } finally {
  56. $event->end = microtime(true);
  57. }
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. public function deleteItem($key)
  63. {
  64. $event = $this->start(__FUNCTION__);
  65. try {
  66. return $event->result[$key] = $this->pool->deleteItem($key);
  67. } finally {
  68. $event->end = microtime(true);
  69. }
  70. }
  71. /**
  72. * {@inheritdoc}
  73. */
  74. public function save(CacheItemInterface $item)
  75. {
  76. $event = $this->start(__FUNCTION__);
  77. try {
  78. return $event->result[$item->getKey()] = $this->pool->save($item);
  79. } finally {
  80. $event->end = microtime(true);
  81. }
  82. }
  83. /**
  84. * {@inheritdoc}
  85. */
  86. public function saveDeferred(CacheItemInterface $item)
  87. {
  88. $event = $this->start(__FUNCTION__);
  89. try {
  90. return $event->result[$item->getKey()] = $this->pool->saveDeferred($item);
  91. } finally {
  92. $event->end = microtime(true);
  93. }
  94. }
  95. /**
  96. * {@inheritdoc}
  97. */
  98. public function getItems(array $keys = [])
  99. {
  100. $event = $this->start(__FUNCTION__);
  101. try {
  102. $result = $this->pool->getItems($keys);
  103. } finally {
  104. $event->end = microtime(true);
  105. }
  106. $f = function () use ($result, $event) {
  107. $event->result = [];
  108. foreach ($result as $key => $item) {
  109. if ($event->result[$key] = $item->isHit()) {
  110. ++$event->hits;
  111. } else {
  112. ++$event->misses;
  113. }
  114. yield $key => $item;
  115. }
  116. };
  117. return $f();
  118. }
  119. /**
  120. * {@inheritdoc}
  121. */
  122. public function clear()
  123. {
  124. $event = $this->start(__FUNCTION__);
  125. try {
  126. return $event->result = $this->pool->clear();
  127. } finally {
  128. $event->end = microtime(true);
  129. }
  130. }
  131. /**
  132. * {@inheritdoc}
  133. */
  134. public function deleteItems(array $keys)
  135. {
  136. $event = $this->start(__FUNCTION__);
  137. $event->result['keys'] = $keys;
  138. try {
  139. return $event->result['result'] = $this->pool->deleteItems($keys);
  140. } finally {
  141. $event->end = microtime(true);
  142. }
  143. }
  144. /**
  145. * {@inheritdoc}
  146. */
  147. public function commit()
  148. {
  149. $event = $this->start(__FUNCTION__);
  150. try {
  151. return $event->result = $this->pool->commit();
  152. } finally {
  153. $event->end = microtime(true);
  154. }
  155. }
  156. /**
  157. * {@inheritdoc}
  158. */
  159. public function prune()
  160. {
  161. if (!$this->pool instanceof PruneableInterface) {
  162. return false;
  163. }
  164. $event = $this->start(__FUNCTION__);
  165. try {
  166. return $event->result = $this->pool->prune();
  167. } finally {
  168. $event->end = microtime(true);
  169. }
  170. }
  171. /**
  172. * {@inheritdoc}
  173. */
  174. public function reset()
  175. {
  176. if (!$this->pool instanceof ResettableInterface) {
  177. return;
  178. }
  179. $event = $this->start(__FUNCTION__);
  180. try {
  181. $this->pool->reset();
  182. } finally {
  183. $event->end = microtime(true);
  184. }
  185. }
  186. public function getCalls()
  187. {
  188. return $this->calls;
  189. }
  190. public function clearCalls()
  191. {
  192. $this->calls = [];
  193. }
  194. protected function start($name)
  195. {
  196. $this->calls[] = $event = new TraceableAdapterEvent();
  197. $event->name = $name;
  198. $event->start = microtime(true);
  199. return $event;
  200. }
  201. }
  202. class TraceableAdapterEvent
  203. {
  204. public $name;
  205. public $start;
  206. public $end;
  207. public $result;
  208. public $hits = 0;
  209. public $misses = 0;
  210. }