Skip to content

Commit

Permalink
Improve flush cache
Browse files Browse the repository at this point in the history
  • Loading branch information
LC43 committed Jan 24, 2020
1 parent aeca82d commit 5a9a87d
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 17 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ See the data-sample.php file for a dump of the data.

## Change log

### v0.2.1
- Improve flush cache

### v0.2.0

- Fixed warnings when opcache was off
- Improve clear cache feedback
- Changed label from Scripts to Files


### TODO

- The ability to sort the list of cached scripts by the various columns
Expand Down
99 changes: 83 additions & 16 deletions opcache.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* https://github.com/wp-cloud/opcache-status
*
* @package OpCacheStatus
* @version 0.2.0
* @version 0.2.1
* @author WP-Cloud <code@wp-cloud.net>
* @copyright Copyright (c) 2016, WP-Cloud
* @copyright Copyright (c) -2016, Rasmus Lerdorf
Expand All @@ -32,13 +32,16 @@ public function __construct()
{
$this->_configuration = opcache_get_configuration();
$this->_status = opcache_get_status() ?: [];
$this->handleFlush();
}

public function getPageTitle()
{
return 'PHP ' . phpversion() . " with OpCache {$this->_configuration['version']['version']}";
}



public function getStatusDataRows()
{
$rows = array();
Expand Down Expand Up @@ -314,30 +317,68 @@ private function _arrayPset(&$array, $key, $value)
return $array;
}

public function clearCache() {
private function flushCache() {
if ( ! function_exists('opcache_reset') ) {
return;
}
return (int) opcache_reset();
}

public function clearCacheStatus() {
/**
* Check if flush was properly done and present it
*/
public function getFlushCacheStatus() {
$status_msg = '(failed)';
if ( ! isset( $_GET['flush_status'] ) ) {
return '';
}
if ( $_GET['flush_status'] == 1) {
$status_msg = '(success)';
}
printf( '<p class="clear_cache__status" title="Status of the flush done here.">%1$s</p>', $status_msg );
}

if ( ! isset( $_GET['reset_status'] ) ) {
return;

/**
* Get last flush and present in a human readble way
*/
public function getFlushTimeAgo() : string {

$stats = opcache_get_status( false );
if ( empty( $stats['opcache_statistics']['last_restart_time'] ) ){
return '';
}
if ( $_GET['reset_status'] == 1) {
echo '(success)';
$timeago_dt = new \DateTime('@' . $stats['opcache_statistics']['last_restart_time'] );

$now = new DateTime();
$interval = $timeago_dt->diff( $now, true );
$reset_ago = '<p class="clear_cache__timeago">Last reset was ';
$reset_ago_end = '</p>';
if ( $interval->y ) { return $reset_ago . $interval->y . ' years ago.' . $reset_ago_end; };
if ( $interval->m ) { return $reset_ago . $interval->m . ' months ago.' . $reset_ago_end; };
if ( $interval->d ) { return $reset_ago . $interval->d . ' days ago.' . $reset_ago_end; };
if ( $interval->h ) { return $reset_ago . $interval->h . ' hours ago.' . $reset_ago_end; };
if ( $interval->i ) { return $reset_ago . $interval->i . ' minutes ago.' . $reset_ago_end; };
return $reset_ago . ' less than 1 minute ago.' . $reset_ago_end;
}

/**
* Flush OpCache and redicted when Action is set to flush.
*/
private function handleFlush() {
if ( empty( $_POST['action'] ) ) {
return;
}
echo "(failed)";
if ( $_POST['action'] === 'flush' && ! empty( $_SERVER['PHP_SELF'] ) ) {
$flush_status = $this->flushCache();
header( 'Location: ' . $_SERVER['PHP_SELF'] . '?flush_status=' . $flush_status );
}
}

}

$dataModel = new OpCacheDataModel();

if (isset($_GET['clear']) && $_GET['clear'] == 1) {
$reset_status = $dataModel->clearCache();
header('Location: ' . $_SERVER['PHP_SELF'] . '?reset_status=' . $reset_status );
}
?>
<!DOCTYPE html>
<meta charset="utf-8">
Expand Down Expand Up @@ -516,6 +557,15 @@ public function clearCacheStatus() {
padding: 10px;
border: 1px solid #cacaca;
}
.clear_cache__status {
font-weight: 100;
text-decoration: underline dotted blue;
}
.clear_cache__timeago {
font-weight: 100;
font-style: italic;
}

</style>
<script src="inc/d3-3.0.1.min.js"></script>
<script src="inc/jquery-1.11.0.min.js"></script>
Expand All @@ -541,10 +591,6 @@ function toggleVisible(head, row) {
<span style="float:right;font-size:small;">OPcache Status v<?php echo $dataModel->version; ?></span>
<h1><?php echo $dataModel->getPageTitle(); ?></h1>

<div class="actions">
<a href="?clear=1">Clear cache</a> <span><?php echo $dataModel->clearCacheStatus(); ?></span>
</div>

<div class="tabs">

<div class="tab">
Expand Down Expand Up @@ -588,6 +634,27 @@ function toggleVisible(head, row) {
<div class="content"></div>
</div>

<div class="tab">
<input type="radio" id="tab-utilities" name="tab-group-1">
<label for="tab-utilities">Utilities</label>
<div class="content">
<table>
<tr>
<th>Flush cache
<?php echo $dataModel->getFlushCacheStatus(); ?>
<?php echo $dataModel->getFlushTimeAgo(); ?>
</th>
<td>
<form method="POST">
<input type="hidden" name="action" value="flush" />
<button name="submit" type="submit">Flush</button>
</form>
</td>
</tr>
</table>
</div>
</div>

</div>