diff --git a/includes/class-tmdo-installer.php b/includes/class-tmdo-installer.php index 12353b7..fd0274d 100644 --- a/includes/class-tmdo-installer.php +++ b/includes/class-tmdo-installer.php @@ -1327,12 +1327,12 @@ class TMDO_Installer { $sqls[] = "CREATE TABLE {$p}wpdo_warm ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, post_id bigint(20) unsigned NOT NULL DEFAULT 0, - meta_key varchar(255) NOT NULL DEFAULT '', + meta_key varchar(191) NOT NULL DEFAULT '', meta_value longtext, expires_at datetime NULL DEFAULT NULL, created_at datetime NOT NULL DEFAULT '0000-00-00 00:00:00', PRIMARY KEY (id), - KEY idx_post_meta (post_id, meta_key(191)), + UNIQUE KEY ui_post_meta (post_id, meta_key), KEY idx_expires_at (expires_at) ) {$charset};"; diff --git a/includes/zones/class-tmdo-zone-warm.php b/includes/zones/class-tmdo-zone-warm.php index 2599f1c..7cbfbd8 100644 --- a/includes/zones/class-tmdo-zone-warm.php +++ b/includes/zones/class-tmdo-zone-warm.php @@ -100,46 +100,58 @@ class TMDO_Zone_Warm { * @param int|null $ttl TTL in seconds. Null = no expiry. */ public static function set( int $post_id, string $meta_key, string $value, ?int $ttl = null ): void { - global $wpdb; $table = self::table(); $now = TMDO_DB::now(); - $expires_at = null; - if ( $ttl && $ttl > 0 ) { - $expires_at = gmdate( 'Y-m-d H:i:s', time() + $ttl ); - } + $expires_at = ( $ttl && $ttl > 0 ) ? gmdate( 'Y-m-d H:i:s', time() + $ttl ) : null; - // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name from TMDO_Zone_Warm::table() via TMDO_DB::table(). - $existing = $wpdb->get_var( + TMDO_DB::upsert( + $table, + array( + 'post_id' => $post_id, + 'meta_key' => $meta_key, + 'meta_value' => $value, + 'expires_at' => $expires_at, + 'created_at' => $now, + ), + array( 'meta_value', 'expires_at' ), + array( 'post_id', 'meta_key' ), + array( '%d', '%s', '%s', '%s', '%s' ) + ); + } + + /** + * Atomically increment an integer counter in the warm zone. + * + * Uses INSERT ... ON DUPLICATE KEY UPDATE to avoid the read-then-write race + * condition present in get()+set() patterns. + * + * @param int $post_id Post ID. + * @param string $meta_key Counter key. + * @param int $by Amount to increment (default 1). + * @param int|null $ttl TTL in seconds. Null = no expiry. + */ + public static function increment( int $post_id, string $meta_key, int $by = 1, ?int $ttl = null ): void { + global $wpdb; + $table = self::table(); + $now = TMDO_DB::now(); + $expires_at = ( $ttl && $ttl > 0 ) ? gmdate( 'Y-m-d H:i:s', time() + $ttl ) : null; + + // phpcs:disable WordPress.DB.DirectDatabaseQuery,WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- table from self::table() + $wpdb->query( $wpdb->prepare( - "SELECT id FROM `{$table}` WHERE post_id = %d AND meta_key = %s LIMIT 1", + "INSERT INTO `{$table}` (post_id, meta_key, meta_value, expires_at, created_at) + VALUES (%d, %s, %d, %s, %s) + ON DUPLICATE KEY UPDATE meta_value = CAST(COALESCE(meta_value, 0) AS SIGNED) + %d", $post_id, - $meta_key + $meta_key, + $by, + $expires_at, + $now, + $by ) ); - // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared - - if ( $existing ) { - $update_data = array( 'meta_value' => $value ); - $update_format = array( '%s' ); - if ( null !== $expires_at ) { - $update_data['expires_at'] = $expires_at; - $update_format[] = '%s'; - } - $wpdb->update( $table, $update_data, array( 'id' => (int) $existing ), $update_format, array( '%d' ) ); - } else { - $wpdb->insert( - $table, - array( - 'post_id' => $post_id, - 'meta_key' => $meta_key, - 'meta_value' => $value, - 'expires_at' => $expires_at, - 'created_at' => $now, - ), - array( '%d', '%s', '%s', $expires_at ? '%s' : null, '%s' ) - ); - } + // phpcs:enable WordPress.DB.DirectDatabaseQuery,WordPress.DB.PreparedSQL.InterpolatedNotPrepared } /** diff --git a/tests/integration/ZoneWarmIntegrationTest.php b/tests/integration/ZoneWarmIntegrationTest.php index 730ad81..d0b19d0 100644 --- a/tests/integration/ZoneWarmIntegrationTest.php +++ b/tests/integration/ZoneWarmIntegrationTest.php @@ -28,7 +28,7 @@ class ZoneWarmIntegrationTest extends TestCase { `expires_at` datetime DEFAULT NULL, `created_at` datetime NOT NULL DEFAULT \'0000-00-00 00:00:00\', PRIMARY KEY (`id`), - KEY `post_id` (`post_id`), + UNIQUE KEY `ui_post_meta` (`post_id`, `meta_key`), KEY `expires_at` (`expires_at`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4' ); diff --git a/tests/unit/ZoneWarmTest.php b/tests/unit/ZoneWarmTest.php index 552be78..4ce6361 100644 --- a/tests/unit/ZoneWarmTest.php +++ b/tests/unit/ZoneWarmTest.php @@ -102,6 +102,40 @@ class ZoneWarmTest extends TestCase { } public function query( string $sql ): int|bool { + $store = &ZoneWarmTest::$store; + $flat = preg_replace( '/\s+/', ' ', $sql ); + + // INSERT ... ON DUPLICATE KEY UPDATE (from set() via TMDO_DB::upsert()) + // Pattern: VALUES (post_id, 'meta_key', 'value', expires_or_NULL, 'now') ON DUPLICATE KEY + if ( stripos( $flat, 'ON DUPLICATE KEY' ) !== false && stripos( $flat, 'COALESCE' ) === false ) { + if ( preg_match( "/VALUES \((\d+), '([^']+)', '([^']*)', (NULL|'[^']+'), '[^']+'\)/i", $flat, $m ) ) { + $post_id = (int) $m[1]; + $meta_key = $m[2]; + $meta_value = $m[3]; + $expires_at = 'NULL' === $m[4] ? null : strtotime( trim( $m[4], "'" ) ); + $store[ $post_id ][ $meta_key ] = array( + 'value' => $meta_value, + 'expires_at' => $expires_at, + ); + return 1; + } + } + + // INSERT ... ON DUPLICATE KEY UPDATE meta_value = CAST(COALESCE...) + N (from increment()) + if ( stripos( $flat, 'COALESCE' ) !== false ) { + if ( preg_match( "/VALUES \((\d+), '([^']+)', (\d+),/i", $flat, $m ) ) { + $post_id = (int) $m[1]; + $meta_key = $m[2]; + $by = (int) $m[3]; + $current = (int) ( $store[ $post_id ][ $meta_key ]['value'] ?? 0 ); + $store[ $post_id ][ $meta_key ] = array( + 'value' => (string) ( $current + $by ), + 'expires_at' => null, + ); + return 1; + } + } + return 0; } };