TraceableCache.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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\Simple;
  11. use Psr\SimpleCache\CacheInterface;
  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 Nicolas Grekas <p@tchwork.com>
  18. */
  19. class TraceableCache implements CacheInterface, PruneableInterface, ResettableInterface
  20. {
  21. private $pool;
  22. private $miss;
  23. private $calls = [];
  24. public function __construct(CacheInterface $pool)
  25. {
  26. $this->pool = $pool;
  27. $this->miss = new \stdClass();
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function get($key, $default = null)
  33. {
  34. $miss = null !== $default && \is_object($default) ? $default : $this->miss;
  35. $event = $this->start(__FUNCTION__);
  36. try {
  37. $value = $this->pool->get($key, $miss);
  38. } finally {
  39. $event->end = microtime(true);
  40. }
  41. if ($event->result[$key] = $miss !== $value) {
  42. ++$event->hits;
  43. } else {
  44. ++$event->misses;
  45. $value = $default;
  46. }
  47. return $value;
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function has($key)
  53. {
  54. $event = $this->start(__FUNCTION__);
  55. try {
  56. return $event->result[$key] = $this->pool->has($key);
  57. } finally {
  58. $event->end = microtime(true);
  59. }
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. public function delete($key)
  65. {
  66. $event = $this->start(__FUNCTION__);
  67. try {
  68. return $event->result[$key] = $this->pool->delete($key);
  69. } finally {
  70. $event->end = microtime(true);
  71. }
  72. }
  73. /**
  74. * {@inheritdoc}
  75. */
  76. public function set($key, $value, $ttl = null)
  77. {
  78. $event = $this->start(__FUNCTION__);
  79. try {
  80. return $event->result[$key] = $this->pool->set($key, $value, $ttl);
  81. } finally {
  82. $event->end = microtime(true);
  83. }
  84. }
  85. /**
  86. * {@inheritdoc}
  87. */
  88. public function setMultiple($values, $ttl = null)
  89. {
  90. $event = $this->start(__FUNCTION__);
  91. $event->result['keys'] = [];
  92. if ($values instanceof \Traversable) {
  93. $values = function () use ($values, $event) {
  94. foreach ($values as $k => $v) {
  95. $event->result['keys'][] = $k;
  96. yield $k => $v;
  97. }
  98. };
  99. $values = $values();
  100. } elseif (\is_array($values)) {
  101. $event->result['keys'] = array_keys($values);
  102. }
  103. try {
  104. return $event->result['result'] = $this->pool->setMultiple($values, $ttl);
  105. } finally {
  106. $event->end = microtime(true);
  107. }
  108. }
  109. /**
  110. * {@inheritdoc}
  111. */
  112. public function getMultiple($keys, $default = null)
  113. {
  114. $miss = null !== $default && \is_object($default) ? $default : $this->miss;
  115. $event = $this->start(__FUNCTION__);
  116. try {
  117. $result = $this->pool->getMultiple($keys, $miss);
  118. } finally {
  119. $event->end = microtime(true);
  120. }
  121. $f = function () use ($result, $event, $miss, $default) {
  122. $event->result = [];
  123. foreach ($result as $key => $value) {
  124. if ($event->result[$key] = $miss !== $value) {
  125. ++$event->hits;
  126. } else {
  127. ++$event->misses;
  128. $value = $default;
  129. }
  130. yield $key => $value;
  131. }
  132. };
  133. return $f();
  134. }
  135. /**
  136. * {@inheritdoc}
  137. */
  138. public function clear()
  139. {
  140. $event = $this->start(__FUNCTION__);
  141. try {
  142. return $event->result = $this->pool->clear();
  143. } finally {
  144. $event->end = microtime(true);
  145. }
  146. }
  147. /**
  148. * {@inheritdoc}
  149. */
  150. public function deleteMultiple($keys)
  151. {
  152. $event = $this->start(__FUNCTION__);
  153. if ($keys instanceof \Traversable) {
  154. $keys = $event->result['keys'] = iterator_to_array($keys, false);
  155. } else {
  156. $event->result['keys'] = $keys;
  157. }
  158. try {
  159. return $event->result['result'] = $this->pool->deleteMultiple($keys);
  160. } finally {
  161. $event->end = microtime(true);
  162. }
  163. }
  164. /**
  165. * {@inheritdoc}
  166. */
  167. public function prune()
  168. {
  169. if (!$this->pool instanceof PruneableInterface) {
  170. return false;
  171. }
  172. $event = $this->start(__FUNCTION__);
  173. try {
  174. return $event->result = $this->pool->prune();
  175. } finally {
  176. $event->end = microtime(true);
  177. }
  178. }
  179. /**
  180. * {@inheritdoc}
  181. */
  182. public function reset()
  183. {
  184. if (!$this->pool instanceof ResettableInterface) {
  185. return;
  186. }
  187. $event = $this->start(__FUNCTION__);
  188. try {
  189. $this->pool->reset();
  190. } finally {
  191. $event->end = microtime(true);
  192. }
  193. }
  194. public function getCalls()
  195. {
  196. try {
  197. return $this->calls;
  198. } finally {
  199. $this->calls = [];
  200. }
  201. }
  202. private function start($name)
  203. {
  204. $this->calls[] = $event = new TraceableCacheEvent();
  205. $event->name = $name;
  206. $event->start = microtime(true);
  207. return $event;
  208. }
  209. }
  210. class TraceableCacheEvent
  211. {
  212. public $name;
  213. public $start;
  214. public $end;
  215. public $result;
  216. public $hits = 0;
  217. public $misses = 0;
  218. }