PHP code example of pedromazala / semaphore

1. Go to this page and download the library: Download pedromazala/semaphore library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

pedromazala / semaphore example snippets


$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);


/**
 * 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;
}