-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Cache] TraceableAdapter #21082
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Cache] TraceableAdapter #21082
Changes from 1 commit
1714aa6
7ba9b1c
2f86f67
1cac255
19c401d
457c8f7
be9726a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,206 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Cache\Adapter; | ||
|
||
use Psr\Cache\CacheItemInterface; | ||
|
||
/** | ||
* An adapter that collects data about all cache calls. | ||
* | ||
* @author Aaron Scherer <aequasi@gmail.com> | ||
* @author Tobias Nyholm <tobias.nyholm@gmail.com> | ||
* @author Nicolas Grekas <p@tchwork.com> | ||
*/ | ||
class TraceableAdapter implements AdapterInterface | ||
{ | ||
private $pool; | ||
private $calls = array(); | ||
|
||
public function __construct(AdapterInterface $pool) | ||
{ | ||
$this->pool = $pool; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getItem($key) | ||
{ | ||
$event = $this->start(__FUNCTION__, $key); | ||
try { | ||
$item = $this->pool->getItem($key); | ||
} finally { | ||
$event->end = microtime(true); | ||
} | ||
if ($item->isHit()) { | ||
++$event->hits; | ||
} else { | ||
++$event->misses; | ||
} | ||
$event->result = $item->get(); | ||
|
||
return $item; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function hasItem($key) | ||
{ | ||
$event = $this->start(__FUNCTION__, $key); | ||
try { | ||
$event->result = $this->pool->hasItem($key); | ||
} finally { | ||
$event->end = microtime(true); | ||
} | ||
|
||
if (!$event->result) { | ||
++$event->misses; | ||
} | ||
|
||
return $event->result; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function deleteItem($key) | ||
{ | ||
$event = $this->start(__FUNCTION__, $key); | ||
try { | ||
return $event->result = $this->pool->deleteItem($key); | ||
} finally { | ||
$event->end = microtime(true); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function save(CacheItemInterface $item) | ||
{ | ||
$event = $this->start(__FUNCTION__, $item); | ||
try { | ||
return $event->result = $this->pool->save($item); | ||
} finally { | ||
$event->end = microtime(true); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function saveDeferred(CacheItemInterface $item) | ||
{ | ||
$event = $this->start(__FUNCTION__, $item); | ||
try { | ||
return $event->result = $this->pool->saveDeferred($item); | ||
} finally { | ||
$event->end = microtime(true); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getItems(array $keys = array()) | ||
{ | ||
$event = $this->start(__FUNCTION__, $keys); | ||
try { | ||
$result = $this->pool->getItems($keys); | ||
} finally { | ||
$event->end = microtime(true); | ||
} | ||
$f = function () use ($result, $event) { | ||
$event->result = array(); | ||
foreach ($result as $key => $item) { | ||
if ($item->isHit()) { | ||
++$event->hits; | ||
} else { | ||
++$event->misses; | ||
} | ||
$event->result[$key] = $item->get(); | ||
yield $key => $item; | ||
} | ||
}; | ||
|
||
return $f(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function clear() | ||
{ | ||
$event = $this->start(__FUNCTION__); | ||
try { | ||
return $event->result = $this->pool->clear(); | ||
} finally { | ||
$event->end = microtime(true); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function deleteItems(array $keys) | ||
{ | ||
$event = $this->start(__FUNCTION__, $keys); | ||
try { | ||
return $event->result = $this->pool->deleteItems($keys); | ||
} finally { | ||
$event->end = microtime(true); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function commit() | ||
{ | ||
$event = $this->start(__FUNCTION__); | ||
try { | ||
return $event->result = $this->pool->commit(); | ||
} finally { | ||
$event->end = microtime(true); | ||
} | ||
} | ||
|
||
public function getCalls() | ||
{ | ||
return $this->calls; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about resetting $calls = $this->calls;
$this->calls = array();
return $calls; or try {
return $this->calls;
} finally {
$this->calls = array();
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, makes sense |
||
} | ||
|
||
private function start($name, $argument = null) | ||
{ | ||
$this->calls[] = $event = new TraceableAdapterEvent(); | ||
$event->name = $name; | ||
$event->argument = $argument; | ||
$event->start = microtime(true); | ||
|
||
return $event; | ||
} | ||
} | ||
|
||
/** | ||
* @internal | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does it really make sense to tag the event class as internal when we return it in the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK for removing the tag then There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 after this change |
||
*/ | ||
class TraceableAdapterEvent | ||
{ | ||
public $name; | ||
public $argument; | ||
public $start; | ||
public $end; | ||
public $result; | ||
public $hits = 0; | ||
public $misses = 0; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isnt it more a wrapper then a adapter as the class doesnt adapt one api for another?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Strictly seen you are right. But I think it would be weird to change the name as we are implementing the
AdapterInterface
here, wouldn't it?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see