diff --git a/README.md b/README.md index 87c21b6..e2a39c8 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/opcache.php b/opcache.php index 1e1afca..9334158 100644 --- a/opcache.php +++ b/opcache.php @@ -6,7 +6,7 @@ * https://github.com/wp-cloud/opcache-status * * @package OpCacheStatus - * @version 0.2.0 + * @version 0.2.1 * @author WP-Cloud * @copyright Copyright (c) 2016, WP-Cloud * @copyright Copyright (c) -2016, Rasmus Lerdorf @@ -32,6 +32,7 @@ public function __construct() { $this->_configuration = opcache_get_configuration(); $this->_status = opcache_get_status() ?: []; + $this->handleFlush(); } public function getPageTitle() @@ -39,6 +40,8 @@ public function getPageTitle() return 'PHP ' . phpversion() . " with OpCache {$this->_configuration['version']['version']}"; } + + public function getStatusDataRows() { $rows = array(); @@ -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( '

%1$s

', $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 = '

Last reset was '; + $reset_ago_end = '

'; + 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 ); -} ?> @@ -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; + } + @@ -541,10 +591,6 @@ function toggleVisible(head, row) { OPcache Status vversion; ?>

getPageTitle(); ?>

-
- Clear cache clearCacheStatus(); ?> -
-
@@ -588,6 +634,27 @@ function toggleVisible(head, row) {
+
+ + +
+ + + + + +
Flush cache + getFlushCacheStatus(); ?> + getFlushTimeAgo(); ?> + +
+ + +
+
+
+
+