vendor/symfony/cache/Adapter/TraceableAdapter.php line 77

Open in your IDE?
  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\PruneableInterface;
  14. use Symfony\Component\Cache\ResettableInterface;
  15. use Symfony\Contracts\Cache\CacheInterface;
  16. use Symfony\Contracts\Service\ResetInterface;
  17. /**
  18.  * An adapter that collects data about all cache calls.
  19.  *
  20.  * @author Aaron Scherer <aequasi@gmail.com>
  21.  * @author Tobias Nyholm <tobias.nyholm@gmail.com>
  22.  * @author Nicolas Grekas <p@tchwork.com>
  23.  */
  24. class TraceableAdapter implements AdapterInterfaceCacheInterfacePruneableInterfaceResettableInterface
  25. {
  26.     protected $pool;
  27.     private $calls = array();
  28.     public function __construct(AdapterInterface $pool)
  29.     {
  30.         $this->pool $pool;
  31.     }
  32.     /**
  33.      * {@inheritdoc}
  34.      */
  35.     public function get(string $key, callable $callbackfloat $beta null, array &$metadata null)
  36.     {
  37.         if (!$this->pool instanceof CacheInterface) {
  38.             throw new \BadMethodCallException(sprintf('Cannot call "%s::get()": this class doesn\'t implement "%s".', \get_class($this->pool), CacheInterface::class));
  39.         }
  40.         $isHit true;
  41.         $callback = function (CacheItem $item) use ($callback, &$isHit) {
  42.             $isHit $item->isHit();
  43.             return $callback($item);
  44.         };
  45.         $event $this->start(__FUNCTION__);
  46.         try {
  47.             $value $this->pool->get($key$callback$beta$metadata);
  48.             $event->result[$key] = \is_object($value) ? \get_class($value) : \gettype($value);
  49.         } finally {
  50.             $event->end microtime(true);
  51.         }
  52.         if ($isHit) {
  53.             ++$event->hits;
  54.         } else {
  55.             ++$event->misses;
  56.         }
  57.         return $value;
  58.     }
  59.     /**
  60.      * {@inheritdoc}
  61.      */
  62.     public function getItem($key)
  63.     {
  64.         $event $this->start(__FUNCTION__);
  65.         try {
  66.             $item $this->pool->getItem($key);
  67.         } finally {
  68.             $event->end microtime(true);
  69.         }
  70.         if ($event->result[$key] = $item->isHit()) {
  71.             ++$event->hits;
  72.         } else {
  73.             ++$event->misses;
  74.         }
  75.         return $item;
  76.     }
  77.     /**
  78.      * {@inheritdoc}
  79.      */
  80.     public function hasItem($key)
  81.     {
  82.         $event $this->start(__FUNCTION__);
  83.         try {
  84.             return $event->result[$key] = $this->pool->hasItem($key);
  85.         } finally {
  86.             $event->end microtime(true);
  87.         }
  88.     }
  89.     /**
  90.      * {@inheritdoc}
  91.      */
  92.     public function deleteItem($key)
  93.     {
  94.         $event $this->start(__FUNCTION__);
  95.         try {
  96.             return $event->result[$key] = $this->pool->deleteItem($key);
  97.         } finally {
  98.             $event->end microtime(true);
  99.         }
  100.     }
  101.     /**
  102.      * {@inheritdoc}
  103.      */
  104.     public function save(CacheItemInterface $item)
  105.     {
  106.         $event $this->start(__FUNCTION__);
  107.         try {
  108.             return $event->result[$item->getKey()] = $this->pool->save($item);
  109.         } finally {
  110.             $event->end microtime(true);
  111.         }
  112.     }
  113.     /**
  114.      * {@inheritdoc}
  115.      */
  116.     public function saveDeferred(CacheItemInterface $item)
  117.     {
  118.         $event $this->start(__FUNCTION__);
  119.         try {
  120.             return $event->result[$item->getKey()] = $this->pool->saveDeferred($item);
  121.         } finally {
  122.             $event->end microtime(true);
  123.         }
  124.     }
  125.     /**
  126.      * {@inheritdoc}
  127.      */
  128.     public function getItems(array $keys = array())
  129.     {
  130.         $event $this->start(__FUNCTION__);
  131.         try {
  132.             $result $this->pool->getItems($keys);
  133.         } finally {
  134.             $event->end microtime(true);
  135.         }
  136.         $f = function () use ($result$event) {
  137.             $event->result = array();
  138.             foreach ($result as $key => $item) {
  139.                 if ($event->result[$key] = $item->isHit()) {
  140.                     ++$event->hits;
  141.                 } else {
  142.                     ++$event->misses;
  143.                 }
  144.                 yield $key => $item;
  145.             }
  146.         };
  147.         return $f();
  148.     }
  149.     /**
  150.      * {@inheritdoc}
  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.     public function deleteItems(array $keys)
  165.     {
  166.         $event $this->start(__FUNCTION__);
  167.         $event->result['keys'] = $keys;
  168.         try {
  169.             return $event->result['result'] = $this->pool->deleteItems($keys);
  170.         } finally {
  171.             $event->end microtime(true);
  172.         }
  173.     }
  174.     /**
  175.      * {@inheritdoc}
  176.      */
  177.     public function commit()
  178.     {
  179.         $event $this->start(__FUNCTION__);
  180.         try {
  181.             return $event->result $this->pool->commit();
  182.         } finally {
  183.             $event->end microtime(true);
  184.         }
  185.     }
  186.     /**
  187.      * {@inheritdoc}
  188.      */
  189.     public function prune()
  190.     {
  191.         if (!$this->pool instanceof PruneableInterface) {
  192.             return false;
  193.         }
  194.         $event $this->start(__FUNCTION__);
  195.         try {
  196.             return $event->result $this->pool->prune();
  197.         } finally {
  198.             $event->end microtime(true);
  199.         }
  200.     }
  201.     /**
  202.      * {@inheritdoc}
  203.      */
  204.     public function reset()
  205.     {
  206.         if (!$this->pool instanceof ResetInterface) {
  207.             return;
  208.         }
  209.         $event $this->start(__FUNCTION__);
  210.         try {
  211.             $this->pool->reset();
  212.         } finally {
  213.             $event->end microtime(true);
  214.         }
  215.     }
  216.     /**
  217.      * {@inheritdoc}
  218.      */
  219.     public function delete(string $key): bool
  220.     {
  221.         $event $this->start(__FUNCTION__);
  222.         try {
  223.             return $event->result[$key] = $this->pool->deleteItem($key);
  224.         } finally {
  225.             $event->end microtime(true);
  226.         }
  227.     }
  228.     public function getCalls()
  229.     {
  230.         return $this->calls;
  231.     }
  232.     public function clearCalls()
  233.     {
  234.         $this->calls = array();
  235.     }
  236.     protected function start($name)
  237.     {
  238.         $this->calls[] = $event = new TraceableAdapterEvent();
  239.         $event->name $name;
  240.         $event->start microtime(true);
  241.         return $event;
  242.     }
  243. }
  244. class TraceableAdapterEvent
  245. {
  246.     public $name;
  247.     public $start;
  248.     public $end;
  249.     public $result;
  250.     public $hits 0;
  251.     public $misses 0;
  252. }