db->get_row($this->db->prepare('SELECT request_hash, subject, subject_id FROM %i WHERE idempotency_key = %s', Schema::idempotencyTable(), $key), ARRAY_A); if (!$row) { return null; } return ['request_hash' => (string) $row['request_hash'], 'subject' => (string) $row['subject'], 'subject_id' => (int) $row['subject_id']]; } public function remember(string $key, string $requestHash, string $subject, int $subjectId): void { $suppress = $this->db->suppress_errors(true); $ok = $this->db->insert(Schema::idempotencyTable(), [ 'idempotency_key' => $key, 'request_hash' => $requestHash, 'subject' => $subject, 'subject_id' => $subjectId, 'created_at' => EventWriter::now(), ]); $this->db->suppress_errors($suppress); if (false === $ok) { if (str_contains($this->db->last_error, 'Duplicate entry')) { // Two requests with the same key at the same time: this one loses. throw DomainError::idempotencyMismatch(); } throw new \RuntimeException('Could not store the idempotency key: ' . esc_html($this->db->last_error)); } } }