Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[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

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Added traceable adapter
  • Loading branch information
Nyholm committed Dec 28, 2016
commit 1714aa6abd6eb4163a108338245094d2bc2b929d
206 changes: 206 additions & 0 deletions 206 src/Symfony/Component/Cache/Adapter/TraceableAdapter.php
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
Copy link
Contributor

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?

Copy link
Member

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?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see

{
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;
Copy link
Member

@nicolas-grekas nicolas-grekas Dec 29, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about resetting $this->calls here? This would allow reusing the same adapter several times in a long running process with no mem leak.

$calls = $this->calls;
$this->calls = array();

return $calls;

or

try {
    return $this->calls;
} finally {
    $this->calls = array();
}

Copy link
Member Author

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The 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 getCalls() method?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK for removing the tag then

Copy link
Member

Choose a reason for hiding this comment

The 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;
}
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.