<?php
/**
 * ------------------------ 
 *  版权所有  www.tecmz.com
 *  商业版本请购买正版授权使用
 * ------------------------
*/ namespace Module\Vendor\Atomic; use ModStart\Core\Dao\ModelUtil; use ModStart\Core\Util\RandomUtil; use ModStart\Core\Util\RedisUtil; class AtomicUtil { private static function autoCleanDB() { if (RandomUtil::percent(20)) { ModelUtil::model('atomic')->where('expire', '<', time())->delete(); } } public static function produce($fYoEH, $DiI0O, $F6pCL = 3600) { if (RedisUtil::isEnable()) { goto wAX7D; OimBS: RedisUtil::set($GlLsx, $DiI0O); goto FGrp7; FGrp7: RedisUtil::expire($GlLsx, $F6pCL); goto yQ4l4; wAX7D: $GlLsx = "Atomic:{$fYoEH}"; goto OimBS; yQ4l4: } else { if (ModelUtil::exists('atomic', array('name' => $fYoEH))) { ModelUtil::update('atomic', array('name' => $fYoEH), array('value' => $DiI0O, 'expire' => time() + $F6pCL)); } else { ModelUtil::insert('atomic', array('name' => $fYoEH, 'value' => $DiI0O, 'expire' => time() + $F6pCL)); } self::autoCleanDB(); } } public static function consume($fYoEH) { if (RedisUtil::isEnable()) { goto wnxB2; rlsul: if (RedisUtil::decr($GlLsx) >= 0) { return true; } goto eqJOR; eqJOR: return false; goto T_EQq; wnxB2: $GlLsx = "Atomic:{$fYoEH}"; goto rlsul; T_EQq: } else { goto wTA1L; ybXrD: ModelUtil::transactionCommit(); goto v05gz; v05gz: return true; goto Tpcwj; wTA1L: self::autoCleanDB(); goto geegB; ZlJam: if (empty($JzI02)) { ModelUtil::transactionCommit(); return false; } goto GOhMv; fdMIc: ModelUtil::update('atomic', array('name' => $fYoEH), array('value' => $JzI02['value'] - 1)); goto ybXrD; geegB: ModelUtil::transactionBegin(); goto vhhVn; GOhMv: if ($JzI02['expire'] < time() || $JzI02['value'] < 0) { goto dazA_; gHFAG: return false; goto eq6tL; kHwdk: ModelUtil::transactionCommit(); goto gHFAG; dazA_: ModelUtil::delete('atomic', array('name' => $fYoEH)); goto kHwdk; eq6tL: } goto fdMIc; vhhVn: $JzI02 = ModelUtil::getWithLock('atomic', array('name' => $fYoEH)); goto ZlJam; Tpcwj: } } public static function remove($fYoEH) { if (RedisUtil::isEnable()) { $GlLsx = "Atomic:{$fYoEH}"; RedisUtil::delete($GlLsx); } else { ModelUtil::delete('atomic', array('name' => $fYoEH)); } } public static function acquire($fYoEH, $F6pCL = 30) { if (RedisUtil::isEnable()) { goto vRa3y; vRa3y: $PeAfP = "Atomic:{$fYoEH}"; goto S_Nie; S_Nie: if (RedisUtil::setnx($PeAfP, time() + $F6pCL)) { RedisUtil::expire($PeAfP, $F6pCL); return true; } goto fOxCt; OpES4: if ($gnk4E < time()) { RedisUtil::delete($PeAfP); return self::acquire($fYoEH, $F6pCL); } goto jKBaI; fOxCt: $gnk4E = RedisUtil::get($PeAfP); goto OpES4; jKBaI: return false; goto Zz56D; Zz56D: } else { goto DfnAp; pDEMF: if ($JzI02['expire'] < time()) { goto FdquT; dsxA_: ModelUtil::transactionCommit(); goto dHCsL; dHCsL: return true; goto j4gCB; FdquT: ModelUtil::update('atomic', array('name' => $fYoEH), array('value' => 1, 'expire' => $gnk4E)); goto dsxA_; j4gCB: } goto MUwuC; q3xtF: if (empty($JzI02)) { goto AoKhP; AoKhP: ModelUtil::insert('atomic', array('name' => $fYoEH, 'value' => 1, 'expire' => $gnk4E)); goto Ujgvj; Ujgvj: ModelUtil::transactionCommit(); goto f_7vQ; f_7vQ: return true; goto xd60D; xd60D: } goto pDEMF; DfnAp: self::autoCleanDB(); goto S5tP0; rk8w9: $gnk4E = time() + $F6pCL; goto q3xtF; MUwuC: ModelUtil::transactionCommit(); goto lKi1t; S5tP0: ModelUtil::transactionBegin(); goto tsHG_; lKi1t: return false; goto kRVcY; tsHG_: $JzI02 = ModelUtil::getWithLock('atomic', array('name' => $fYoEH)); goto rk8w9; kRVcY: } } public static function release($fYoEH) { if (RedisUtil::isEnable()) { $PeAfP = "Atomic:{$fYoEH}"; RedisUtil::delete($PeAfP); } else { ModelUtil::delete('atomic', array('name' => $fYoEH)); } } }