Skip to content

Commit d696b39

Browse files
bug symfony#22465 [Cache] Keep only hit/miss (not values) in TraceableAdapter/Cache (nicolas-grekas)
This PR was merged into the 3.3-dev branch. Discussion ---------- [Cache] Keep only hit/miss (not values) in TraceableAdapter/Cache | Q | A | ------------- | --- | Branch? | 3.3 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Right now, TraceableAdapter and TraceableCache both keep all fetched values. This exhaustive reporting is too much data gathering to me. Here is a PR that keeps only true/false for each hit/miss + all corresponding keys. That can still be a lot of data (one item per fetched key) - but a bit better. Should we go further in stats-only gathering? Or should we *not* do this and keep collecting *all* the values? The PR also fixes "Traversable" handling. ping @Nyholm The profiler panel still works (although breaking lines in the middle of words is strange, but this is the profiler's CSS, nothing special to this specific case). ![capture du 2017-04-19 11-40-13](https://cloud.githubusercontent.com/assets/243674/25173586/f9615dd6-24f4-11e7-8d6f-36fb2437c3b6.png) Commits ------- 0c73c5d [Cache] Keep only hit/miss (not values) in TraceableAdapter/Cache
2 parents b9ee33f + 0c73c5d commit d696b39

File tree

5 files changed

+96
-90
lines changed

5 files changed

+96
-90
lines changed

src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/cache.html.twig

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,18 +130,16 @@
130130
<th>#</th>
131131
<th>Time</th>
132132
<th>Call</th>
133-
<th>Argument</th>
134-
<th>Result</th>
133+
<th>Hit</th>
135134
</tr>
136135
</thead>
137136
<tbody>
138137
{% for call in calls %}
139138
<tr>
140139
<td class="font-normal text-small text-muted nowrap">{{ loop.index }}</td>
141-
<td>{{ '%0.2f'|format((call.end - call.start) * 1000) }} ms</td>
142-
<td>{{ call.name }}()</td>
143-
<td>{{ profiler_dump(call.value.argument, maxDepth=1) }}</td>
144-
<td>{{ profiler_dump(call.value.result, maxDepth=1) }}</td>
140+
<td class="nowrap">{{ '%0.2f'|format((call.end - call.start) * 1000) }} ms</td>
141+
<td class="nowrap">{{ call.name }}()</td>
142+
<td>{{ profiler_dump(call.value.result, maxDepth=2) }}</td>
145143
</tr>
146144
{% endfor %}
147145
</tbody>

src/Symfony/Component/Cache/Adapter/TraceableAdapter.php

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,17 @@ public function __construct(AdapterInterface $pool)
3535
*/
3636
public function getItem($key)
3737
{
38-
$event = $this->start(__FUNCTION__, $key);
38+
$event = $this->start(__FUNCTION__);
3939
try {
4040
$item = $this->pool->getItem($key);
4141
} finally {
4242
$event->end = microtime(true);
4343
}
44-
if ($item->isHit()) {
44+
if ($event->result[$key] = $item->isHit()) {
4545
++$event->hits;
4646
} else {
4747
++$event->misses;
4848
}
49-
$event->result = $item->get();
5049

5150
return $item;
5251
}
@@ -56,9 +55,9 @@ public function getItem($key)
5655
*/
5756
public function hasItem($key)
5857
{
59-
$event = $this->start(__FUNCTION__, $key);
58+
$event = $this->start(__FUNCTION__);
6059
try {
61-
return $event->result = $this->pool->hasItem($key);
60+
return $event->result[$key] = $this->pool->hasItem($key);
6261
} finally {
6362
$event->end = microtime(true);
6463
}
@@ -69,9 +68,9 @@ public function hasItem($key)
6968
*/
7069
public function deleteItem($key)
7170
{
72-
$event = $this->start(__FUNCTION__, $key);
71+
$event = $this->start(__FUNCTION__);
7372
try {
74-
return $event->result = $this->pool->deleteItem($key);
73+
return $event->result[$key] = $this->pool->deleteItem($key);
7574
} finally {
7675
$event->end = microtime(true);
7776
}
@@ -82,9 +81,9 @@ public function deleteItem($key)
8281
*/
8382
public function save(CacheItemInterface $item)
8483
{
85-
$event = $this->start(__FUNCTION__, $item);
84+
$event = $this->start(__FUNCTION__);
8685
try {
87-
return $event->result = $this->pool->save($item);
86+
return $event->result[$item->getKey()] = $this->pool->save($item);
8887
} finally {
8988
$event->end = microtime(true);
9089
}
@@ -95,9 +94,9 @@ public function save(CacheItemInterface $item)
9594
*/
9695
public function saveDeferred(CacheItemInterface $item)
9796
{
98-
$event = $this->start(__FUNCTION__, $item);
97+
$event = $this->start(__FUNCTION__);
9998
try {
100-
return $event->result = $this->pool->saveDeferred($item);
99+
return $event->result[$item->getKey()] = $this->pool->saveDeferred($item);
101100
} finally {
102101
$event->end = microtime(true);
103102
}
@@ -117,12 +116,11 @@ public function getItems(array $keys = array())
117116
$f = function () use ($result, $event) {
118117
$event->result = array();
119118
foreach ($result as $key => $item) {
120-
if ($item->isHit()) {
119+
if ($event->result[$key] = $item->isHit()) {
121120
++$event->hits;
122121
} else {
123122
++$event->misses;
124123
}
125-
$event->result[$key] = $item->get();
126124
yield $key => $item;
127125
}
128126
};
@@ -148,9 +146,10 @@ public function clear()
148146
*/
149147
public function deleteItems(array $keys)
150148
{
151-
$event = $this->start(__FUNCTION__, $keys);
149+
$event = $this->start(__FUNCTION__);
150+
$event->result['keys'] = $keys;
152151
try {
153-
return $event->result = $this->pool->deleteItems($keys);
152+
return $event->result['result'] = $this->pool->deleteItems($keys);
154153
} finally {
155154
$event->end = microtime(true);
156155
}
@@ -178,11 +177,10 @@ public function getCalls()
178177
}
179178
}
180179

181-
private function start($name, $argument = null)
180+
private function start($name)
182181
{
183182
$this->calls[] = $event = new TraceableAdapterEvent();
184183
$event->name = $name;
185-
$event->argument = $argument;
186184
$event->start = microtime(true);
187185

188186
return $event;
@@ -192,7 +190,6 @@ private function start($name, $argument = null)
192190
class TraceableAdapterEvent
193191
{
194192
public $name;
195-
public $argument;
196193
public $start;
197194
public $end;
198195
public $result;

src/Symfony/Component/Cache/Simple/TraceableCache.php

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,30 +36,30 @@ public function __construct(CacheInterface $pool)
3636
public function get($key, $default = null)
3737
{
3838
$miss = null !== $default && is_object($default) ? $default : $this->miss;
39-
$event = $this->start(__FUNCTION__, compact('key', 'default'));
39+
$event = $this->start(__FUNCTION__);
4040
try {
4141
$value = $this->pool->get($key, $miss);
4242
} finally {
4343
$event->end = microtime(true);
4444
}
45-
if ($miss !== $value) {
45+
if ($event->result[$key] = $miss !== $value) {
4646
++$event->hits;
4747
} else {
4848
++$event->misses;
4949
$value = $default;
5050
}
5151

52-
return $event->result = $value;
52+
return $value;
5353
}
5454

5555
/**
5656
* {@inheritdoc}
5757
*/
5858
public function has($key)
5959
{
60-
$event = $this->start(__FUNCTION__, compact('key'));
60+
$event = $this->start(__FUNCTION__);
6161
try {
62-
return $event->result = $this->pool->has($key);
62+
return $event->result[$key] = $this->pool->has($key);
6363
} finally {
6464
$event->end = microtime(true);
6565
}
@@ -70,9 +70,9 @@ public function has($key)
7070
*/
7171
public function delete($key)
7272
{
73-
$event = $this->start(__FUNCTION__, compact('key'));
73+
$event = $this->start(__FUNCTION__);
7474
try {
75-
return $event->result = $this->pool->delete($key);
75+
return $event->result[$key] = $this->pool->delete($key);
7676
} finally {
7777
$event->end = microtime(true);
7878
}
@@ -83,9 +83,9 @@ public function delete($key)
8383
*/
8484
public function set($key, $value, $ttl = null)
8585
{
86-
$event = $this->start(__FUNCTION__, compact('key', 'value', 'ttl'));
86+
$event = $this->start(__FUNCTION__);
8787
try {
88-
return $event->result = $this->pool->set($key, $value, $ttl);
88+
return $event->result[$key] = $this->pool->set($key, $value, $ttl);
8989
} finally {
9090
$event->end = microtime(true);
9191
}
@@ -96,9 +96,23 @@ public function set($key, $value, $ttl = null)
9696
*/
9797
public function setMultiple($values, $ttl = null)
9898
{
99-
$event = $this->start(__FUNCTION__, compact('values', 'ttl'));
99+
$event = $this->start(__FUNCTION__);
100+
$event->result['keys'] = array();
101+
102+
if ($values instanceof \Traversable) {
103+
$values = function () use ($values, $event) {
104+
foreach ($values as $k => $v) {
105+
$event->result['keys'][] = $k;
106+
yield $k => $v;
107+
}
108+
};
109+
$values = $values();
110+
} elseif (is_array($values)) {
111+
$event->result['keys'] = array_keys($values);
112+
}
113+
100114
try {
101-
return $event->result = $this->pool->setMultiple($values, $ttl);
115+
return $event->result['result'] = $this->pool->setMultiple($values, $ttl);
102116
} finally {
103117
$event->end = microtime(true);
104118
}
@@ -110,7 +124,7 @@ public function setMultiple($values, $ttl = null)
110124
public function getMultiple($keys, $default = null)
111125
{
112126
$miss = null !== $default && is_object($default) ? $default : $this->miss;
113-
$event = $this->start(__FUNCTION__, compact('keys', 'default'));
127+
$event = $this->start(__FUNCTION__);
114128
try {
115129
$result = $this->pool->getMultiple($keys, $miss);
116130
} finally {
@@ -119,13 +133,13 @@ public function getMultiple($keys, $default = null)
119133
$f = function () use ($result, $event, $miss, $default) {
120134
$event->result = array();
121135
foreach ($result as $key => $value) {
122-
if ($miss !== $value) {
136+
if ($event->result[$key] = $miss !== $value) {
123137
++$event->hits;
124138
} else {
125139
++$event->misses;
126140
$value = $default;
127141
}
128-
yield $key => $event->result[$key] = $value;
142+
yield $key => $value;
129143
}
130144
};
131145

@@ -150,9 +164,14 @@ public function clear()
150164
*/
151165
public function deleteMultiple($keys)
152166
{
153-
$event = $this->start(__FUNCTION__, compact('keys'));
167+
$event = $this->start(__FUNCTION__);
168+
if ($keys instanceof \Traversable) {
169+
$keys = $event->result['keys'] = iterator_to_array($keys, false);
170+
} else {
171+
$event->result['keys'] = $keys;
172+
}
154173
try {
155-
return $event->result = $this->pool->deleteMultiple($keys);
174+
return $event->result['result'] = $this->pool->deleteMultiple($keys);
156175
} finally {
157176
$event->end = microtime(true);
158177
}
@@ -167,11 +186,10 @@ public function getCalls()
167186
}
168187
}
169188

170-
private function start($name, array $arguments = null)
189+
private function start($name)
171190
{
172191
$this->calls[] = $event = new TraceableCacheEvent();
173192
$event->name = $name;
174-
$event->arguments = $arguments;
175193
$event->start = microtime(true);
176194

177195
return $event;
@@ -181,7 +199,6 @@ private function start($name, array $arguments = null)
181199
class TraceableCacheEvent
182200
{
183201
public $name;
184-
public $arguments;
185202
public $start;
186203
public $end;
187204
public $result;

0 commit comments

Comments
 (0)