Skip to content

Commit

Permalink
bump 20.10.04
Browse files Browse the repository at this point in the history
- Improved CronAgent.
- Improved CLI.
- Improved disk I/O and CPU usage.
- Optimized WP Alloptions.
  • Loading branch information
nawawi committed Nov 10, 2020
1 parent b50b007 commit b9df4b8
Show file tree
Hide file tree
Showing 16 changed files with 256 additions and 276 deletions.
2 changes: 1 addition & 1 deletion .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
custom: ['https://www.patreon.com/docketcache']
custom: ['https://www.patreon.com/bePatron?u=41796862']
7 changes: 7 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
= 20.10.04 (2020-11-11)

- Improved CronAgent.
- Improved CLI.
- Improved disk I/O and CPU usage.
- Optimized WP Alloptions.

= 20.10.03 (2020-11-03)

- Fixed nwdcx_optget() -> missing sql syntax.
Expand Down
4 changes: 2 additions & 2 deletions docket-cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
* @wordpress-plugin
* Plugin Name: Docket Cache
* Plugin URI: https://wordpress.org/plugins/docket-cache/
* Version: 20.10.03
* VerPrev: 20.10.02
* Version: 20.10.04
* VerPrev: 20.10.03
* Description: A persistent object cache stored as a plain PHP code, accelerates caching with OPcache backend.
* GitHub Plugin URI: https://github.com/nawawi/docket-cache
* Author: Nawawi Jamili
Expand Down
2 changes: 1 addition & 1 deletion includes/admin/resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
<p>
<strong><?php esc_html_e('SPONSOR', 'docket-cache'); ?></strong><br class="break">
<?php esc_html_e('Become our sponsor to funding further development of this project.', 'docket-cache'); ?>
<a href="https://docketcache.com/sponsorship" class="button button-secondary button-small bt-cx" rel="noopener" target="new"><?php esc_html_e('Become Sponsor', 'docket-cache'); ?></a>
<a href="https://www.patreon.com/bePatron?u=41796862" class="button button-secondary button-small bt-cx" rel="noopener" target="new"><?php esc_html_e('Become Sponsor', 'docket-cache'); ?></a>
</p>
</div>
</div>
Expand Down
67 changes: 36 additions & 31 deletions includes/cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -669,16 +669,17 @@ private function maybe_expire($group, $expire = 0, $key = '')
}

$expire = $this->fs()->sanitize_second($expire);
$maxttl = $this->cache_maxttl;

if (0 === $expire) {
if (0 === $expire && $maxttl < 2419200) {
if (\in_array($group, ['site-transient', 'transient'])) {
if ('site-transient' === $group && \in_array($key, ['update_plugins', 'update_themes', 'update_core'])) {
$expire = 2419200; // 28d
$expire = $maxttl < 2419200 ? 2419200 : $maxttl; // 28d
} else {
$expire = 604800; // 7d
$expire = $maxttl < 604800 ? 604800 : $maxttl; // 7d
}
} elseif (\in_array($group, ['post_meta', 'options'])) {
$expire = 1209600; // 14d
} elseif (\in_array($group, ['terms', 'posts', 'post_meta', 'options', 'site-options', 'comments'])) {
$expire = $maxttl < 1209600 ? 1209600 : $maxttl; // 14d
}
}

Expand Down Expand Up @@ -751,7 +752,7 @@ private function is_data_uptodate($key, $group, $data)
return false;
}

if (!$doserialize && $data_p === $data) {
if (!$doserialize && (('string' === $data_type && 0 === strcmp($data_p, $data)) || $data_p === $data)) {
return true;
}

Expand Down Expand Up @@ -925,6 +926,7 @@ private function dc_get($key, $group, $is_raw = false)
$is_timeout = true;
}

