<?php
/**
 * ------------------------ 
 *  版权所有  www.tecmz.com
 *  商业版本请购买正版授权使用
 * ------------------------
*/ namespace Module\Vendor\Util; 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 cVPlW; yi396: RedisUtil::set($GlLsx, $DiI0O); goto MLW4g; MLW4g: RedisUtil::expire($GlLsx, $F6pCL); goto juN0E; cVPlW: $GlLsx = "Atomic:{$fYoEH}"; goto yi396; juN0E: } 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 TZIRb; mwIBS: if (RedisUtil::decr($GlLsx) >= 0) { return true; } goto XCRdf; XCRdf: return false; goto I3Zlu; TZIRb: $GlLsx = "Atomic:{$fYoEH}"; goto mwIBS; I3Zlu: } else { goto OJelb; QRLpT: ModelUtil::update('atomic', array('name' => $fYoEH), array('value' => $JzI02['value'] - 1)); goto twyS4; OJelb: self::autoCleanDB(); goto do3t4; do3t4: ModelUtil::transactionBegin(); goto vHwyD; A1i1V: if (empty($JzI02)) { ModelUtil::transactionCommit(); return false; } goto xhLfQ; xhLfQ: if ($JzI02['expire'] < time() || $JzI02['value'] < 0) { goto VglLy; xfZEg: ModelUtil::transactionCommit(); goto TL9ho; VglLy: ModelUtil::delete('atomic', array('name' => $fYoEH)); goto xfZEg; TL9ho: return false; goto YVujU; YVujU: } goto QRLpT; twyS4: ModelUtil::transactionCommit(); goto fHrQV; fHrQV: return true; goto WoLV_; vHwyD: $JzI02 = ModelUtil::getWithLock('atomic', array('name' => $fYoEH)); goto A1i1V; WoLV_: } } 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 yV2Ek; zWrS9: return false; goto EapwA; r5Iti: $gnk4E = RedisUtil::get($PeAfP); goto niVTx; yV2Ek: $PeAfP = "Atomic:{$fYoEH}"; goto sabE8; niVTx: if ($gnk4E < time()) { RedisUtil::delete($PeAfP); return self::acquire($fYoEH, $F6pCL); } goto zWrS9; sabE8: if (RedisUtil::setnx($PeAfP, time() + $F6pCL)) { RedisUtil::expire($PeAfP, $F6pCL); return true; } goto r5Iti; EapwA: } else { goto Sylw9; N6T1N: ModelUtil::transactionBegin(); goto NdElK; hCylK: return false; goto vVo0s; ClP4T: if ($JzI02['expire'] < time()) { goto goAZu; goAZu: ModelUtil::update('atomic', array('name' => $fYoEH), array('value' => 1, 'expire' => $gnk4E)); goto Czq_Q; Czq_Q: ModelUtil::transactionCommit(); goto qBpuA; qBpuA: return true; goto JevT7; JevT7: } goto cdTaQ; w1mZ7: if (empty($JzI02)) { goto Ol8lF; ciL9m: return true; goto Tq_YX; Ol8lF: ModelUtil::insert('atomic', array('name' => $fYoEH, 'value' => 1, 'expire' => $gnk4E)); goto m9Rwc; m9Rwc: ModelUtil::transactionCommit(); goto ciL9m; Tq_YX: } goto ClP4T; NdElK: $JzI02 = ModelUtil::getWithLock('atomic', array('name' => $fYoEH)); goto ZXMl0; ZXMl0: $gnk4E = time() + $F6pCL; goto w1mZ7; cdTaQ: ModelUtil::transactionCommit(); goto hCylK; Sylw9: self::autoCleanDB(); goto N6T1N; vVo0s: } } public static function release($fYoEH) { if (RedisUtil::isEnable()) { $PeAfP = "Atomic:{$fYoEH}"; RedisUtil::delete($PeAfP); } else { ModelUtil::delete('atomic', array('name' => $fYoEH)); } } }