| 1 |
<?php |
| 2 |
namespace ABlocks\Classes; |
| 3 |
|
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
/** |
| 9 |
* Shared cache backend for the Performance Suite. |
| 10 |
* |
| 11 |
* Wraps the object cache, falling back to transients, and — more importantly — |
| 12 |
* lets a feature ask whether caching is actually worth doing before it starts. |
| 13 |
* |
| 14 |
* ## Why the distinction matters |
| 15 |
* |
| 16 |
* Without a persistent object cache (Redis, Memcached), `wp_cache_*` is |
| 17 |
* per-request memory only and transients are rows in `wp_options`, so every |
| 18 |
* read is a database round trip. That is fine when the cached work is |
| 19 |
* expensive, and actively harmful when it is not. |
| 20 |
* |
| 21 |
* Measured here on the block template cache: resolving templates costs 7–10 |
| 22 |
* queries, but caching the result in transients produced *more* queries than it |
| 23 |
* saved — 81 per request cached versus 75 uncached — because each lookup adds |
| 24 |
* its own option reads and has to unserialize large WP_Block_Template objects. |
| 25 |
* With a persistent object cache the same lookup is an out-of-process fetch |
| 26 |
* with no database involvement, and the trade flips. |
| 27 |
* |
| 28 |
* So features that cache cheap-but-frequent work call |
| 29 |
* {@see CacheBackend::is_persistent()} and disable themselves when it returns |
| 30 |
* false, rather than assuming a cache is free. |
| 31 |
*/ |
| 32 |
class CacheBackend { |
| 33 |
|
| 34 |
const GROUP = 'ablocks_perf'; |
| 35 |
|
| 36 |
/** |
| 37 |
* Is a persistent (cross-request, out-of-process) object cache available? |
| 38 |
* |
| 39 |
* @return bool |
| 40 |
*/ |
| 41 |
public static function is_persistent() { |
| 42 |
return function_exists( 'wp_using_ext_object_cache' ) && wp_using_ext_object_cache(); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Read a value. |
| 47 |
* |
| 48 |
* @param string $key Cache key. |
| 49 |
* @param bool $use_fallback Fall back to a transient when no object cache exists. |
| 50 |
* @return mixed|false |
| 51 |
*/ |
| 52 |
public static function get( $key, $use_fallback = true ) { |
| 53 |
if ( self::is_persistent() ) { |
| 54 |
$found = false; |
| 55 |
$value = wp_cache_get( $key, self::GROUP, false, $found ); |
| 56 |
if ( $found ) { |
| 57 |
return $value; |
| 58 |
} |
| 59 |
return false; |
| 60 |
} |
| 61 |
|
| 62 |
return $use_fallback ? get_transient( self::GROUP . '_' . $key ) : false; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Write a value. |
| 67 |
* |
| 68 |
* @param string $key Cache key. |
| 69 |
* @param mixed $value Payload. |
| 70 |
* @param int $ttl Lifetime in seconds. |
| 71 |
* @param bool $use_fallback Fall back to a transient when no object cache exists. |
| 72 |
* @return bool |
| 73 |
*/ |
| 74 |
public static function set( $key, $value, $ttl, $use_fallback = true ) { |
| 75 |
$ttl = max( 60, (int) $ttl ); |
| 76 |
|
| 77 |
if ( self::is_persistent() ) { |
| 78 |
return (bool) wp_cache_set( $key, $value, self::GROUP, $ttl ); |
| 79 |
} |
| 80 |
|
| 81 |
return $use_fallback ? (bool) set_transient( self::GROUP . '_' . $key, $value, $ttl ) : false; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Delete a value. |
| 86 |
* |
| 87 |
* @param string $key Cache key. |
| 88 |
* @return bool |
| 89 |
*/ |
| 90 |
public static function delete( $key ) { |
| 91 |
if ( self::is_persistent() ) { |
| 92 |
return (bool) wp_cache_delete( $key, self::GROUP ); |
| 93 |
} |
| 94 |
return (bool) delete_transient( self::GROUP . '_' . $key ); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Read a monotonic generation counter. |
| 99 |
* |
| 100 |
* Stored autoloaded so it costs no query on a normal page load. Bumping it |
| 101 |
* invalidates every key built from it, which is one write regardless of how |
| 102 |
* many entries exist and cannot half-complete the way a scan-and-delete can. |
| 103 |
* |
| 104 |
* @param string $option Option name. |
| 105 |
* @return int |
| 106 |
*/ |
| 107 |
public static function generation( $option ) { |
| 108 |
return (int) get_option( $option, 0 ); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Advance a generation counter. |
| 113 |
* |
| 114 |
* @param string $option Option name. |
| 115 |
* @return void |
| 116 |
*/ |
| 117 |
public static function bump_generation( $option ) { |
| 118 |
update_option( $option, self::generation( $option ) + 1, true ); |
| 119 |
} |
| 120 |
} |
| 121 |
|