Skip to content

Commit

Permalink
bump 22.07.05
Browse files Browse the repository at this point in the history
- Fixed: Plugin::register_plugin_hooks() -> Undefined property: stdClass::$slug.
- Fixed: Event::garbage_collector() -> Stale cache, invalid filter for comment_feed.
- Fixed: Event::garbage_collector() -> Stale cache, add filter for adjacent_post, wp_get_archives and get_comment_child_ids.
- Fixed: Tweaks::wplazyload() -> Add filter for wp_get_attachment_image_attributes.
- Fixed: WP_Object_Cache::dc_save() -> Returns false if data type is "unknown type".
- Added: Filesystem::is_wp_cache_group_queries() -> Match group for *-queries.
- Added: WP_Object_Cache::maybe_expire() ->  Match group for *-queries.
  • Loading branch information
nawawi committed Mar 18, 2023
1 parent 449b870 commit 2e42f42
Show file tree
Hide file tree
Showing 18 changed files with 208 additions and 95 deletions.
12 changes: 12 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
= v22.07.05 (2023-03-18) =

- Fixed: Plugin::register_plugin_hooks() -> Undefined property: stdClass::$slug.
- Fixed: Event::garbage_collector() -> Stale cache, invalid filter for comment_feed.
- Fixed: Event::garbage_collector() -> Stale cache, add filter for adjacent_post, wp_get_archives and get_comment_child_ids.
- Fixed: Tweaks::wplazyload() -> Add filter for wp_get_attachment_image_attributes.
- Fixed: WP_Object_Cache::dc_save() -> Returns false if data type is "unknown type".
- Added: Filesystem::is_wp_cache_group_queries() -> Match group for *-queries.
- Added: WP_Object_Cache::maybe_expire() -> Match group for *-queries.

Thanks to Ronny from web55.se for bug report.

= v22.07.04 (2023-03-01) =

- Fixed: Advanced Post Cache -> Only visible to wp < 6.1.1 as it is already implemented in wp core (WP_Query caching).
Expand Down
Binary file modified dist/docket-cache.zip
Binary file not shown.
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://docketcache.com/?utm_source=wp-plugins&utm_campaign=plugin-uri&utm_medium=wp-dash
* Version: 22.07.04
* VerPrev: 22.07.03
* Version: 22.07.05
* VerPrev: 22.07.04
* 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
47 changes: 33 additions & 14 deletions includes/cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -939,6 +939,12 @@ private function maybe_expire($group, $expire = 0, $key = '')
$expire = $maxttl < 1209600 ? 1209600 : $maxttl; // 14d
}

// wp stale cache
// group-queries: wp >= 6.3
elseif ($this->fs()->is_wp_cache_group_queries($group)) {
$expire = $maxttl < 86400 ? $maxttl : 86400; // 1d
}

