illchuk / illchuk-locker
防止某些动作长时间执行。小时、天、月、年。
v0.1
2017-11-24 19:20 UTC
Requires
- php: >=5.6
- zendframework/zend-db: ^2.5.2
- zendframework/zend-math: ^2.5.2 || ^3.0
- zendframework/zend-modulemanager: ^2.5.2
- zendframework/zend-servicemanager: ^3.0
Requires (Dev)
- fzaninotto/faker: ^1.7
- phpunit/dbunit: 2.0.x-dev
- phpunit/phpunit: ~4.0
This package is auto-updated.
Last update: 2024-09-08 09:27:13 UTC
README
现在拥有100%的代码覆盖率。
防止某些时间段内的动作执行。小时、天、月、年,任何时间段。允许多个锁(例如:每天100次)以及清除/释放刚创建的锁。它的工作方式就像它应该做的那样,每个锁的持续时间正好是你指定的那么长,并且是原子性的。
安装
- 在
application.config.php
中,添加如下:
'modules' => [..., 'IllchukLock', ...];
- 将以下数据库导入到您的数据库中
data/illchuk_lock.sql
CREATE TABLE IF NOT EXISTS `illchuk_lock` ( `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 `illchuk_lock` ADD INDEX (`end_datetime`);
使用方法
要么你获得锁,要么你得不到。
// in controller $locker = $this->getServiceLocator()->get('IllchukLock'); // term? $term = new DateTimeUnit(2, 'weeks'); // or $term = new DateTimeEnd(new DateTime('+2 weeks')); if ($locker->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 \IllchukLock\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/小时,'lock2' => 100/天。以下是方法:
// copy IllchukLock.global.php to your config/autoload/ $locker = [ // ... 'adapter_class' => 'IllchukLock\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 $locker = $this->getServiceLocator()->get('IllchukLock'); $locker->takeLock('do-stuff', new DateTimeUnit(1, 'day')); // YES $locker->takeLock('do-stuff', new DateTimeUnit(1, 'day')); // YES $locker->takeLock('do-stuff', new DateTimeUnit(1, 'day')); // YES $locker->takeLock('do-stuff', new DateTimeUnit(1, 'day')); // FALSE // ... // A DAY LATER $locker->takeLock('do-stuff', new DateTimeUnit(1, 'day')); // YES
清除锁
$locker = $this->getServiceLocator()->get('IllchukLock'); $handle = $locker->takeLock('year-end', new DateTimeUnit(1, 'year')); // YES $locker->takeLock('year-end', new DateTimeUnit(1, 'year')); // FALSE if ($whoopsBackingOut) { $locker->clearLock($handle); } $locker->takeLock('year-end', new DateTimeUnit(1, 'year')); // YES
使用APC锁
对于更快速和简略的锁,使用APC锁。这对于短期限的节流是足够的,但请注意APC持久性的警告(例如:您的应用的其他部分可能会清除整个缓存,PHP重启,内存不足)。
注意。如果 takeLock()
失败,不要尝试 sleep()
它,这是出于某种原因(与 apc_add()
的工作方式有关)不会起作用。相反,处理无锁状态,然后在下一个请求中再次尝试。
// copy illchuklock.global.php to your config/autoload/ $locker = [ // ... 'adapter_class' => 'IllchukLock\Adapter\Apc', // was Adapter\Db // ... ] // from service manager $locker = $container->get('IllchukLock'); // alternatively, a shortcut factory that doesn't require config $locker = $container->get('illchuk_lock_apc');