beaucal/beaucal-long-throttle

防止某些操作长时间执行。可以是小时、天、月、年。

v0.6.0 2016-03-12 19:08 UTC

This package is not auto-updated.

Last update: 2024-09-18 07:54:40 UTC


README

Build Status

现在100%代码覆盖率。

防止某些时间段的操作。可以是小时、天、月、年,任何时间。允许设置多个锁(例如100/天)和清除/释放新设置的锁。并且它的工作方式就像它应该的那样,每个锁持续的时间正好是你指定的那么长,并且是原子性的。

注意: 这是一种真假节流;此库不执行 sleep();

安装

  1. application.config.php 中添加以下内容
'modules' => [..., 'BeaucalLongThrottle', ...];
  1. 导入数据库 data/beaucal_throttle.sql
CREATE TABLE IF NOT EXISTS `beaucal_throttle` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `key` varchar(255) NOT NULL UNIQUE KEY,
  `end_datetime` DATETIME NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE `beaucal_throttle` ADD INDEX (`end_datetime`);

使用方法

要么你得到锁,要么你得不到。

// 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.
 */

允许多个锁

你可以允许任意数量的锁,例如 'lock1' => 5/hour, 'lock2' => 100/day。以下是方法

// 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

使用 APC 锁定

对于更快速简单的解决方案,使用 APC 锁定。这对于短期节流是足够的,但请注意有关 APC 持久性的常规警告(例如,应用程序的某个其他部分可能会清除整个缓存,PHP 重启,内存不足)。

注意:如果 takeLock() 失败,不要尝试 sleep() 它;由于某种原因,这不会工作,这与 apc_add() 的工作方式有关。相反,处理无锁条件,然后尝试在下一次请求中再次尝试。

// 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 require config
$throttle = $this->getServiceLocator()->get('BeaucalLongThrottle_APC');