lockKey = BASE . $key . '_lock'; $this->timeout = $timeout; } public function withLock(callable $callback): void { if (!$this->acquire()) return; try { $callback(); } finally { $this->unlock(); } } private function acquire(): bool { $this->token = bin2hex(random_bytes(8)); return (bool) wp_cache_add($this->lockKey, $this->token, 'locks', $this->timeout); } public function unlock(): void { $current = wp_cache_get($this->lockKey, 'locks'); if ($current === $this->token) { wp_cache_delete($this->lockKey, 'locks'); } $this->token = null; } }