TraceableCache.php 6.3 KB

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