tourze/lock-service-bundle

Lock Service

Installs: 5 041

Dependents: 6

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 0

Type:symfony-bundle

0.1.0 2025-05-11 07:30 UTC

This package is auto-updated.

Last update: 2025-05-11 07:32:40 UTC


README

English | 中文

Latest Version Build Status Quality Score Total Downloads

A flexible distributed lock service bundle for Symfony, supporting Redis Cluster, database, and file-based backends. Ideal for resource mutual exclusion and synchronization in high concurrency scenarios.

Features

  • Supports Redis Cluster, database, and file lock backends
  • SmartLockStore for automatic backend switching
  • Provides read/write locks, blocking locks, and multi-resource locking
  • Seamless integration with Symfony ecosystem
  • Easy to extend and customize

Installation

Requirements

  • PHP >= 8.1
  • Symfony >= 6.4
  • Redis, database, or file backend configured

Composer

composer require tourze/lock-service-bundle

Quick Start

use Tourze\LockServiceBundle\Service\LockService;

$lockService = ... // Get via dependency injection

// Blocking execution
$lockService->blockingRun('resource-key', function () {
    // Logic protected by lock
});

// Multi-resource locking
$lockService->blockingRun(['key1', 'key2'], function () {
    // Logic protected by multiple locks
});

Configuration

Select lock type via APP_LOCK_TYPE environment variable:

  • redis
  • redis-cluster
  • dbal
  • flock

Example:

APP_LOCK_TYPE=redis

Advanced Usage

  • Automatic backend switching via SmartLockStore
  • Retry and wait mechanism for lock acquisition
  • Read/write lock support (see RedisClusterStore)
  • Extensible: implement LockEntity interface for custom lock resources

Entity Design

This bundle provides a LockEntity interface for defining lock resources:

interface LockEntity {
    public function retrieveLockResource(): string;
}

Implement this interface for your business entities to enable fine-grained distributed locking.

Contributing

Please see CONTRIBUTING.md for details. Follow PSR standards and provide tests.

License

MIT License

Changelog

See CHANGELOG.md