Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
37 changes: 31 additions & 6 deletions src/Cache/LoggingCachePool.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,13 @@ public function getItem($key, array $tags = [])
{
$call = $this->timeCall(__FUNCTION__, [$key, $tags]);
$result = $call->result;
$call->isHit = $result->isHit();

if ($result->isHit()) {
$call->result = sprintf('<DATA:%s>', gettype($result->get()));
// Display the result in a good way depending on the data type
if ($call->isHit) {
$call->result = $this->getValueRepresentation($result->get());
} else {
$call->result = false;
$call->result = null;
}

$this->calls[] = $call;
Expand All @@ -91,11 +93,11 @@ public function deleteItem($key, array $tags = [])

public function save(CacheItemInterface $item)
{
$itemClone = clone $item;
$itemClone->set(sprintf('<DATA:%s', gettype($item->get())));
$key = $item->getKey();
$value = $this->getValueRepresentation($item->get());

$call = $this->timeCall(__FUNCTION__, [$item]);
$call->arguments = ['<CacheItem>', $itemClone];
$call->arguments = ['<CacheItem>', $key, $value];
$this->calls[] = $call;

return $call->result;
Expand Down Expand Up @@ -155,4 +157,27 @@ public function getCalls()
{
return $this->calls;
}

/**
* Get a string to represent the value.
*
* @param mixed $value
*
* @return string
*/
private function getValueRepresentation($value)
{
$type = gettype($value);
if (in_array($type, ['boolean', 'integer', 'double', 'string', 'NULL'])) {
$rep = $value;
} elseif ($type === 'array') {
$rep = json_encode($value);
} elseif ($type === 'object') {
$rep = get_class($value);
} else {
$rep = sprintf('<DATA:%s>', $type);
Copy link
Member

Choose a reason for hiding this comment

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

maybe we could try running through jms_serializer, if the user has it installed

Copy link
Member Author

Choose a reason for hiding this comment

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

You are talking about if $type=='object'? Yeah, why not.

On line 178 we do only have 'resource' and some other thing we dont want to display.

Copy link
Member

Choose a reason for hiding this comment

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

yeah

}

return $rep;
}
}
2 changes: 1 addition & 1 deletion src/DataCollector/CacheDataCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ private function calculateStatistics()
$statistics[$name]['time'] += $call->time;
if ($call->name === 'getItem') {
$statistics[$name]['reads'] += 1;
if ($call->result !== false) {
if ($call->isHit) {
Copy link
Member

Choose a reason for hiding this comment

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

should be a method?

Copy link
Member Author

Choose a reason for hiding this comment

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

No, It is a property that only exists if call->name == 'getItem*

$statistics[$name]['hits'] += 1;
} else {
$statistics[$name]['misses'] += 1;
Expand Down
17 changes: 15 additions & 2 deletions src/Resources/views/Collector/cache.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@

{% extends 'WebProfilerBundle:Profiler:layout.html.twig' %}

{% block head %}
{{ parent() }}
<style>
.color-red {
color:red;
}
</style>
{% endblock head %}

{% block toolbar %}
{% set icon %}
<img width="20" height="28" alt="Cache"
Expand Down Expand Up @@ -91,8 +100,12 @@
<li class="{{ i is odd ? 'odd' : 'even' }}">
<div>
<strong>Name</strong>: {{ call.name }}<br />
<strong>Arguments</strong>: {{ call.arguments|json_encode() }}<br />
<strong>Results</strong>: {{ call.result|json_encode() }}<br />
<strong>Arguments</strong>: {{ call.arguments|json_encode|truncate(140) }}<br />
<strong>Results</strong>: {% if call.result == false %}
<span class="color-red">Miss</span>
{% else %}
{{ call.result|truncate(140) }}
Copy link
Member

Choose a reason for hiding this comment

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

may make sense to have a button to click to show the rest at some point too

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes. Agree. But I left that for a future improvement.

{% endif %}<br />
</div>
<small>
<strong>Time</strong>: {{ '%0.2f'|format(call.time * 1000) }} ms
Expand Down