AgentSkillsCN

symfony-7-4-lock

Symfony 7.4 Lock组件参考文档,用于创建与管理锁,为共享资源提供独占访问权限。在处理互斥、分布式锁、锁机制,或进行并发控制时,均可参考该文档。触发器包括:Lock、锁定、互斥、分布式锁、Flock、Redis锁、LockFactory、LockInterface、SharedLockInterface、FlockStore、RedisStore、SemaphoreStore、PostgreSqlStore、CombinedStore、BlockingStoreInterface、Key、锁TTL、过期锁。

SKILL.md
--- frontmatter
name: "symfony-7-4-lock"
description: "Symfony 7.4 Lock component reference for creating and managing locks to provide exclusive access to shared resources. Use when working with mutual exclusion, distributed locks, locking mechanisms, or concurrency control. Triggers on: Lock, locking, mutual exclusion, distributed locks, Flock, Redis lock, LockFactory, LockInterface, SharedLockInterface, FlockStore, RedisStore, SemaphoreStore, PostgreSqlStore, CombinedStore, BlockingStoreInterface, Key, lock TTL, expiring locks."

Symfony 7.4 Lock Component

GitHub: https://github.com/symfony/lock Docs: https://symfony.com/doc/7.4/components/lock.html

Quick Reference

Installation

bash
composer require symfony/lock

Basic Usage

php
use Symfony\Component\Lock\LockFactory;
use Symfony\Component\Lock\Store\SemaphoreStore;

$store = new SemaphoreStore();
$factory = new LockFactory($store);

$lock = $factory->createLock('pdf-creation');

if ($lock->acquire()) {
    // Exclusive access to "pdf-creation" resource
    $lock->release();
}

Blocking Lock

php
$lock = $factory->createLock('pdf-creation');
$lock->acquire(true); // Blocks until acquired

Expiring Lock with TTL

php
$lock = $factory->createLock('pdf-creation', ttl: 30);

if ($lock->acquire()) {
    try {
        while (!$finished) {
            performPartialWork();
            $lock->refresh(); // Reset TTL
        }
    } finally {
        $lock->release();
    }
}

Shared (Readers-Writer) Lock

php
$lock = $factory->createLock('resource');
$lock->acquireRead();       // Read lock (concurrent readers allowed)
$lock->acquire();           // Promote to write lock
$lock->acquireRead();       // Demote back to read lock

Disable Auto-Release

php
$lock = $factory->createLock('resource', 3600, autoRelease: false);

Serializing Locks Across Processes

php
use Symfony\Component\Lock\Key;

$key = new Key('article.'.$article->getId());
$lock = $factory->createLockFromKey($key, 300, autoRelease: false);
$lock->acquire(true);
// Pass $key to another process via message bus

Available Stores

StoreScopeBlockingExpiringSharing
FlockStorelocalyesnoyes
SemaphoreStorelocalyesnono
RedisStoreremoteretryyesyes
MemcachedStoreremoteretryyesno
PdoStoreremoteretryyesno
DoctrineDbalStoreremoteretryyesno
PostgreSqlStoreremoteyesnoyes
DoctrineDbalPostgreSqlStoreremoteyesnoyes
MongoDbStoreremoteretryyesno
ZookeeperStoreremoteretrynono
CombinedStoreremotevariesvariesvaries
DynamoDbStoreremoteretryyesno
InMemoryStorelocal---

Common Store Setup

php
// FlockStore (filesystem)
$store = new FlockStore('/var/stores');

// RedisStore
$redis = new \Redis();
$redis->connect('localhost');
$store = new RedisStore($redis);

// PostgreSqlStore (advisory locks, no table needed)
$store = new PostgreSqlStore('pgsql:host=localhost;dbname=app');

// CombinedStore (high availability)
$store = new CombinedStore($stores, new ConsensusStrategy());

LockInterface Methods

  • acquire(bool $blocking = false): bool - Acquire exclusive lock
  • acquireRead(bool $blocking = false): bool - Acquire shared read lock
  • release(): void - Release the lock
  • isAcquired(): bool - Check if lock is held
  • refresh(float|null $ttl = null): void - Reset TTL
  • getRemainingLifetime(): float|null - Seconds until expiry
  • isExpired(): bool - Check if lock expired

Full Documentation

See references/lock.md for complete store configuration, strategies, serialization details, and reliability considerations.