PHP code example of beaucal / beaucal-long-throttle

1. Go to this page and download the library: Download beaucal/beaucal-long-throttle 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/ */

    

beaucal / beaucal-long-throttle example snippets


'modules' => [..., 'BeaucalLongThrottle', ...];

// in controller
$throttle = $this->getServiceLocator()->get('BeaucalLongThrottle');

// term?
$term = new DateTimeUnit(2, 'weeks'); // or
$term = new DateTimeEnd(new DateTime('+2 weeks'));

if ($throttle->takeLock('BiWeeklyReport', $term)) {
    // lock is taken atomically, made for 2 weeks: safe to do your work
}
else {
    // locked from before: leave it alone & perhaps try again later
}

/**
 * N.B. May throw \BeaucalLongThrottle\Exception\PhantomLockException, when
 * lock is reported to be set but upon verification step is actually not.
 * This is truly exceptional and shouldn't be just thrown aside.
 */

// copy beaucallongthrottle.global.php to your config/autoload/
$throttle = [
// ...
    'adapter_class' => 'BeaucalLongThrottle\Adapter\DbMultiple', // was Adapter\Db
// ...
]
$regexCounts = [
    /**
     * E.g. You can create 3 'do-stuff' locks before the lock can't be taken.
     * Those not matching here are allowed the usual 1.
     */
    '/^do-stuff$/' => 3
];

// in controller
$throttle = $this->getServiceLocator()->get('BeaucalLongThrottle');
$throttle->takeLock('do-stuff', new DateTimeUnit(1, 'day')); // YES
$throttle->takeLock('do-stuff', new DateTimeUnit(1, 'day')); // YES
$throttle->takeLock('do-stuff', new DateTimeUnit(1, 'day')); // YES
$throttle->takeLock('do-stuff', new DateTimeUnit(1, 'day')); // FALSE
// ...
// A DAY LATER
$throttle->takeLock('do-stuff', new DateTimeUnit(1, 'day')); // YES

$throttle = $this->getServiceLocator()->get('BeaucalLongThrottle');
$handle = $throttle->takeLock('year-end', new DateTimeUnit(1, 'year')); // YES
$throttle->takeLock('year-end', new DateTimeUnit(1, 'year')); // FALSE
if ($whoopsBackingOut) {
    $throttle->clearLock($handle);
}
$throttle->takeLock('year-end', new DateTimeUnit(1, 'year')); // YES


// copy beaucallongthrottle.global.php to your config/autoload/
$throttle = [
// ...
    'adapter_class' => 'BeaucalLongThrottle\Adapter\Apc', // was Adapter\Db
// ...
]

// in controller
$throttle = $this->getServiceLocator()->get('BeaucalLongThrottle');


// alternatively, a shortcut factory that doesn't