123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 |
- <?php
- namespace Symfony\Component\Cache\Adapter;
- use Psr\Cache\CacheItemInterface;
- use Symfony\Component\Cache\CacheItem;
- use Symfony\Component\Cache\PruneableInterface;
- use Symfony\Component\Cache\ResettableInterface;
- use Symfony\Contracts\Cache\CacheInterface;
- use Symfony\Contracts\Service\ResetInterface;
- class TraceableAdapter implements AdapterInterface, CacheInterface, PruneableInterface, ResettableInterface
- {
- protected $pool;
- private $calls = [];
- public function __construct(AdapterInterface $pool)
- {
- $this->pool = $pool;
- }
-
- public function get(string $key, callable $callback, float $beta = null, array &$metadata = null)
- {
- if (!$this->pool instanceof CacheInterface) {
- throw new \BadMethodCallException(sprintf('Cannot call "%s::get()": this class doesn\'t implement "%s".', get_debug_type($this->pool), CacheInterface::class));
- }
- $isHit = true;
- $callback = function (CacheItem $item, bool &$save) use ($callback, &$isHit) {
- $isHit = $item->isHit();
- return $callback($item, $save);
- };
- $event = $this->start(__FUNCTION__);
- try {
- $value = $this->pool->get($key, $callback, $beta, $metadata);
- $event->result[$key] = get_debug_type($value);
- } finally {
- $event->end = microtime(true);
- }
- if ($isHit) {
- ++$event->hits;
- } else {
- ++$event->misses;
- }
- return $value;
- }
-
- public function getItem($key)
- {
- $event = $this->start(__FUNCTION__);
- try {
- $item = $this->pool->getItem($key);
- } finally {
- $event->end = microtime(true);
- }
- if ($event->result[$key] = $item->isHit()) {
- ++$event->hits;
- } else {
- ++$event->misses;
- }
- return $item;
- }
-
- public function hasItem($key)
- {
- $event = $this->start(__FUNCTION__);
- try {
- return $event->result[$key] = $this->pool->hasItem($key);
- } finally {
- $event->end = microtime(true);
- }
- }
-
- public function deleteItem($key)
- {
- $event = $this->start(__FUNCTION__);
- try {
- return $event->result[$key] = $this->pool->deleteItem($key);
- } finally {
- $event->end = microtime(true);
- }
- }
-
- public function save(CacheItemInterface $item)
- {
- $event = $this->start(__FUNCTION__);
- try {
- return $event->result[$item->getKey()] = $this->pool->save($item);
- } finally {
- $event->end = microtime(true);
- }
- }
-
- public function saveDeferred(CacheItemInterface $item)
- {
- $event = $this->start(__FUNCTION__);
- try {
- return $event->result[$item->getKey()] = $this->pool->saveDeferred($item);
- } finally {
- $event->end = microtime(true);
- }
- }
-
- public function getItems(array $keys = [])
- {
- $event = $this->start(__FUNCTION__);
- try {
- $result = $this->pool->getItems($keys);
- } finally {
- $event->end = microtime(true);
- }
- $f = function () use ($result, $event) {
- $event->result = [];
- foreach ($result as $key => $item) {
- if ($event->result[$key] = $item->isHit()) {
- ++$event->hits;
- } else {
- ++$event->misses;
- }
- yield $key => $item;
- }
- };
- return $f();
- }
-
- public function clear(string $prefix = '')
- {
- $event = $this->start(__FUNCTION__);
- try {
- if ($this->pool instanceof AdapterInterface) {
- return $event->result = $this->pool->clear($prefix);
- }
- return $event->result = $this->pool->clear();
- } finally {
- $event->end = microtime(true);
- }
- }
-
- public function deleteItems(array $keys)
- {
- $event = $this->start(__FUNCTION__);
- $event->result['keys'] = $keys;
- try {
- return $event->result['result'] = $this->pool->deleteItems($keys);
- } finally {
- $event->end = microtime(true);
- }
- }
-
- public function commit()
- {
- $event = $this->start(__FUNCTION__);
- try {
- return $event->result = $this->pool->commit();
- } finally {
- $event->end = microtime(true);
- }
- }
-
- public function prune()
- {
- if (!$this->pool instanceof PruneableInterface) {
- return false;
- }
- $event = $this->start(__FUNCTION__);
- try {
- return $event->result = $this->pool->prune();
- } finally {
- $event->end = microtime(true);
- }
- }
-
- public function reset()
- {
- if ($this->pool instanceof ResetInterface) {
- $this->pool->reset();
- }
- $this->clearCalls();
- }
-
- public function delete(string $key): bool
- {
- $event = $this->start(__FUNCTION__);
- try {
- return $event->result[$key] = $this->pool->deleteItem($key);
- } finally {
- $event->end = microtime(true);
- }
- }
- public function getCalls()
- {
- return $this->calls;
- }
- public function clearCalls()
- {
- $this->calls = [];
- }
- protected function start(string $name)
- {
- $this->calls[] = $event = new TraceableAdapterEvent();
- $event->name = $name;
- $event->start = microtime(true);
- return $event;
- }
- }
- class TraceableAdapterEvent
- {
- public $name;
- public $start;
- public $end;
- public $result;
- public $hits = 0;
- public $misses = 0;
- }
|