Generates Distributed Lock for PHP 8.4. Creates LockInterface, LockFactory, RedisLockAdapter with TTL, and database lock adapter. Includes unit tests.
From accnpx claudepluginhub dykyi-roman/awesome-claude-code --plugin accThis skill uses the workspace's default tool permissions.
references/examples.mdreferences/templates.mdCreates distributed locking infrastructure for concurrency control and mutual exclusion.
| Scenario | Example |
|---|---|
| Resource contention | Prevent concurrent writes to same entity |
| Scheduled tasks | Ensure cron job runs on single node |
| Inventory management | Prevent overselling with stock locks |
| Distributed coordination | Leader election, singleton processes |
Path: src/Infrastructure/Lock/
LockInterface.php — Lock contractLockConfig.php — Configuration value objectLockException.php — Lock acquisition exceptionPath: src/Infrastructure/Lock/
RedisLockAdapter.php — Redis SETNX + TTL lock implementationPath: src/Infrastructure/Lock/
LockFactory.php — Creates configured lock instancesLockConfigTest.php — Configuration validation testsRedisLockAdapterTest.php — Lock behavior tests| Component | Path |
|---|---|
| All Classes | src/Infrastructure/Lock/ |
| Unit Tests | tests/Unit/Infrastructure/Lock/ |
| Component | Pattern | Example |
|---|---|---|
| Interface | LockInterface | LockInterface |
| Config VO | LockConfig | LockConfig |
| Redis Adapter | RedisLockAdapter | RedisLockAdapter |
| Factory | LockFactory | LockFactory |
| Exception | LockException | LockException |
| Test | {ClassName}Test | RedisLockAdapterTest |
interface LockInterface
{
public function acquire(string $resource): bool;
public function release(string $resource): void;
public function isAcquired(string $resource): bool;
public function refresh(string $resource): bool;
}
final readonly class LockConfig
{
public function __construct(
public int $ttlSeconds = 30,
public bool $autoRelease = true,
public int $retryCount = 3,
public int $retryDelayMs = 200
) {}
}
final class RedisLockAdapter implements LockInterface
{
public function acquire(string $resource): bool;
public function release(string $resource): void;
public function isAcquired(string $resource): bool;
public function refresh(string $resource): bool;
}
$lock = $lockFactory->create('order-processing');
try {
if (!$lock->acquire('order:12345')) {
throw LockException::cannotAcquire('order:12345');
}
$order->process();
} finally {
$lock->release('order:12345');
}
acquire() ──→ SET resource token NX EX ttl
│
Success? ──→ Lock held (token stored)
│ │
NO refresh() ──→ Extend TTL
│ │
Retry? ──→ Sleep → retry release() ──→ Compare token → DEL
│
LockException
| Anti-pattern | Problem | Solution |
|---|---|---|
| No TTL | Dead locks on crash | Always set TTL |
| DEL without token check | Release others' locks | Use Lua compare-and-delete |
| Spin lock without delay | CPU waste on contention | Use retry delay |
| No auto-release | Lock leak on exception | Use try/finally or auto-release |
| Single Redis node | No fault tolerance | Consider Redlock for HA |
| Infinite retry | Thread stuck forever | Set max retry count |
For complete PHP templates and examples, see:
references/templates.md — LockInterface, LockConfig, RedisLockAdapter, LockFactory templatesreferences/examples.md — Usage examples and testsProvides UI/UX resources: 50+ styles, color palettes, font pairings, guidelines, charts for web/mobile across React, Next.js, Vue, Svelte, Tailwind, React Native, Flutter. Aids planning, building, reviewing interfaces.