zerkalica/semaphore

此库提供了信号量获取和释放的API

v1.1.1 2017-06-13 14:54 UTC

This package is not auto-updated.

Last update: 2024-09-19 01:27:07 UTC


README

此库提供了信号量获取和释放的API。

支持适配器:flock、pdo、doctrine/orm、apc、memcached、sem

用法

$adapter   = new \Millwright\Semaphore\Adapter\ApcAdapter;
$semaphore = new \Millwright\Semaphore\Model\SemaphoreManager($adapter);

$ttl = 60; // Time in seconds, used, if script dies and release never called.

$handle = $semaphore->acquire('lock key', $ttl);

// Do something thread-safe

$semaphore->release($handle);

SQL表结构

CREATE TABLE `semaphore__semaphore` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `expire_date` datetime NOT NULL,
  `semaphore_key` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `semaphore_key_idx` (`semaphore_key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

用于doctrine适配器的示例模型。Doctrine适配器直接与sql-table交互。此模型仅创建适当的表。

/**
 * Semaphore entity
 *
 * @ORM\Entity()
 *
 * @ORM\Table(name="semaphore__semaphore",
 *     uniqueConstraints={@ORM\UniqueConstraint(name="semaphore_key_idx", columns={"semaphore_key"})}
 * )
 */
class Semaphore
{
    /**
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(name="expire_date", type="datetime", nullable=false)
     */
    protected $date;

    /**
     * @ORM\Column(name="semaphore_key", type="string", nullable=false)
     */
    protected $key;
}