TraceableCache.php 6.0 KB

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