Skip to content

Commit

Permalink
Cache details
Browse files Browse the repository at this point in the history
  • Loading branch information
mducharme committed Nov 29, 2017
1 parent aec041d commit f6d71bc
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 7 deletions.
63 changes: 56 additions & 7 deletions src/Charcoal/Admin/Template/System/ClearCacheTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,12 @@ public function cacheInfo()
$cacheType = isset($flip['\\'.$driver]) ? $flip['\\'.$driver] : $driver;
return [
'type' => $cacheType,
'active' => true,
'active' => $this->cacheConfig['active'],
'global' => $this->globalCacheInfo(),
'pages' => $this->pagesCacheInfo(),
'objects' => $this->objectsCacheInfo()
'objects' => $this->objectsCacheInfo(),
'pages_items' => $this->pagesCacheItems(),
'objects_items' => $this->objectsCacheItems()
];
}

Expand All @@ -86,7 +88,7 @@ public function cacheInfo()
*/
private function globalCacheInfo()
{
if ($this->isApc()) {
if ($this->isApc() === true) {
return $this->apcCacheInfo('/::'.$this->cache->getNamespace().'::/');
} else {
return [
Expand All @@ -104,7 +106,7 @@ private function globalCacheInfo()
*/
private function pagesCacheInfo()
{
if ($this->isApc()) {
if ($this->isApc() === true) {
return $this->apcCacheInfo(
'/::'.$this->cache->getNamespace().'::request::|::'.$this->cache->getNamespace().'::template::/'
);
Expand All @@ -119,12 +121,24 @@ private function pagesCacheInfo()
}
}

/**
* @return array
*/
private function pagesCacheItems()
{
if ($this->isApc() === true) {
return $this->apcCacheItems('/::'.$this->cache->getNamespace().'::request::|::'.$this->cache->getNamespace().'::template::/');
} else {
return [];
}
}

/**
* @return array
*/
private function objectsCacheInfo()
{
if ($this->isApc()) {
if ($this->isApc() === true) {
return $this->apcCacheInfo(
'/::'.$this->cache->getNamespace().'::object::|::'.$this->cache->getNamespace().'::metadata::/'
);
Expand All @@ -139,6 +153,18 @@ private function objectsCacheInfo()
}
}

/**
* @return array
*/
private function objectsCacheItems()
{
if ($this->isApc()) {
return $this->apcCacheItems('/::'.$this->cache->getNamespace().'::objects::|::'.$this->cache->getNamespace().'::metadata::/');
} else {
return [];
}
}

/**
* @param string $key The cache key to look at.
* @return array
Expand Down Expand Up @@ -175,6 +201,29 @@ private function apcCacheInfo($key)
];
}

/**
* @param string $key The cache key to look at.
* @return array|\Generator
*/
private function apcCacheItems($key)
{
if (class_exists(APCUIterator::class)) {
$iter = new APCUIterator($key);
} elseif (class_exists(APCIterator::class)) {
$iter = new APCIterator($key);
} else {
return [];
}

foreach ($iter as $item) {
$item['ident'] = str_replace('::', '⇒', str_replace('.', '/', trim(str_replace($this->cache->getNamespace(), '', strstr($item['key'], $this->cache->getNamespace().'::')), ':')));
$item['size'] = $this->formatBytes($item['mem_size']);
$item['created'] = date('Y-m-d H:i:s', $item['creation_time']);
$item['expiry'] = date('Y-m-d H:i:s', ($item['creation_time']+$item['ttl']));
yield $item;
}
}

/**
* @return boolean
*/
Expand Down Expand Up @@ -203,9 +252,9 @@ private function formatBytes($size)
return 0;
}
$base = log($size, 1024);
$suffixes = [ '', 'K', 'M', 'G', 'T' ];
$suffixes = [ 'b', 'k', 'M', 'G', 'T' ];

$floor = floor($base);
return round(pow(1024, ($base - $floor)), 2).' '.$suffixes[$floor];
return round(pow(1024, ($base - $floor)), 2).''.$suffixes[$floor];
}
}
52 changes: 52 additions & 0 deletions templates/charcoal/admin/template/system/clear-cache.mustache
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{{!--
`charcoal/admin/template/system/users`
--}}
{{#addCss}}
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.11.1/bootstrap-table.min.css">
{{/addCss}}
{{>charcoal/admin/template/inc.header}}

<div class="o-flex">
Expand Down Expand Up @@ -61,6 +64,7 @@
</div>
</section>
</div>

<div class="col-xs-12 col-sm-6 col-lg-6">
<section class="panel panel-default">
<header class="panel-heading">
Expand All @@ -82,9 +86,11 @@
<span class="btn-label">{{# _t }}Clear pages cache{{/ _t }}</span>
</button>
</p>

</div>
</section>
</div>

<div class="col-xs-12 col-sm-6 col-lg-6">
<section class="panel panel-default">
<header class="panel-heading">
Expand All @@ -106,13 +112,59 @@
<span class="btn-label">{{# _t }}Clear objects cache{{/ _t }}</span>
</button>
</p>

</div>
</section>
</div>
</div>

{{#cacheInfo.global.num_entries}}
<div class="col-md-12">
<section class="panel panel-default">
<header class="panel-heading">
<h3 class="panel-title">{{#_t}}Cache entries{{/_t}}</h3>
</header>
<div class="panel-body" style="padding:0">
<table class="table" data-toggle="table">
<thead>
<tr>
<th scope="col" data-sortable="true">{{#_t}}Key{{/_t}}</th>
<th scope="col" data-sortable="true">{{#_t}}Hits{{/_t}}</th>
<th scope="col" data-sortable="true">{{#_t}}Size{{/_t}}</th>
<th scope="col" data-sortable="true">{{#_t}}Created{{/_t}}</th>
<th scope="col" data-sortable="true">{{#_t}}Timeout{{/_t}}</th>
<th scope="col" data-sortable="true">{{#_t}}Expiry{{/_t}}</th>
<th scope="col">Action</th>
</tr>
</thead>
<tbody>
{{#cacheInfo.objects_items}}
<tr>
<td><strong>{{ident}}</strong></td>
<td>{{num_hits}}</td>
<td><span data-value="{{mem_size}}">{{size}}<span></td>
<td>{{created}}</td>
<td>{{ttl}}</td>
<td>{{expiry}}</td>
<td><!--<a class="btn btn-default">Delete</a>--></td>
</tr>
{{/cacheInfo.objects_items}}

</table>
</div>
</section>
</div>
{{/cacheInfo.global.num_entries}}




</div>
</div>
</div>
</div>

{{#addJs}}
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.11.1/bootstrap-table.min.js"></script>
{{/addJs}}
{{>charcoal/admin/template/inc.footer}}

0 comments on commit f6d71bc

Please sign in to comment.