From 34b05c8b423f1f666dbec1ae6e0ab16ede2ba3a3 Mon Sep 17 00:00:00 2001 From: Matthew Haines-Young Date: Mon, 24 Jun 2024 15:20:42 +0100 Subject: [PATCH 1/4] Support storage of cache in an option --- inc/namespace.php | 49 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 15 deletions(-) diff --git a/inc/namespace.php b/inc/namespace.php index 5f19235..2eb135f 100644 --- a/inc/namespace.php +++ b/inc/namespace.php @@ -23,6 +23,7 @@ use RuntimeException; const CRON_ACTION = 'hm.swrCache.cron'; +const ALLOWED_CACHE_STORAGE = [ 'cache', 'option' ]; /** * Bootstrapping. @@ -30,7 +31,7 @@ * @return void */ function bootstrap() : void { - add_action( CRON_ACTION, __NAMESPACE__ . '\\do_cron', 10, 6 ); + add_action( CRON_ACTION, __NAMESPACE__ . '\\do_cron', 10, 7 ); } /** @@ -57,8 +58,12 @@ function cache_delete_group( string $cache_group ) : bool { * * @return array An array containing the cached contents (or false) and its expiry timestamp. */ -function cache_get_with_expiry( string $cache_key, string $cache_group = '' ) : array { - $data = wp_cache_get( $cache_key, $cache_group ); +function cache_get_with_expiry( string $cache_key, string $cache_group = '', $cache_storage = 'cache' ) : array { + if ( $cache_storage === 'option') { + $data = get_option( $cache_group . '-' . $cache_key ); + } else { + $data = wp_cache_get( $cache_key, $cache_group ); + } $expiry_timestamp = (int) wp_cache_get( $cache_key . '_expiry', $cache_group ); return [ $data, $expiry_timestamp ]; @@ -73,8 +78,12 @@ function cache_get_with_expiry( string $cache_key, string $cache_group = '' ) : * @param string $cache_key The key for the cache. * @param string $cache_group Optional. The group for the cache. Default value is empty string. */ -function cache_set_with_expiry( string $lock_key, mixed $data, int $cache_duration, string $cache_key, string $cache_group = '' ) : void { - wp_cache_set( $cache_key, $data, $cache_group ); +function cache_set_with_expiry( string $lock_key, mixed $data, int $cache_duration, string $cache_key, string $cache_group = '', $cache_storage = 'cache' ) : void { + if ( $cache_storage === 'option') { + update_option( $cache_group . '-' . $cache_key, $data, false ); // Don't autoload. + } else { + wp_cache_set( $cache_key, $data, $cache_group ); + } wp_cache_set( $cache_key . '_expiry', time() + $cache_duration, $cache_group, $cache_duration ); wp_cache_delete( $lock_key, $cache_group ); } @@ -106,16 +115,21 @@ function cache_is_warm( mixed $data, int $expiry_time ) : bool { * @throws InvalidArgumentException If a closure is provided as a callback. * @throws RuntimeException If an error occurs during the execution of the callback function. */ -function do_cron( string $lock_value, callable $callback, array $callback_args, int $expiry_duration, string $cache_key, string $cache_group = '' ) : void { - if ($callback instanceof Closure) { - throw new InvalidArgumentException("Closures are not allowed as callbacks."); - } +function do_cron( string $lock_value, callable $callback, array $callback_args, int $expiry_duration, string $cache_key, string $cache_group = '', $cache_storage = 'cache' ) : void { + if ( $callback instanceof Closure ) { + throw new InvalidArgumentException("Closures are not allowed as callbacks."); + } + + if ( ! in_array( $cache_storage, ALLOWED_CACHE_STORAGE, true ) ) { + throw new InvalidArgumentException( sprintf( 'Cache storage type not valid. Expected one of %s', implode( ', ', ALLOWED_CACHE_STORAGE ) ) ); + } + $lock_key = "lock_$cache_key"; if ( ! lock_verify( $lock_key, $lock_value, $cache_group ) ) { // Another invocation already reserved this cron job. return; } - $data = $callback( $callback_args ); + $data = $callback( $callback_args ); if ( is_wp_error( $data ) ) { throw new RuntimeException( $data->get_error_message(), $data->get_error_code() ); @@ -132,17 +146,22 @@ function do_cron( string $lock_value, callable $callback, array $callback_args, * @param callable $callback The callback function to fetch the data if not available in cache. * @param array $callback_args The arguments to pass to the callback function. * @param int $cache_duration The duration of fresh cache content in seconds. + * @param string $cache_storage Where cache data is stored. Allowed values are 'cache' or 'options'. * * @return mixed The cached data if available, or false if the data is not available in cache yet. * * @throws InvalidArgumentException If a closure is provided as a callback. */ -function get( string $cache_key, string $cache_group, callable $callback, array $callback_args, int $cache_duration ) : mixed { - if ($callback instanceof Closure) { - throw new InvalidArgumentException("Closures are not allowed as callbacks."); - } +function get( string $cache_key, string $cache_group, callable $callback, array $callback_args, int $cache_duration, string $cache_storage = 'cache' ) : mixed { + if ($callback instanceof Closure) { + throw new InvalidArgumentException( 'Closures are not allowed as callbacks.' ); + } + + if ( ! in_array( $cache_storage, ALLOWED_CACHE_STORAGE, true ) ) { + throw new InvalidArgumentException( sprintf( 'Cache storage type not valid. Expected one of %s. %s given.', implode( ', ', ALLOWED_CACHE_STORAGE ), $cache_storage ) ); + } - [ $data, $expiry_timestamp ] = cache_get_with_expiry( $cache_key, $cache_group ); + [ $data, $expiry_timestamp ] = cache_get_with_expiry( $cache_key, $cache_group, $cache_storage ); if ( cache_is_warm( $data, $expiry_timestamp ) ) { // Cache is warm From bd0b51e6682fb6f49aa6d44d17bdf86bdfa6c3fa Mon Sep 17 00:00:00 2001 From: Sander van Dragt Date: Tue, 25 Jun 2024 12:44:32 +0100 Subject: [PATCH 2/4] StorageProvider work --- inc/class-cachestorageprovider.php | 74 ++++++++++++++++++++ inc/class-optionstorageprovider.php | 100 ++++++++++++++++++++++++++++ inc/class-storageprovider.php | 62 +++++++++++++++++ inc/namespace.php | 56 +++++++--------- plugin.php | 3 + 5 files changed, 264 insertions(+), 31 deletions(-) create mode 100644 inc/class-cachestorageprovider.php create mode 100644 inc/class-optionstorageprovider.php create mode 100644 inc/class-storageprovider.php diff --git a/inc/class-cachestorageprovider.php b/inc/class-cachestorageprovider.php new file mode 100644 index 0000000..e6ddb1b --- /dev/null +++ b/inc/class-cachestorageprovider.php @@ -0,0 +1,74 @@ +dashit( $cache_group ) . $cache_key ); + $expiry_timestamp = (int) get_option( $this->dashit( $cache_group ) . $cache_key . '_expiry' ); + + return [ $data, $expiry_timestamp ]; + } + + /** + * Add a dash to the end of a string if it is not empty. + * + * @param string $str The string to append the dash to. + * + * @return string The modified string. + */ + protected function dashit( $str ) : string { + return $str ? $str . '-' : $str; + } + + /** + * Set data in cache with expiry and delete the lock transient. + * + * @param string $lock_key The name of the lock. + * @param mixed $data The data to be cached. + * @param int $cache_duration The expiry time for the cache in seconds. + * @param string $cache_key The key for the cache. + * @param string $cache_group Optional. The group for the cache. Default value is empty string. + */ + public function set_with_expiry( string $lock_key, mixed $data, int $cache_duration, string $cache_key, string $cache_group = '' ) : void { + update_option( $this->dashit( $cache_group ) . $cache_key, $data, false ); // Don't autoload. + set_transient( $this->dashit( $cache_group ) . $cache_key . '_expiry', time() + $cache_duration, $cache_duration ); + delete_transient( $this->dashit( $cache_group ) . $lock_key ); + } + + /** + * Adds a lock key in the cache to avoid race conditions and enables synchronization between processes. + * It's only stored if the lock doesn't exist (or is expired). + * + * @param string $lock_key The unique key for the lock. + * @param string $cache_group The cache group where the lock key will be stored + * @param int $lock_time The duration in seconds for which the lock will be held. Default is MINUTE_IN_SECONDS constant. + * + * @return string The generated lock value, unique per invocation, regardless whether the lock is added. + */ + public function lock_add( string $lock_key, string $cache_group = '', int $lock_time = MINUTE_IN_SECONDS ) : string { + $lock_value = wp_generate_uuid4(); + // Add will fail if it already exists. + $this->add_transient( $lock_key, $lock_value, $cache_group, $lock_time ); + + return $lock_value; + } + + /** + * Adds a transient value to the cache. + * + * @param string $key The key of the transient value. + * @param mixed $data The data to be stored in the transient value. + * @param string $group The cache group where the transient value will be stored. Default is an empty string. + * @param int $expire The duration in seconds for which the transient value will be stored. Default is 0, which means no expiration. + * + * @return bool True if the transient value was successfully added to the cache, false if the key and group already exists. + */ + protected function add_transient( string $key, mixed $data, string $group = '', int $expire = 0 ) : bool { + $group = $this->dashit( $group ); + if ( get_transient( $group . $key ) ) { + return false; + } + $transient = $group . $key; + + return set_transient( $transient, $data, $expire ); + } +} diff --git a/inc/class-storageprovider.php b/inc/class-storageprovider.php new file mode 100644 index 0000000..71e306c --- /dev/null +++ b/inc/class-storageprovider.php @@ -0,0 +1,62 @@ +delete_group( $cache_group ); } /** @@ -58,15 +62,10 @@ function cache_delete_group( string $cache_group ) : bool { * * @return array An array containing the cached contents (or false) and its expiry timestamp. */ -function cache_get_with_expiry( string $cache_key, string $cache_group = '', $cache_storage = 'cache' ) : array { - if ( $cache_storage === 'option') { - $data = get_option( $cache_group . '-' . $cache_key ); - } else { - $data = wp_cache_get( $cache_key, $cache_group ); - } - $expiry_timestamp = (int) wp_cache_get( $cache_key . '_expiry', $cache_group ); +function cache_get_with_expiry( string $cache_key, string $cache_group = '' ) : array { + global $storage; - return [ $data, $expiry_timestamp ]; + return $storage->get_with_expiry( $cache_key, $cache_group ); } /** @@ -78,14 +77,9 @@ function cache_get_with_expiry( string $cache_key, string $cache_group = '', $ca * @param string $cache_key The key for the cache. * @param string $cache_group Optional. The group for the cache. Default value is empty string. */ -function cache_set_with_expiry( string $lock_key, mixed $data, int $cache_duration, string $cache_key, string $cache_group = '', $cache_storage = 'cache' ) : void { - if ( $cache_storage === 'option') { - update_option( $cache_group . '-' . $cache_key, $data, false ); // Don't autoload. - } else { - wp_cache_set( $cache_key, $data, $cache_group ); - } - wp_cache_set( $cache_key . '_expiry', time() + $cache_duration, $cache_group, $cache_duration ); - wp_cache_delete( $lock_key, $cache_group ); +function cache_set_with_expiry( string $lock_key, mixed $data, int $cache_duration, string $cache_key, string $cache_group = '' ) : void { + global $storage; + $storage->set_with_expiry( $lock_key, $data, $cache_duration, $cache_key, $cache_group ); } /** @@ -115,9 +109,9 @@ function cache_is_warm( mixed $data, int $expiry_time ) : bool { * @throws InvalidArgumentException If a closure is provided as a callback. * @throws RuntimeException If an error occurs during the execution of the callback function. */ -function do_cron( string $lock_value, callable $callback, array $callback_args, int $expiry_duration, string $cache_key, string $cache_group = '', $cache_storage = 'cache' ) : void { +function do_cron( string $lock_value, callable $callback, array $callback_args, int $expiry_duration, string $cache_key, string $cache_group = '', $cache_storage = 'cache' ) : void { if ( $callback instanceof Closure ) { - throw new InvalidArgumentException("Closures are not allowed as callbacks."); + throw new InvalidArgumentException( 'Closures are not allowed as callbacks.' ); } if ( ! in_array( $cache_storage, ALLOWED_CACHE_STORAGE, true ) ) { @@ -153,7 +147,7 @@ function do_cron( string $lock_value, callable $callback, array $callback_args, * @throws InvalidArgumentException If a closure is provided as a callback. */ function get( string $cache_key, string $cache_group, callable $callback, array $callback_args, int $cache_duration, string $cache_storage = 'cache' ) : mixed { - if ($callback instanceof Closure) { + if ( $callback instanceof Closure ) { throw new InvalidArgumentException( 'Closures are not allowed as callbacks.' ); } @@ -161,7 +155,7 @@ function get( string $cache_key, string $cache_group, callable $callback, array throw new InvalidArgumentException( sprintf( 'Cache storage type not valid. Expected one of %s. %s given.', implode( ', ', ALLOWED_CACHE_STORAGE ), $cache_storage ) ); } - [ $data, $expiry_timestamp ] = cache_get_with_expiry( $cache_key, $cache_group, $cache_storage ); + [ $data, $expiry_timestamp ] = cache_get_with_expiry( $cache_key, $cache_group ); if ( cache_is_warm( $data, $expiry_timestamp ) ) { // Cache is warm @@ -192,11 +186,9 @@ function get( string $cache_key, string $cache_group, callable $callback, array * @return string The generated lock value, unique per invocation, regardless whether the lock is added. */ function lock_add( string $lock_key, string $cache_group = '', int $lock_time = MINUTE_IN_SECONDS ) : string { - $lock_value = wp_generate_uuid4(); - // Add will fail if it already exists. - wp_cache_add( $lock_key, $lock_value, $cache_group, $lock_time ); + global $storage; - return $lock_value; + return $storage->lock_add( $lock_key, $cache_group, $lock_time ); } /** @@ -227,7 +219,9 @@ function register_cache_group( string $cache_group ) { if ( function_exists( 'wp_cache_add_redis_hash_groups' ) ) { // Enable cache group flushing for this group wp_cache_add_redis_hash_groups( $cache_group ); + return $wp_object_cache && isset( $wp_object_cache->redis_hash_groups[ $cache_group ] ); } + return false; } diff --git a/plugin.php b/plugin.php index 2e38363..60d729e 100644 --- a/plugin.php +++ b/plugin.php @@ -5,6 +5,9 @@ namespace HM\SwrCache; +require_once __DIR__ . '/inc/class-storageprovider.php'; +require_once __DIR__ . '/inc/class-cachestorageprovider.php'; +require_once __DIR__ . '/inc/class-optionstorageprovider.php'; require_once __DIR__ . '/inc/namespace.php'; bootstrap(); From 01af9a4146f4877a4ce3705aa17fcd8d0a762dd0 Mon Sep 17 00:00:00 2001 From: Sander van Dragt Date: Tue, 25 Jun 2024 14:57:41 +0100 Subject: [PATCH 3/4] Update cache storage provider and related classes This commit involves updating the StorageProvider class and related classes such as CacheStorageProvider and TransoptionStorageProvider. Changes include adding new functions for lock verification and cache group registration. Also, OptionStorageProvider was renamed to TransoptionStorageProvider and several adjustments were made to the `namespace.php` file. These updates aim to enhance the caching functionality and improve data consistency. --- inc/class-cachestorageprovider.php | 35 ++++++ inc/class-storageprovider.php | 58 ++++++++-- ...p => class-transoptionstorageprovider.php} | 50 ++++++++- inc/namespace.php | 106 +++--------------- plugin.php | 2 +- 5 files changed, 149 insertions(+), 102 deletions(-) rename inc/{class-optionstorageprovider.php => class-transoptionstorageprovider.php} (69%) diff --git a/inc/class-cachestorageprovider.php b/inc/class-cachestorageprovider.php index e6ddb1b..9fd1932 100644 --- a/inc/class-cachestorageprovider.php +++ b/inc/class-cachestorageprovider.php @@ -71,4 +71,39 @@ function lock_add( string $lock_key, string $cache_group = '', int $lock_time = return $lock_value; } + + /** + * Verifies the lock against the cached lock. + * + * @param string $lock_key The transient key used to lock the group. + * @param string $lock_value The lock value to be verified. + * @param string $cache_group The cache group in which the lock value is stored. Default is an empty string. + * + * @return bool Whether the lock value matches the cached lock value in the cache group. + */ + function lock_verify( string $lock_key, string $lock_value, string $cache_group = '' ) : bool { + $found = null; + $cached_lock = wp_cache_get( $lock_key, $cache_group, false, $found ); + + return $found && $cached_lock === $lock_value; + } + + /** + * Registers a cache group for flushing. + * + * @param string $cache_group The cache group to be registered. + * + * @return bool Whether the cache group was successfully registered. + */ + public function register_cache_group( string $cache_group ) : bool { + global $wp_object_cache; + if ( function_exists( 'wp_cache_add_redis_hash_groups' ) ) { + // Enable cache group flushing for this group + wp_cache_add_redis_hash_groups( $cache_group ); + + return $wp_object_cache && isset( $wp_object_cache->redis_hash_groups[ $cache_group ] ); + } + + return false; + } } diff --git a/inc/class-storageprovider.php b/inc/class-storageprovider.php index 71e306c..dec2b65 100644 --- a/inc/class-storageprovider.php +++ b/inc/class-storageprovider.php @@ -1,16 +1,38 @@ registered_cache_groups[ $cache_group ] ) ) { + return false; + } + + $cache_group = $this->dashit( $cache_group ); + $affected = 0; + // Deleting all options and transients with shared group name + $affected += (int) $wpdb->query( + $wpdb->prepare( "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s", "$cache_group%" ) + ); + $affected += (int) $wpdb->query( + $wpdb->prepare( "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s", "_transient_$cache_group%" ) + ); + return $affected > 0; + } + + /** + * Registers a cache group for flushing. + * + * @param string $cache_group The cache group to be registered. + * + * @return bool Whether the cache group was successfully registered. + */ + public function register_cache_group( string $cache_group ) : bool { + $this->registered_cache_groups[ $cache_group ] = $cache_group; + return true; } /** @@ -78,6 +108,22 @@ public function lock_add( string $lock_key, string $cache_group = '', int $lock_ return $lock_value; } + /** + * Verifies the lock against the cached lock. + * + * @param string $lock_key The transient key used to lock the group. + * @param string $lock_value The lock value to be verified. + * @param string $cache_group The cache group in which the lock value is stored. Default is an empty string. + * + * @return bool Whether the lock value matches the cached lock value in the cache group. + */ + function lock_verify( string $lock_key, string $lock_value, string $cache_group = '' ) : bool { + $cached_lock = get_transient( $this->dashit( $cache_group ) . $lock_key ); + $found = $cached_lock !== false; + + return $found && $cached_lock === $lock_value; + } + /** * Adds a transient value to the cache. * diff --git a/inc/namespace.php b/inc/namespace.php index 2fb93eb..094bab4 100644 --- a/inc/namespace.php +++ b/inc/namespace.php @@ -23,11 +23,6 @@ use RuntimeException; const CRON_ACTION = 'hm.swrCache.cron'; -const ALLOWED_CACHE_STORAGE = [ StorageProvider::CACHE, StorageProvider::OPTION ]; - -/** @var StorageProvider $storage */ -$storage = null; - /** * Bootstrapping. * @@ -35,10 +30,9 @@ */ function bootstrap() : void { global $storage; - add_action( CRON_ACTION, __NAMESPACE__ . '\\do_cron', 10, 7 ); - $storage_type = apply_filters( 'hm_swrcache_storagetype', StorageProvider::CACHE ); - $storage = StorageProvider::getInstance( $storage_type ); + add_action( CRON_ACTION, __NAMESPACE__ . '\\do_cron', 10, 7 ); + $storage = StorageProvider::get_instance( StorageProvider::CACHE ); } /** @@ -54,33 +48,6 @@ function cache_delete_group( string $cache_group ) : bool { return $storage->delete_group( $cache_group ); } -/** - * Retrieves a cached form with its expiry time. - * - * @param string $cache_key The key of the cache to retrieve. - * @param string $cache_group Optional. The cache group. Default is empty string. - * - * @return array An array containing the cached contents (or false) and its expiry timestamp. - */ -function cache_get_with_expiry( string $cache_key, string $cache_group = '' ) : array { - global $storage; - - return $storage->get_with_expiry( $cache_key, $cache_group ); -} - -/** - * Set data in cache with expiry and delete the lock transient. - * - * @param string $lock_key The name of the lock. - * @param mixed $data The data to be cached. - * @param int $cache_duration The expiry time for the cache in seconds. - * @param string $cache_key The key for the cache. - * @param string $cache_group Optional. The group for the cache. Default value is empty string. - */ -function cache_set_with_expiry( string $lock_key, mixed $data, int $cache_duration, string $cache_key, string $cache_group = '' ) : void { - global $storage; - $storage->set_with_expiry( $lock_key, $data, $cache_duration, $cache_key, $cache_group ); -} /** * Check if the cache is warm. @@ -109,17 +76,15 @@ function cache_is_warm( mixed $data, int $expiry_time ) : bool { * @throws InvalidArgumentException If a closure is provided as a callback. * @throws RuntimeException If an error occurs during the execution of the callback function. */ -function do_cron( string $lock_value, callable $callback, array $callback_args, int $expiry_duration, string $cache_key, string $cache_group = '', $cache_storage = 'cache' ) : void { +function do_cron( string $lock_value, callable $callback, array $callback_args, int $expiry_duration, string $cache_key, string $cache_group = '') : void { + global $storage; + if ( $callback instanceof Closure ) { throw new InvalidArgumentException( 'Closures are not allowed as callbacks.' ); } - if ( ! in_array( $cache_storage, ALLOWED_CACHE_STORAGE, true ) ) { - throw new InvalidArgumentException( sprintf( 'Cache storage type not valid. Expected one of %s', implode( ', ', ALLOWED_CACHE_STORAGE ) ) ); - } - $lock_key = "lock_$cache_key"; - if ( ! lock_verify( $lock_key, $lock_value, $cache_group ) ) { + if ( ! $storage->lock_verify( $lock_key, $lock_value, $cache_group ) ) { // Another invocation already reserved this cron job. return; } @@ -129,7 +94,7 @@ function do_cron( string $lock_value, callable $callback, array $callback_args, throw new RuntimeException( $data->get_error_message(), $data->get_error_code() ); } - cache_set_with_expiry( $lock_key, $data, $expiry_duration, $cache_key, $cache_group ); + $storage->set_with_expiry( $lock_key, $data, $expiry_duration, $cache_key, $cache_group ); } /** @@ -140,22 +105,19 @@ function do_cron( string $lock_value, callable $callback, array $callback_args, * @param callable $callback The callback function to fetch the data if not available in cache. * @param array $callback_args The arguments to pass to the callback function. * @param int $cache_duration The duration of fresh cache content in seconds. - * @param string $cache_storage Where cache data is stored. Allowed values are 'cache' or 'options'. * * @return mixed The cached data if available, or false if the data is not available in cache yet. * * @throws InvalidArgumentException If a closure is provided as a callback. */ -function get( string $cache_key, string $cache_group, callable $callback, array $callback_args, int $cache_duration, string $cache_storage = 'cache' ) : mixed { +function get( string $cache_key, string $cache_group, callable $callback, array $callback_args, int $cache_duration ) : mixed { + global $storage; + if ( $callback instanceof Closure ) { throw new InvalidArgumentException( 'Closures are not allowed as callbacks.' ); } - if ( ! in_array( $cache_storage, ALLOWED_CACHE_STORAGE, true ) ) { - throw new InvalidArgumentException( sprintf( 'Cache storage type not valid. Expected one of %s. %s given.', implode( ', ', ALLOWED_CACHE_STORAGE ), $cache_storage ) ); - } - - [ $data, $expiry_timestamp ] = cache_get_with_expiry( $cache_key, $cache_group ); + [ $data, $expiry_timestamp ] = $storage->get_with_expiry( $cache_key, $cache_group ); if ( cache_is_warm( $data, $expiry_timestamp ) ) { // Cache is warm @@ -163,7 +125,7 @@ function get( string $cache_key, string $cache_group, callable $callback, array } wp_schedule_single_event( time(), CRON_ACTION, [ - lock_add( "lock_$cache_key", $cache_group ), + $storage->lock_add( "lock_$cache_key", $cache_group ), $callback, $callback_args, $cache_duration, @@ -175,38 +137,6 @@ function get( string $cache_key, string $cache_group, callable $callback, array return $data; } -/** - * Adds a lock key in the cache to avoid race conditions and enables synchronization between processes. - * It's only stored if the lock doesn't exist (or is expired). - * - * @param string $lock_key The unique key for the lock. - * @param string $cache_group The cache group where the lock key will be stored - * @param int $lock_time The duration in seconds for which the lock will be held. Default is MINUTE_IN_SECONDS constant. - * - * @return string The generated lock value, unique per invocation, regardless whether the lock is added. - */ -function lock_add( string $lock_key, string $cache_group = '', int $lock_time = MINUTE_IN_SECONDS ) : string { - global $storage; - - return $storage->lock_add( $lock_key, $cache_group, $lock_time ); -} - -/** - * Verifies the lock against the cached lock. - * - * @param string $lock_key The transient key used to lock the group. - * @param string $lock_value The lock value to be verified. - * @param string $cache_group The cache group in which the lock value is stored. Default is an empty string. - * - * @return bool Whether the lock value matches the cached lock value in the cache group. - */ -function lock_verify( string $lock_key, string $lock_value, string $cache_group = '' ) : bool { - $found = null; - $cached_lock = wp_cache_get( $lock_key, $cache_group, false, $found ); - - return $found && $cached_lock === $lock_value; -} - /** * Registers a cache group for flushing. * @@ -214,14 +144,8 @@ function lock_verify( string $lock_key, string $lock_value, string $cache_group * * @return bool Whether the cache group was successfully registered. */ -function register_cache_group( string $cache_group ) { - global $wp_object_cache; - if ( function_exists( 'wp_cache_add_redis_hash_groups' ) ) { - // Enable cache group flushing for this group - wp_cache_add_redis_hash_groups( $cache_group ); - - return $wp_object_cache && isset( $wp_object_cache->redis_hash_groups[ $cache_group ] ); - } +function register_cache_group( string $cache_group ) : bool { + global $storage; - return false; + return $storage->register_cache_group( $cache_group ); } diff --git a/plugin.php b/plugin.php index 60d729e..9c65edb 100644 --- a/plugin.php +++ b/plugin.php @@ -7,7 +7,7 @@ require_once __DIR__ . '/inc/class-storageprovider.php'; require_once __DIR__ . '/inc/class-cachestorageprovider.php'; -require_once __DIR__ . '/inc/class-optionstorageprovider.php'; +require_once __DIR__ . '/inc/class-transoptionstorageprovider.php'; require_once __DIR__ . '/inc/namespace.php'; bootstrap(); From ee5d491273f8503d75f43a56882c331298fc7ebd Mon Sep 17 00:00:00 2001 From: Sander van Dragt Date: Tue, 25 Jun 2024 16:09:18 +0100 Subject: [PATCH 4/4] Rename "cache_group" to "group" in method parameters This change updates the variable name from "cache_group" to "group" in several function parameters for consistency and simplicity. This change affects the register function in multiple files and helps to streamline the code, making it easier to read and understand. --- inc/class-cachestorageprovider.php | 8 ++++---- inc/class-storageprovider.php | 4 ++-- inc/class-transoptionstorageprovider.php | 10 +++++----- inc/namespace.php | 2 +- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/inc/class-cachestorageprovider.php b/inc/class-cachestorageprovider.php index 9fd1932..526bebd 100644 --- a/inc/class-cachestorageprovider.php +++ b/inc/class-cachestorageprovider.php @@ -91,17 +91,17 @@ function lock_verify( string $lock_key, string $lock_value, string $cache_group /** * Registers a cache group for flushing. * - * @param string $cache_group The cache group to be registered. + * @param string $group The cache group to be registered. * * @return bool Whether the cache group was successfully registered. */ - public function register_cache_group( string $cache_group ) : bool { + public function register_group( string $group ) : bool { global $wp_object_cache; if ( function_exists( 'wp_cache_add_redis_hash_groups' ) ) { // Enable cache group flushing for this group - wp_cache_add_redis_hash_groups( $cache_group ); + wp_cache_add_redis_hash_groups( $group ); - return $wp_object_cache && isset( $wp_object_cache->redis_hash_groups[ $cache_group ] ); + return $wp_object_cache && isset( $wp_object_cache->redis_hash_groups[ $group ] ); } return false; diff --git a/inc/class-storageprovider.php b/inc/class-storageprovider.php index dec2b65..c9a090f 100644 --- a/inc/class-storageprovider.php +++ b/inc/class-storageprovider.php @@ -96,9 +96,9 @@ abstract public function lock_verify( string $lock_key, string $lock_value, stri /** * Registers a cache group for flushing. * - * @param string $cache_group The cache group to be registered. + * @param string $group The cache group to be registered. * * @return bool Whether the cache group was successfully registered. */ - abstract public function register_cache_group( string $cache_group ) : bool; + abstract public function register_group( string $group ) : bool; } diff --git a/inc/class-transoptionstorageprovider.php b/inc/class-transoptionstorageprovider.php index bf1bc0d..2004458 100644 --- a/inc/class-transoptionstorageprovider.php +++ b/inc/class-transoptionstorageprovider.php @@ -7,7 +7,7 @@ */ class TransoptionStorageProvider extends StorageProvider { - private array $registered_cache_groups = []; + private array $registered_groups = []; /** * Deletes a cache group and allows for immediate regeneration. * @@ -21,7 +21,7 @@ public function delete_group( string $cache_group ) : bool { global $wpdb; // cache groups must be registered first. - if ( ! isset( $this->registered_cache_groups[ $cache_group ] ) ) { + if ( ! isset( $this->registered_groups[ $cache_group ] ) ) { return false; } @@ -40,12 +40,12 @@ public function delete_group( string $cache_group ) : bool { /** * Registers a cache group for flushing. * - * @param string $cache_group The cache group to be registered. + * @param string $group The cache group to be registered. * * @return bool Whether the cache group was successfully registered. */ - public function register_cache_group( string $cache_group ) : bool { - $this->registered_cache_groups[ $cache_group ] = $cache_group; + public function register_group( string $group ) : bool { + $this->registered_groups[ $group ] = $group; return true; } diff --git a/inc/namespace.php b/inc/namespace.php index 094bab4..9a606e8 100644 --- a/inc/namespace.php +++ b/inc/namespace.php @@ -147,5 +147,5 @@ function get( string $cache_key, string $cache_group, callable $callback, array function register_cache_group( string $cache_group ) : bool { global $storage; - return $storage->register_cache_group( $cache_group ); + return $storage->register_group( $cache_group ); }