CacheEngine.php
3 years ago
CacheException.php
1 year ago
CacheNameSpaceTrait.php
1 month ago
ObjectCache.php
1 year ago
WPCacheEngine.php
1 year ago
CacheNameSpaceTrait.php
119 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Automattic\WooCommerce\Caching; |
| 4 | |
| 5 | /** |
| 6 | * Implements namespacing algorithm to simulate grouping and namespacing for wp_cache, memcache and other caching engines that don't support grouping natively. |
| 7 | * |
| 8 | * See the algorithm details here: https://github.com/memcached/memcached/wiki/ProgrammingTricks#namespacing. |
| 9 | * |
| 10 | * To use the namespacing algorithm in the CacheEngine class: |
| 11 | * 1. Use a group string to identify all objects of a type. |
| 12 | * 2. Before setting cache, prefix the cache key by using the `get_cache_prefix`. |
| 13 | * 3. Use `invalidate_cache_group` function to invalidate all caches in entire group at once. |
| 14 | */ |
| 15 | trait CacheNameSpaceTrait { |
| 16 | |
| 17 | /** |
| 18 | * Get prefix for use with wp_cache_set. Allows all cache in a group to be invalidated at once. |
| 19 | * |
| 20 | * @param string $group Group of cache to get. |
| 21 | * @return string Prefix. |
| 22 | */ |
| 23 | public static function get_cache_prefix( $group ) { |
| 24 | // Get cache key - uses cache key wc_orders_cache_prefix to invalidate when needed. |
| 25 | $cache_key = 'wc_' . $group . '_cache_prefix'; |
| 26 | $found = false; |
| 27 | $prefix = wp_cache_get( $cache_key, $group, false, $found ); |
| 28 | |
| 29 | if ( self::is_valid_cache_prefix( $prefix ) ) { |
| 30 | return 'wc_cache_' . $prefix . '_'; |
| 31 | } |
| 32 | |
| 33 | if ( $found ) { |
| 34 | /** |
| 35 | * Fires when WooCommerce detects an invalid cache prefix before replacing it. |
| 36 | * |
| 37 | * @since 10.8.0 |
| 38 | * |
| 39 | * @param string $group Cache group. |
| 40 | * @param mixed $prefix Invalid cached prefix value. |
| 41 | */ |
| 42 | do_action( 'woocommerce_invalid_cache_prefix_detected', $group, $prefix ); |
| 43 | } |
| 44 | |
| 45 | $prefix = self::generate_cache_prefix(); |
| 46 | |
| 47 | if ( ! $found ) { |
| 48 | // Use add on cold prefixes so concurrent requests converge on the first |
| 49 | // persisted value instead of writing competing cache namespaces. |
| 50 | if ( wp_cache_add( $cache_key, $prefix, $group ) ) { |
| 51 | return 'wc_cache_' . $prefix . '_'; |
| 52 | } |
| 53 | |
| 54 | $cached_prefix = wp_cache_get( $cache_key, $group ); |
| 55 | |
| 56 | if ( self::is_valid_cache_prefix( $cached_prefix ) ) { |
| 57 | return 'wc_cache_' . $cached_prefix . '_'; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | wp_cache_set( $cache_key, $prefix, $group ); |
| 62 | |
| 63 | return 'wc_cache_' . $prefix . '_'; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Increment group cache prefix (invalidates cache). |
| 68 | * |
| 69 | * @param string $group Group of cache to clear. |
| 70 | */ |
| 71 | public static function incr_cache_prefix( $group ) { |
| 72 | wc_deprecated_function( 'WC_Cache_Helper::incr_cache_prefix', '3.9.0', 'WC_Cache_Helper::invalidate_cache_group' ); |
| 73 | self::invalidate_cache_group( $group ); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Invalidate cache group. |
| 78 | * |
| 79 | * @param string $group Group of cache to clear. |
| 80 | * @return bool True when the new prefix was persisted to the object cache, |
| 81 | * false otherwise. |
| 82 | * @since 3.9.0 |
| 83 | */ |
| 84 | public static function invalidate_cache_group( $group ) { |
| 85 | return wp_cache_set( 'wc_' . $group . '_cache_prefix', self::generate_cache_prefix(), $group ); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Helper method to get prefixed key. |
| 90 | * |
| 91 | * @param string $key Key to prefix. |
| 92 | * @param string $group Group of cache to get. |
| 93 | * |
| 94 | * @return string Prefixed key. |
| 95 | */ |
| 96 | public static function get_prefixed_key( $key, $group ) { |
| 97 | return self::get_cache_prefix( $group ) . $key; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Generate a cache-safe prefix value. |
| 102 | * |
| 103 | * @return string Cache prefix. |
| 104 | */ |
| 105 | private static function generate_cache_prefix() { |
| 106 | return str_replace( ' ', '_', microtime() ) . '_' . bin2hex( random_bytes( 8 ) ); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Check whether a cached prefix can be used as a cache-key namespace. |
| 111 | * |
| 112 | * @param mixed $prefix Cached prefix value. |
| 113 | * @return bool True if the prefix is valid. |
| 114 | */ |
| 115 | private static function is_valid_cache_prefix( $prefix ) { |
| 116 | return is_string( $prefix ) && '' !== trim( $prefix ); |
| 117 | } |
| 118 | } |
| 119 |