// wp stale cache
// prefix:md5hash:microtime
// wp_query|get_terms|get_comments|comment_feed|get_sites|get_network_ids|get_page_by_path|other?
Expand Down Expand Up @@ -1058,6 +1064,10 @@ private function skip_stats($group, $key = '')
*/
private function has_stalecache($key, $group = '')
{
if ($this->fs()->is_wp_cache_group_queries($group)) {
return true;
}

if ('wc_' === substr($key, 0, 3) && '_cache_prefix' === substr($key, -13)) {
return true;
}
Expand All @@ -1083,7 +1093,7 @@ private function has_stalecache($key, $group = '')
private function is_stalecache_ignored($key, $group = '')
{
if ($this->ignore_stalecache) {
return $this->has_stalecache($key, $group = '');
return $this->has_stalecache($key, $group);
}

return false;
Expand Down Expand Up @@ -1514,6 +1524,7 @@ private function dc_save($cache_key, $data, $group = 'default', $expire = 0, $ke
if (('' === $data || (\is_array($data) && empty($data))) && ($this->fs()->is_transient($group) || $this->ignore_emptycache)) {
nwdcx_debuglog(__FUNCTION__.': '.$logkey.': Process aborted. No data availale.');
$this->fs()->unlink($file, false);
unset($this->precache[$group][$cache_key]);

return true;
}
Expand All @@ -1527,27 +1538,35 @@ private function dc_save($cache_key, $data, $group = 'default', $expire = 0, $ke
$timeout = ($expire > 0 ? time() + $expire : 0);

$type = \gettype($data);
if ('unknown type' === $type) {
return false;
}

if ('NULL' === $type && null === $data) {
$data = '';
}

if (!empty($data)) {
// Abort if object too large.
$len = 0;
$nwdcx_suppresserrors = nwdcx_suppresserrors(true);
if (\function_exists('maybe_serialize')) {
$len = \strlen(@maybe_serialize($data));
} else {
$len = \strlen(@serialize($data));
}
nwdcx_suppresserrors($nwdcx_suppresserrors);
if (!\in_array($type, ['boolean', 'integer', 'double', 'NULL'])) {
// Abort if object too large.
$len = 0;
$nwdcx_suppresserrors = nwdcx_suppresserrors(true);
if (\function_exists('maybe_serialize')) {
$len = \strlen(@maybe_serialize($data));
} else {
$len = \strlen(@serialize($data));
}
nwdcx_suppresserrors($nwdcx_suppresserrors);

if ($len >= $this->cache_maxsize) {
$this->dc_log('err', $logkey, $group.':'.$cache_key.' '.$logpref.' Object too large -> '.$len.'/'.$this->cache_maxsize);
if ($len >= $this->cache_maxsize) {
$this->dc_log('err', $logkey, $group.':'.$cache_key.' '.$logpref.' The size of object has breached '.$len.' of '.$this->cache_maxsize.' bytes.');

nwdcx_debuglog(__FUNCTION__.': '.$logkey.': Process aborted. Object too large ('.$len.'/'.$this->cache_maxsize.')');
nwdcx_debuglog(__FUNCTION__.': '.$logkey.': Process aborted. The size of object has breached '.$len.' of '.$this->cache_maxsize.' bytes.');
$this->fs()->unlink($file, false);
unset($this->precache[$group][$cache_key]);

return false;
return false;
}
}

// Unserialize content first.
Expand Down
4 changes: 2 additions & 2 deletions includes/object-cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
* @wordpress-plugin
* Plugin Name: Docket Cache Drop-in
* Plugin URI: https://wordpress.org/plugins/docket-cache/
* Version: 22.07.04
* Description: A persistent object cache stored as a plain PHP code, accelerates caching with OPcache backend.
* Version: 22.07.05
* Description: Object Cache drop-in for Docket Cache.
* Author: Nawawi Jamili
* Author URI: https://docketcache.com
* Requires at least: 5.4
Expand Down
2 changes: 2 additions & 0 deletions includes/src/Becache.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ private function store_cache($key, $data, $group, $expire = 0)
nwdcx_suppresserrors($nwdcx_suppresserrors);

if ($len >= $this->cache_maxsize) {
$this->fs()->unlink($file, false);

return false;
}

Expand Down
2 changes: 2 additions & 0 deletions includes/src/Constans.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,11 +239,13 @@ public function register_default()
'blog-lookup',
'global-posts',
'networks',
'network-queries',
'rss',
'sites',
'site-details',
'site-lookup',
'site-options',
'site-queries',
'site-transient',
'users',
'useremail',
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 = '22.07.04';
private static $version = '22.07.05';
public static $send_cookie = false;

private static function default_args($param = [])
Expand Down
51 changes: 47 additions & 4 deletions includes/src/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,12 +258,15 @@ public function garbage_collector($force = false)
'posts' => 'wp_query',
'terms' => 'get_terms',
'comment' => 'get_comments',
'comment_feed' => 'comment_feed',
'sites' => 'get_sites',
'networks' => 'get_network_ids',
];
foreach ($wp_cache_last_changed_match as $grp => $kk) {
$wp_cache_last_changed[$grp] = wp_cache_get_last_changed($grp);
// wp >= 6.3
// remove ending 's' -> posts = post-queries.
$grpq = strtok($grp, 's').'-queries';
$wp_cache_last_changed[$grpq] = $wp_cache_last_changed[$grp];
}

$wp_cache_last_changed['advpost'] = wp_cache_get('cache_incr', 'docketcache-post');
Expand Down Expand Up @@ -413,9 +416,30 @@ public function garbage_collector($force = false)

if ($is_flush_stalecache) {
// wp stale cache
if (!empty($wp_cache_last_changed_match[$data['group']]) && preg_match('@^(wp_query|get_terms|get_comments|comment_feed|get_sites|get_network_ids|get_page_by_path):([0-9a-f]{32}):([0-9\. ]{21})([0-9\. ]+)?$@', $data['key'], $mm)) {
if ('get_page_by_path' === $mm[1]) {
$mm[1] = 'wp_query';
// wp >= 6.3
if ($is_ignore_stalecache && $this->is_wp_cache_group_queries($data['group'])) {
if (@unlink($fx)) {
clearstatcache(true, $fx);

nwdcx_cliverbose('run-gc:stale-cache: '.$fx."\n");

++$collect->cleanup_stalecache;
continue;
}
}

// wp stale cache
// prefix:hash:timestamp timestamp
if (!empty($wp_cache_last_changed_match[$data['group']]) && preg_match('@^(wp_query|get_terms|get_comments|comment_feed|get_sites|get_network_ids|get_page_by_path|adjacent_post|wp_get_archives):([0-9a-f]{32}):([0-9\. ]{21})([0-9\. ]+)?$@', $data['key'], $mm)) {
// main type
switch ($mm[1]) {
case 'get_page_by_path':
case 'adjacent_post':
$mm[1] = 'wp_query';
break;
case 'comment_feed':
$mm[1] = 'get_comments';
break;
}

$km = $wp_cache_last_changed_match[$data['group']];
Expand All @@ -432,6 +456,25 @@ public function garbage_collector($force = false)
}
}

// wp stale cache
// get_comment_child_ids:int:hash:timestamp timestamp
// get_comment_child_ids:1654:5247020d1a40e3b2e9a40de1139bc5c9:0.48761900 1678553206
if (!empty($wp_cache_last_changed_match[$data['group']]) && preg_match('@^(get_comment_child_ids):(\d+):([0-9a-f]{32}):([0-9\. ]{21})([0-9\. ]+)?$@', $data['key'], $mm)) {
$mm[1] = 'get_comments';
$km = $wp_cache_last_changed_match[$data['group']];

if (($km === $mm[1] && $wp_cache_last_changed[$data['group']] !== $mm[4]) || $is_ignore_stalecache) {
if (@unlink($fx)) {
clearstatcache(true, $fx);

nwdcx_cliverbose('run-gc:stale-cache: '.$fx."\n");

++$collect->cleanup_stalecache;
continue;
}
}
}

// advpost stale cache
if (false !== strpos($data['group'], 'docketcache-post-') && preg_match('@^docketcache-post-(\d+)$@', $data['group'], $mm)) {
if ((int) $wp_cache_last_changed['advpost'] !== (int) $mm[1] || $is_ignore_stalecache) {
Expand Down
8 changes: 8 additions & 0 deletions includes/src/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ public function is_wp_options($group)
return 'options' === $group || 'site-options' === $group;
}

/**
* is_wp_cache_group_queries.
*/
public function is_wp_cache_group_queries($group)
{
return \in_array($group, ['term-queries', 'post-queries', 'comment-queries', 'site-queries', 'network-queries']);
}

/**
* is_dirempty.
*/
Expand Down
8 changes: 6 additions & 2 deletions includes/src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -1327,7 +1327,11 @@ function ($option, $auto_updates, $old_auto_updates, $network_id) {
add_filter(
'pre_update_site_option_auto_update_plugins',
function ($value, $old_value, $option, $network_id) {
return array_values($value);
if (\is_array($value) && !empty($value)) {
return array_values($value);
}

return $value;
},
10,
4
Expand All @@ -1336,7 +1340,7 @@ function ($value, $old_value, $option, $network_id) {
add_filter(
'auto_update_plugin',
function ($update, $item) {
if ('docket-cache' === $item->slug) {
if (\is_object($item) && isset($item->slug) && 'docket-cache' === $item->slug) {
if (\defined('DOCKET_CACHE_AUTOUPDATE') && \is_bool(DOCKET_CACHE_AUTOUPDATE)) {
return DOCKET_CACHE_AUTOUPDATE;
}
Expand Down
13 changes: 6 additions & 7 deletions includes/src/Tweaks.php
Original file line number Diff line number Diff line change
Expand Up @@ -928,13 +928,12 @@ function () {

public function wplazyload()
{
add_action(
'init',
function () {
add_filter('wp_lazy_loading_enabled', '__return_false');
},
\PHP_INT_MAX
);
add_filter('wp_lazy_loading_enabled', '__return_false', \PHP_INT_MAX);
add_filter('wp_get_attachment_image_attributes', function ($attr, $attachment, $size) {
$attr['loading'] = 'eager';

return $attr;
}, \PHP_INT_MAX, 3);
}

public function wpsitemap()
Expand Down
17 changes: 15 additions & 2 deletions includes/vendor/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,21 @@
// autoload.php @generated by Composer

if (PHP_VERSION_ID < 50600) {
echo 'Composer 2.3.0 dropped support for autoloading on PHP <5.6 and you are running '.PHP_VERSION.', please upgrade PHP or use Composer 2.2 LTS via "composer self-update --2.2". Aborting.'.PHP_EOL;
exit(1);
if (!headers_sent()) {
header('HTTP/1.1 500 Internal Server Error');
}
$err = 'Composer 2.3.0 dropped support for autoloading on PHP <5.6 and you are running '.PHP_VERSION.', please upgrade PHP or use Composer 2.2 LTS via "composer self-update --2.2". Aborting.'.PHP_EOL;
if (!ini_get('display_errors')) {
if (PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg') {
fwrite(STDERR, $err);
} elseif (!headers_sent()) {
echo $err;
}
}
trigger_error(
$err,
E_USER_ERROR
);
}

require_once __DIR__ . '/composer/autoload_real.php';
Expand Down
41 changes: 27 additions & 14 deletions includes/vendor/composer/ClassLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
*/
class ClassLoader
{
/** @var \Closure(string):void */
private static $includeFile;

/** @var ?string */
private $vendorDir;

Expand Down Expand Up @@ -106,6 +109,7 @@ class ClassLoader
public function __construct($vendorDir = null)
{
$this->vendorDir = $vendorDir;
self::initializeIncludeClosure();
}

/**
Expand Down Expand Up @@ -425,7 +429,8 @@ public function unregister()
public function loadClass($class)
{
if ($file = $this->findFile($class)) {
includeFile($file);
$includeFile = self::$includeFile;
$includeFile($file);

return true;
}
Expand Down Expand Up @@ -555,18 +560,26 @@ private function findFileWithExtension($class, $ext)

return false;
}
}

/**
* Scope isolated include.
*
* Prevents access to $this/self from included files.
*
* @param string $file
* @return void
* @private
*/
function includeFile($file)
{
include $file;
/**
* @return void
*/
private static function initializeIncludeClosure()
{
if (self::$includeFile !== null) {
return;
}

/**
* Scope isolated include.
*
* Prevents access to $this/self from included files.
*
* @param string $file
* @return void
*/
self::$includeFile = \Closure::bind(static function($file) {
include $file;
}, null, null);
}
}
Loading

0 comments on commit 2e42f42

Please sign in to comment.