vendor/symfony/symfony/src/Symfony/Component/Cache/Adapter/TraceableAdapter.php line 42

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\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 AdapterInterfacePruneableInterfaceResettableInterface
  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.             $this->pool->reset();
  178.         }
  179.         $this->clearCalls();
  180.     }
  181.     public function getCalls()
  182.     {
  183.         return $this->calls;
  184.     }
  185.     public function clearCalls()
  186.     {
  187.         $this->calls = [];
  188.     }
  189.     protected function start($name)
  190.     {
  191.         $this->calls[] = $event = new TraceableAdapterEvent();
  192.         $event->name $name;
  193.         $event->start microtime(true);
  194.         return $event;
  195.     }
  196. }
  197. class TraceableAdapterEvent
  198. {
  199.     public $name;
  200.     public $start;
  201.     public $end;
  202.     public $result;
  203.     public $hits 0;
  204.     public $misses 0;
  205. }