lockKey = BASE . '_lock_' . $key; $this->ttl = $ttl; } public function acquire(): bool { // Generate unique token for this process $this->token = uniqid(gethostname() . '_', true); // SET NX with TTL - atomic "set if not exists" $result = wp_cache_add($this->lockKey, $this->token, 'locks', $this->ttl); return $result === true; } public function release(): void { if (!$this->token) { return; } // Only release if we own the lock $current = wp_cache_get($this->lockKey, 'locks'); if ($current === $this->token) { wp_cache_delete($this->lockKey, 'locks'); } $this->token = null; } public function isLocked(): bool { return wp_cache_get($this->lockKey, 'locks') !== false; } /** * Execute callback with lock, auto-release after */ public function withLock(callable $callback): mixed { if (!$this->acquire()) { return null; } try { return $callback(); } finally { $this->release(); } } }