<?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($BjHij, $gbkxa, $UHOKU = 3600) { if (RedisUtil::isEnable()) { goto hrvIV; WvEii: RedisUtil::expire($Jd65H, $UHOKU); goto sRXDD; HJwjp: RedisUtil::set($Jd65H, $gbkxa); goto WvEii; hrvIV: $Jd65H = "Atomic:{$BjHij}"; goto HJwjp; sRXDD: } else { if (ModelUtil::exists('atomic', array('name' => $BjHij))) { ModelUtil::update('atomic', array('name' => $BjHij), array('value' => $gbkxa, 'expire' => time() + $UHOKU)); } else { ModelUtil::insert('atomic', array('name' => $BjHij, 'value' => $gbkxa, 'expire' => time() + $UHOKU)); } self::autoCleanDB(); } } public static function consume($BjHij) { if (RedisUtil::isEnable()) { goto LPCs5; LPCs5: $Jd65H = "Atomic:{$BjHij}"; goto JG1bP; Q4S8c: return false; goto aNiuV; JG1bP: if (RedisUtil::decr($Jd65H) >= 0) { return true; } goto Q4S8c; aNiuV: } else { goto Zzh22; LBd_M: ModelUtil::transactionCommit(); goto CBuhi; Zzh22: self::autoCleanDB(); goto sgzp8; vnGM2: if (empty($RpNvI)) { ModelUtil::transactionCommit(); return false; } goto CHgNS; bkxrX: ModelUtil::update('atomic', array('name' => $BjHij), array('value' => $RpNvI['value'] - 1)); goto LBd_M; CBuhi: return true; goto ftzuJ; CHgNS: if ($RpNvI['expire'] < time() || $RpNvI['value'] < 0) { goto mpUXQ; dYzww: ModelUtil::transactionCommit(); goto A4KRD; A4KRD: return false; goto zN_5p; mpUXQ: ModelUtil::delete('atomic', array('name' => $BjHij)); goto dYzww; zN_5p: } goto bkxrX; sgzp8: ModelUtil::transactionBegin(); goto VnOjB; VnOjB: $RpNvI = ModelUtil::getWithLock('atomic', array('name' => $BjHij)); goto vnGM2; ftzuJ: } } public static function remove($BjHij) { if (RedisUtil::isEnable()) { $Jd65H = "Atomic:{$BjHij}"; RedisUtil::delete($Jd65H); } else { ModelUtil::delete('atomic', array('name' => $BjHij)); } } public static function acquire($BjHij, $UHOKU = 30) { if (RedisUtil::isEnable()) { goto aM6B0; R1PbQ: return false; goto mLgxO; EFSg1: $J143u = RedisUtil::get($Jo7ME); goto O5p5M; O5p5M: if ($J143u < time()) { RedisUtil::delete($Jo7ME); return self::acquire($BjHij, $UHOKU); } goto R1PbQ; HjPxa: if (RedisUtil::setnx($Jo7ME, time() + $UHOKU)) { RedisUtil::expire($Jo7ME, $UHOKU); return true; } goto EFSg1; aM6B0: $Jo7ME = "Atomic:{$BjHij}"; goto HjPxa; mLgxO: } else { goto isLGO; EERbp: if ($RpNvI['expire'] < time()) { goto U9_2s; VTuFH: return true; goto xqFzc; U9_2s: ModelUtil::update('atomic', array('name' => $BjHij), array('value' => 1, 'expire' => $J143u)); goto omlVJ; omlVJ: ModelUtil::transactionCommit(); goto VTuFH; xqFzc: } goto jow55; Ab8e8: if (empty($RpNvI)) { goto Zow_y; DobRt: ModelUtil::transactionCommit(); goto rQ5uC; rQ5uC: return true; goto TOKXO; Zow_y: ModelUtil::insert('atomic', array('name' => $BjHij, 'value' => 1, 'expire' => $J143u)); goto DobRt; TOKXO: } goto EERbp; jow55: ModelUtil::transactionCommit(); goto HJ2Xp; w2tXD: $RpNvI = ModelUtil::getWithLock('atomic', array('name' => $BjHij)); goto sXfxP; kx7LA: ModelUtil::transactionBegin(); goto w2tXD; HJ2Xp: return false; goto c0EH4; isLGO: self::autoCleanDB(); goto kx7LA; sXfxP: $J143u = time() + $UHOKU; goto Ab8e8; c0EH4: } } public static function release($BjHij) { if (RedisUtil::isEnable()) { $Jo7ME = "Atomic:{$BjHij}"; RedisUtil::delete($Jo7ME); } else { ModelUtil::delete('atomic', array('name' => $BjHij)); } } }