Yoast\WP\SEO\Expiring_Store\Infrastructure
WPDB_Expiring_Store_Repository::insert_if_absent
Inserts a value only if the key does not already exist or has expired.
First removes any expired row for this key, then attempts an INSERT IGNORE. The primary key constraint ensures that only one concurrent caller can win the insert. Both queries use standard SQL compatible with MySQL and SQLite (via the WP SQLite plugin).
Метод класса: WPDB_Expiring_Store_Repository{}
Хуков нет.
Возвращает
bool. True if the value was inserted, false if the key already exists and is not expired.
Использование
$WPDB_Expiring_Store_Repository = new WPDB_Expiring_Store_Repository(); $WPDB_Expiring_Store_Repository->insert_if_absent( $key, $json_value, $expiration_datetime, $current_datetime ): bool;
- $key(строка) (обязательный)
- The key to store.
- $json_value(строка) (обязательный)
- The JSON-encoded value.
- $expiration_datetime(строка) (обязательный)
- The expiration datetime in
'Y-m-d H:i:s'format. - $current_datetime(строка) (обязательный)
- The current datetime in
'Y-m-d H:i:s'format.
Код WPDB_Expiring_Store_Repository::insert_if_absent() WPDB Expiring Store Repository::insert if absent Yoast 28.9
public function insert_if_absent( string $key, string $json_value, string $expiration_datetime, string $current_datetime ): bool {
global $wpdb;
$table = $this->get_table_name();
// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom table with no caching layer.
// Remove the row only if it has expired. Non-expired rows are left untouched.
$wpdb->query(
$wpdb->prepare(
'DELETE FROM %i WHERE `key_name` = %s AND `exp` <= %s',
$table,
$key,
$current_datetime,
),
);
// Attempt to insert. INSERT IGNORE silently skips on duplicate key without triggering wpdb errors.
$wpdb->query(
$wpdb->prepare(
'INSERT IGNORE INTO %i (`key_name`, `value`, `exp`) VALUES (%s, %s, %s)',
$table,
$key,
$json_value,
$expiration_datetime,
),
);
// phpcs:enable
return $wpdb->rows_affected === 1;
}