fix(zone-warm): warm 表 UNIQUE KEY + set() 改 upsert + 原子 increment(B1/B3)
- CREATE TABLE wpdo_warm 的 meta_key varchar(255)→varchar(191), KEY idx_post_meta → UNIQUE KEY ui_post_meta:沒有這個唯一鍵, ON DUPLICATE KEY UPDATE 不會觸發,會不斷 INSERT 重複列。 (既有站台的 ALTER 遷移原本就在 installer:1329,只有 CREATE 落後) - Zone_Warm::set() 由 SELECT→update/insert 改為 TMDO_DB::upsert() - 新增 Zone_Warm::increment():CAST(COALESCE(meta_value,0) AS SIGNED)+N 原子計數,取代 get()+set() 的 read-modify-write race(A v3.3.3) - ZoneWarmTest stub 與 ZoneWarmIntegrationTest 建表同步適配 unit 379 / integration 398 GREEN Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TbG1keQQ7XBa7qMQY16KCY
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user