// incase gc not run
if (!$is_timeout && !empty($this->cache_maxttl) && !empty($data['timestamp']) && $this->fs()->valid_timestamp($data['timestamp'])) {
$maxttl = time() - $this->cache_maxttl;
if ($data['timestamp'] < $maxttl) {
Expand Down Expand Up @@ -994,6 +996,7 @@ private function dc_save($cache_key, $data, $group = 'default', $expire = 0, $ke

$file = $this->get_file_path($cache_key, $group);

// jangan gatai tangan usik.
$timeout = ($expire > 0 ? time() + $expire : 0);

$type = \gettype($data);
Expand All @@ -1014,8 +1017,11 @@ private function dc_save($cache_key, $data, $group = 'default', $expire = 0, $ke
}
}

// since timeout set to timestamp.
if (0 === $expire && !empty($key) && @is_file($file) && $this->is_data_uptodate($key, $group, $data)) {
$this->dc_log('info', $group.':'.$cache_key, __FUNCTION__.'()->nochanges');
if ($this->cf()->is_dctrue('DEV')) {
$this->dc_log('info', $group.':'.$cache_key, __FUNCTION__.'()->nochanges');
}

return false;
}
Expand All @@ -1025,6 +1031,7 @@ private function dc_save($cache_key, $data, $group = 'default', $expire = 0, $ke

if ($this->multisite) {
// try to avoid error-prone
// in rare condition, get_current_network_id not exists.
try {
$meta['network_id'] = get_current_network_id();
} catch (\Exception $e) {
Expand All @@ -1040,10 +1047,15 @@ private function dc_save($cache_key, $data, $group = 'default', $expire = 0, $ke
$meta['data'] = $data;

if (true === $this->dc_code($file, $meta)) {
// if 0 let gc handle it by comparing file mtime.
if ($timeout > 0) {
@touch($file, $timeout);
}

if ($this->cf()->is_dctrue('DEV')) {
$this->dc_log('info', $group.':'.$cache_key, __FUNCTION__.'()->todisk');
}

return true;
}

Expand Down Expand Up @@ -1130,7 +1142,9 @@ private function dc_precache_set($hash)

if (!empty($data)) {
if ($this->is_data_uptodate($hash, $group, $data)) {
$this->dc_log('info', $group.':'.$hash, __FUNCTION__.'()->nochanges');
if ($this->cf()->is_dctrue('DEV')) {
$this->dc_log('info', $group.':'.$hash, __FUNCTION__.'()->nochanges');
}

return;
}
Expand All @@ -1149,7 +1163,7 @@ private function dc_precache()
}

$dostrip = !empty($_SERVER['QUERY_STRING']);
if ($dostrip && !empty($_GET) && !empty($_GET['_wpnonce']) || !empty($_GET['action']) || !empty($_GET['message']) || isset($_GET['doing_wp_cron']) || isset($_GET['_fs_blog_admin'])) {
if ($dostrip && !empty($_GET) && isset($_GET['docketcache_ping']) || isset($_GET['doing_wp_cron']) || isset($_GET['_fs_blog_admin']) || !empty($_GET['_wpnonce']) || !empty($_GET['action']) || !empty($_GET['message'])) {
return;
}

Expand Down Expand Up @@ -1209,12 +1223,7 @@ private function dc_init()

if ($this->cf()->is_dcint('MAXTTL', $dcvalue)) {
if (!empty($dcvalue)) {
if ($dcvalue < 86400) {
$dcvalue = 345600;
} elseif ($dcvalue > 2419200) {
$dcvalue = 2419200;
}
$this->cache_maxttl = $dcvalue;
$this->cache_maxttl = $this->fs()->sanitize_maxttl($dcvalue);
}
}

Expand Down Expand Up @@ -1251,6 +1260,18 @@ private function dc_init()
$this->fs()->optimize_alloptions();
}

add_filter(
'pre_cache_alloptions',
function ($alloptions) {
if (isset($alloptions['cron'])) {
unset($alloptions['cron']);
}

return $alloptions;
},
PHP_INT_MAX
);

foreach (['added', 'updated', 'deleted'] as $prefix) {
add_action(
$prefix.'_option',
Expand Down Expand Up @@ -1298,22 +1319,6 @@ function () {
);
}

// cron
add_filter(
'pre_clear_scheduled_hook',
function ($a, $hook, $args) {
add_action(
'shutdown',
function () {
$this->delete('alloptions', 'options');
},
PHP_INT_MAX - 1
);
},
PHP_INT_MAX,
3
);

// filtered groups hooks
if (\is_array($this->filtered_groups)) {
add_action(
Expand Down
2 changes: 1 addition & 1 deletion includes/object-cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @wordpress-plugin
* Plugin Name: Docket Cache Drop-in
* Plugin URI: https://wordpress.org/plugins/docket-cache/
* Version: 20.10.03
* Version: 20.10.04
* Description: A persistent object cache stored as a plain PHP code, accelerates caching with OPcache backend.
* Author: Nawawi Jamili
* Author URI: https://docketcache.com
Expand Down
38 changes: 16 additions & 22 deletions includes/src/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,28 +213,22 @@ public function rungc()
$this->halt_error(__('Garbage collector not available.', 'docket-cache'));
}

$is_debug = \defined('WP_DEBUG') && WP_DEBUG;
$pad = $is_debug ? 25 : 10;
WP_CLI::line(__('Executing garbage collector. Please wait..', 'docket-cache'));

$pad = 35;
$collect = apply_filters('docketcache/garbage-collector', true);
$header = [
'maxttl' => 'MaxTTL Seconds',
'maxttl_h' => 'MaxTTL Human',
'maxttl_c' => 'MaxTTL Cleaned',
'maxfile' => 'MaxFiles',
'maxfile_c' => 'MaxFiles Cleaned',
'total' => 'Totals',
'clean' => 'Cleaned',
'expired' => 'Expired',
'ignore' => 'Ignored',
];
foreach ($collect as $n => $v) {
if (!$is_debug && 'max' === substr($n, 0, 3)) {
continue;
}
$n = $this->title($header[$n], $pad);
WP_CLI::line($n.$v);
}
$this->halt_success(__('Executing garbage collector.', 'docket-cache'));

WP_CLI::line($this->title(__('Cache MaxTTL', 'docket-cache'), $pad).$collect->cache_maxttl);
WP_CLI::line($this->title(__('Cache File Limit', 'docket-cache'), $pad).$collect->cache_maxfile);
WP_CLI::line($this->title(__('Cache Disk Limit', 'docket-cache'), $pad).$this->pt->normalize_size($collect->cache_maxdisk));
WP_CLI::line($this->title(__('Cleanup Cache MaxTTL', 'docket-cache'), $pad).$collect->cleanup_maxttl);
WP_CLI::line($this->title(__('Cleanup Cache File Limit', 'docket-cache'), $pad).$collect->cleanup_maxfile);
WP_CLI::line($this->title(__('Cleanup Cache Disk Limit', 'docket-cache'), $pad).$collect->cleanup_maxdisk);
WP_CLI::line($this->title(__('Total Cache Cleanup', 'docket-cache'), $pad).$collect->cache_cleanup);
WP_CLI::line($this->title(__('Total Cache Ignored', 'docket-cache'), $pad).$collect->cache_ignore);
WP_CLI::line($this->title(__('Total Cache File', 'docket-cache'), $pad).$collect->cache_file);

$this->halt_success(__('Executing garbage collector completed.', 'docket-cache'));
}

/**
Expand All @@ -251,6 +245,6 @@ public function rungc()
*/
public function type()
{
$this->halt_status($this->pt->slug);
$this->halt_status($this->pt->slug.' (v'.$this->pt->version().')');
}
}
3 changes: 3 additions & 0 deletions includes/src/Constans.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,9 @@ public function register_default()
'site-transient:update_themes',
'site-transient:update_plugins',
'site-transient:update_core',
'options:uninstall_plugins',
'options:active_plugins',
'options:cron',
]
);

Expand Down
2 changes: 1 addition & 1 deletion includes/src/Crawler.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

final class Crawler
{
private static $version = '20.10.03';
private static $version = '20.10.04';
public static $send_cookie = false;

private static function default_args($param = [])
Expand Down
68 changes: 27 additions & 41 deletions includes/src/CronAgent.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ private function is_ping_request()
return !empty($_POST['ping']) && !empty($_GET['docketcache_ping']) && !empty($_SERVER['REQUEST_URI']) && false !== strpos($_SERVER['REQUEST_URI'], '/?docketcache_ping=');
}

private function maybe_disable_wp_cron()
{
// signal wp do not run wp-cron
$this->pt->cf()->maybe_define('DISABLE_WP_CRON', true);
}

private function send_action($action, $is_hello = false)
{
$is_quick = $is_hello && 'pong' !== $is_hello ? true : false;
Expand Down Expand Up @@ -220,23 +226,6 @@ function ($arr) {
$this->pt->close_exit(json_encode($response, JSON_UNESCAPED_SLASHES));
}

private function get_wpcron_lock()
{
$value = 0;
if (wp_using_ext_object_cache()) {
$value = wp_cache_get('doing_cron', 'transient', true);
} else {
if (nwdcx_wpdb($wpdb)) {
$row = $wpdb->get_row("SELECT `option_value` FROM `{$wpdb->options}` WHERE `option_name` = '_transient_doing_cron' LIMIT 1");
if (\is_object($row)) {
$value = $row->option_value;
}
}
}

return $value;
}

private function run_wpcron($run_now = false)
{
$crons = $this->pt->get_crons($run_now, $cron_event);
Expand All @@ -263,30 +252,24 @@ private function run_wpcron($run_now = false)
}

$gmt_time = microtime(true);
$doing_cron_transient = get_transient('doing_cron');
$doing_wp_cron = $doing_cron_transient;

if ($this->is_pingpong) {
$doing_wp_cron = sprintf('%.22F', microtime(true));
if (!empty($doing_cron_transient) && ((int) $doing_cron_transient + 60 > $gmt_time)) {
// hijack current process
$results['wpcron_doing'] = 'Process locked, reset current lock';
$doing_wp_cron = $doing_wp_cron + 60;
}

set_transient('doing_cron', $doing_wp_cron, 120);
}
// overwrite cron lock
$doing_wp_cron = sprintf('%.22F', microtime(true));
set_transient('doing_cron', $doing_wp_cron, 86400);

$run_event = 0;
$slowdown = 0;
$delay = $this->is_pingpong ? 750 : 200;

foreach ($crons as $timestamp => $cronhooks) {
if (false === $run_now && ($timestamp > $gmt_time)) {
continue;
}

if ($slowdown > 10) {
$slowdown = 0;
usleep(200);

usleep($delay);
}

++$slowdown;
Expand Down Expand Up @@ -314,21 +297,17 @@ private function run_wpcron($run_now = false)
wp_clear_scheduled_hook($hook);
--$run_event;
}

if ($this->is_pingpong && ($this->get_wpcron_lock() !== $doing_wp_cron)) {
$results['wpcron_return'] = 2;
$results['wpcron_msg'] = 'Timeout, another cron process stole the lock';
$results['wpcron_event'] = $run_event;

return $results;
}
}
}
}

if ($this->is_pingpong && ($this->get_wpcron_lock() === $doing_wp_cron)) {
delete_transient('doing_cron');
}
unset($cronhooks);

// lock must below 10 minutes
// wp-includes/cron.php -> spawn_cron()
// wp-cron.php
$lock_wp_cron = microtime(true) + 300;
set_transient('doing_cron', $lock_wp_cron, 86400);

$results['wpcron_return'] = 1;
$results['wpcron_event'] = $run_event;
Expand All @@ -348,6 +327,9 @@ private function receive_ping()
return;
}

// signal wp do not run wp-cron
$this->maybe_disable_wp_cron();

$site_url = $this->pt->site_url();

if (!empty($_POST['token'])) {
Expand Down Expand Up @@ -484,6 +466,10 @@ private function check_connection()
$timestamp = strtotime('+90 minutes', strtotime($pingdata['timestamp']));
if ($timestamp > 0 && time() > $timestamp) {
$this->pt->co()->setlock('check_connection', time() + 300);

// signal wp do not run wp-cron
$this->maybe_disable_wp_cron();

$this->send_action('on', true);
}
}
Expand Down
Loading

0 comments on commit b9df4b8

Please sign in to comment.