<?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($BjHij, $gbkxa, $UHOKU = 3600) { if (RedisUtil::isEnable()) { goto geNLp; YvCjA: RedisUtil::set($Jd65H, $gbkxa); goto Uu4Sf; geNLp: $Jd65H = "Atomic:{$BjHij}"; goto YvCjA; Uu4Sf: RedisUtil::expire($Jd65H, $UHOKU); goto nKV1g; nKV1g: } 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 bUpPw; bUpPw: $Jd65H = "Atomic:{$BjHij}"; goto HA4Yb; HA4Yb: if (RedisUtil::decr($Jd65H) >= 0) { return true; } goto qWrgy; qWrgy: return false; goto iiL5y; iiL5y: } else { goto Ga8BP; M035m: if (empty($RpNvI)) { ModelUtil::transactionCommit(); return false; } goto S5hw9; Ga8BP: self::autoCleanDB(); goto U7XuL; ijqdX: ModelUtil::transactionCommit(); goto mtZ2r; S5hw9: if ($RpNvI['expire'] < time() || $RpNvI['value'] < 0) { goto iNu_f; Y4Mks: return false; goto HPI7y; iNu_f: ModelUtil::delete('atomic', array('name' => $BjHij)); goto Q1dxM; Q1dxM: ModelUtil::transactionCommit(); goto Y4Mks; HPI7y: } goto O3QZj; U7XuL: ModelUtil::transactionBegin(); goto wl6YR; mtZ2r: return true; goto SZXGS; wl6YR: $RpNvI = ModelUtil::getWithLock('atomic', array('name' => $BjHij)); goto M035m; O3QZj: ModelUtil::update('atomic', array('name' => $BjHij), array('value' => $RpNvI['value'] - 1)); goto ijqdX; SZXGS: } } 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 EYzme; SpjpH: if ($J143u < time()) { RedisUtil::delete($Jo7ME); return self::acquire($BjHij, $UHOKU); } goto jgPDw; laG1p: if (RedisUtil::setnx($Jo7ME, time() + $UHOKU)) { RedisUtil::expire($Jo7ME, $UHOKU); return true; } goto gBbIR; EYzme: $Jo7ME = "Atomic:{$BjHij}"; goto laG1p; gBbIR: $J143u = RedisUtil::get($Jo7ME); goto SpjpH; jgPDw: return false; goto SEERC; SEERC: } else { goto fkoGl; Szgfq: if (empty($RpNvI)) { goto zxZUG; tmmlw: ModelUtil::transactionCommit(); goto VpXmj; VpXmj: return true; goto TNC6U; zxZUG: ModelUtil::insert('atomic', array('name' => $BjHij, 'value' => 1, 'expire' => $J143u)); goto tmmlw; TNC6U: } goto JEVeT; JEVeT: if ($RpNvI['expire'] < time()) { goto vKZ2Z; O3Asi: return true; goto YhdcP; vKZ2Z: ModelUtil::update('atomic', array('name' => $BjHij), array('value' => 1, 'expire' => $J143u)); goto IuuFz; IuuFz: ModelUtil::transactionCommit(); goto O3Asi; YhdcP: } goto V9BjV; fkoGl: self::autoCleanDB(); goto nEs2q; be9ly: $J143u = time() + $UHOKU; goto Szgfq; V9BjV: ModelUtil::transactionCommit(); goto qqUgp; qqUgp: return false; goto WAL0j; pdhti: $RpNvI = ModelUtil::getWithLock('atomic', array('name' => $BjHij)); goto be9ly; nEs2q: ModelUtil::transactionBegin(); goto pdhti; WAL0j: } } public static function release($BjHij) { if (RedisUtil::isEnable()) { $Jo7ME = "Atomic:{$BjHij}"; RedisUtil::delete($Jo7ME); } else { ModelUtil::delete('atomic', array('name' => $BjHij)); } } }