Skip to content
This repository was archived by the owner on Jan 8, 2024. It is now read-only.

Commit 48b7b61

Browse files
committed
HumanReadable size
1 parent 21a57e4 commit 48b7b61

File tree

4 files changed

+94
-15
lines changed

4 files changed

+94
-15
lines changed

DataCollector/CacheCollector.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,35 @@ public function __construct(Cache $cache)
3232
*/
3333
public function collect(Request $request, Response $response, \Exception $exception = null)
3434
{
35+
$size = 0;
36+
/** @var $cache */
37+
foreach ($this->cache->getInstances() as $cache) {
38+
if ($cache->getStats()->getSize()) {
39+
$size += $cache->getStats()->getSize();
40+
}
41+
}
3542
$this->data = [
36-
'cache' => $this->cache,
43+
'caches' => $this->cache->getInstances(),
44+
'size' => $this->cache->size_format($size),
3745
];
3846
}
3947

40-
public function getCache()
48+
public function getCaches()
4149
{
4250
/**
43-
* @var $name
51+
* @var $name
4452
* @var ExtendedCacheItemPoolInterface $cache
4553
*/
46-
return $this->data['cache'];
54+
return $this->data['caches'];
55+
}
56+
57+
public function getSize()
58+
{
59+
/**
60+
* @var $name
61+
* @var ExtendedCacheItemPoolInterface $cache
62+
*/
63+
return $this->data['size'];
4764
}
4865

4966
public function getName()

Resources/config/services.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
parameters:
22
php_fast_cache.cache.class: phpFastCache\Bundle\Service\Cache
33
php_fast_cache.data_collector.class: phpFastCache\Bundle\DataCollector\CacheCollector
4+
php_fast_cache.human_readable.class: phpFastCache\Bundle\Twig\HumanReadableExtension
45

56
services:
67
phpfastcache:
@@ -18,4 +19,9 @@ services:
1819
name: data_collector
1920
template: '@phpFastCache/data_collector/template.html.twig'
2021
id: 'phpfastcache.request_collector'
21-
priority: 300
22+
priority: 300
23+
24+
phpfastcache.human_readable_size:
25+
class: "%php_fast_cache.human_readable.class%"
26+
tags:
27+
- { name: twig.extension }

Resources/views/data_collector/template.html.twig

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
{% block toolbar %}
44
{% set icon %}
55
<span class="icon">{{ include('@phpFastCache/data_collector/icon.svg') }}</span>
6-
<span class="sf-toolbar-value">PhpFastCache</span>
6+
<span class="sf-toolbar-value">{{ collector.size|sizeFormat }}</span>
77
{% endset %}
88

99
{{ include('@WebProfiler/Profiler/toolbar_item.html.twig') }}
@@ -18,18 +18,30 @@
1818

1919
{% block panel %}
2020
<div class="sf-tabs">
21-
{% for name, cache in collector.cache.instances %}
21+
{% for name, cache in collector.caches %}
2222
<div class="tab">
2323
<h3 class="tab-title">{{ name }}</h3>
2424
<div class="tab-content">
25-
<h3>Info</h3>
26-
{{ dump(cache.stats.info) }}
27-
<h3>Size</h3>
28-
{{ dump(cache.stats.size) }}
29-
<h3>Data</h3>
30-
{{ dump(cache.stats.data) }}
31-
<h3>RawData</h3>
32-
{{ dump(cache.stats.rawData) }}
25+
<table class="{{ class|default('') }}">
26+
<tbody>
27+
<tr>
28+
<th>Info</th>
29+
<td>{{ cache.stats.info }}</td>
30+
</tr>
31+
<tr>
32+
<th>Size</th>
33+
<td>{{ cache.stats.size|sizeFormat }}</td>
34+
</tr>
35+
<tr>
36+
<th>Data</th>
37+
<td>{{ cache.stats.data }}</td>
38+
</tr>
39+
<tr>
40+
<th>RawData</th>
41+
<td>{{ cache.stats.rawData }}</td>
42+
</tr>
43+
</tbody>
44+
</table>
3345
</div>
3446
</div>
3547
{% endfor %}

Twig/HumanReadableExtension.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace phpFastCache\Bundle\Twig;
4+
5+
/**
6+
* Class HumanReadableExtension
7+
* @package phpFastCache\Bundle\Twig
8+
*/
9+
class HumanReadableExtension extends \Twig_Extension
10+
{
11+
/**
12+
* @return array
13+
*/
14+
public function getFilters()
15+
{
16+
return array(
17+
new \Twig_SimpleFilter('sizeFormat', [$this, 'size_format']),
18+
);
19+
}
20+
/**
21+
* @param $bytes
22+
* @param int $decimals
23+
*
24+
* @return string
25+
*/
26+
public function size_format($bytes, $decimals = 2)
27+
{
28+
$bytes = (int) $bytes;
29+
$sz = 'BKMGTP';
30+
$factor = floor(( strlen($bytes) - 1 ) / 3);
31+
32+
return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)).@$sz[ $factor ].( $factor ? 'o' : '' );
33+
}
34+
35+
/**
36+
* Extension name
37+
*
38+
* @return string
39+
*/
40+
public function getName()
41+
{
42+
return 'human_readable_extension';
43+
}
44+
}

0 commit comments

Comments
 (0)