<?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($HRrYd, $L58zW, $sQwqe = 3600) { if (RedisUtil::isEnable()) { goto TwBCF; aA_FM: RedisUtil::set($YP7B1, $L58zW); goto N7U8V; N7U8V: RedisUtil::expire($YP7B1, $sQwqe); goto T2ltk; TwBCF: $YP7B1 = "Atomic:{$HRrYd}"; goto aA_FM; T2ltk: } else { if (ModelUtil::exists('atomic', array('name' => $HRrYd))) { ModelUtil::update('atomic', array('name' => $HRrYd), array('value' => $L58zW, 'expire' => time() + $sQwqe)); } else { ModelUtil::insert('atomic', array('name' => $HRrYd, 'value' => $L58zW, 'expire' => time() + $sQwqe)); } self::autoCleanDB(); } } public static function consume($HRrYd) { if (RedisUtil::isEnable()) { goto hJ_TB; tHZaA: if (RedisUtil::decr($YP7B1) >= 0) { return true; } goto tOlXP; tOlXP: return false; goto MUOY4; hJ_TB: $YP7B1 = "Atomic:{$HRrYd}"; goto tHZaA; MUOY4: } else { goto FOJ53; anas8: ModelUtil::transactionCommit(); goto xSAm5; RQEQ1: $KDnRL = ModelUtil::getWithLock('atomic', array('name' => $HRrYd)); goto jKR77; J_AES: if ($KDnRL['expire'] < time() || $KDnRL['value'] < 0) { goto Q6tLV; Q6tLV: ModelUtil::delete('atomic', array('name' => $HRrYd)); goto Ht5kr; Ht5kr: ModelUtil::transactionCommit(); goto sdoRN; sdoRN: return false; goto Z1VPm; Z1VPm: } goto TF7V4; jKR77: if (empty($KDnRL)) { ModelUtil::transactionCommit(); return false; } goto J_AES; xSAm5: return true; goto da2J0; TF7V4: ModelUtil::update('atomic', array('name' => $HRrYd), array('value' => $KDnRL['value'] - 1)); goto anas8; mvKLA: ModelUtil::transactionBegin(); goto RQEQ1; FOJ53: self::autoCleanDB(); goto mvKLA; da2J0: } } public static function remove($HRrYd) { if (RedisUtil::isEnable()) { $YP7B1 = "Atomic:{$HRrYd}"; RedisUtil::delete($YP7B1); } else { ModelUtil::delete('atomic', array('name' => $HRrYd)); } } public static function acquire($HRrYd, $sQwqe = 30) { if (RedisUtil::isEnable()) { goto eXgZ6; eXgZ6: $VKrDJ = "Atomic:{$HRrYd}"; goto OS1eo; lPaFs: if ($bJxke < time()) { RedisUtil::delete($VKrDJ); return self::acquire($HRrYd, $sQwqe); } goto TgFY1; O3rDn: $bJxke = RedisUtil::get($VKrDJ); goto lPaFs; OS1eo: if (RedisUtil::setnx($VKrDJ, time() + $sQwqe)) { RedisUtil::expire($VKrDJ, $sQwqe); return true; } goto O3rDn; TgFY1: return false; goto iKp6q; iKp6q: } else { goto Nh_12; CbTpU: ModelUtil::transactionCommit(); goto A2_mR; jFQ3m: if ($KDnRL['expire'] < time()) { goto bJD1U; qBQPG: return true; goto ZXUzc; kv2jN: ModelUtil::transactionCommit(); goto qBQPG; bJD1U: ModelUtil::update('atomic', array('name' => $HRrYd), array('value' => 1, 'expire' => $bJxke)); goto kv2jN; ZXUzc: } goto CbTpU; nJuwa: $KDnRL = ModelUtil::getWithLock('atomic', array('name' => $HRrYd)); goto LfN8E; BP7a_: ModelUtil::transactionBegin(); goto nJuwa; Nh_12: self::autoCleanDB(); goto BP7a_; A2_mR: return false; goto yCjpS; LfN8E: $bJxke = time() + $sQwqe; goto G2KJQ; G2KJQ: if (empty($KDnRL)) { goto R1_Wx; vKuHh: return true; goto vnmm8; T0RBL: ModelUtil::transactionCommit(); goto vKuHh; R1_Wx: ModelUtil::insert('atomic', array('name' => $HRrYd, 'value' => 1, 'expire' => $bJxke)); goto T0RBL; vnmm8: } goto jFQ3m; yCjpS: } } public static function release($HRrYd) { if (RedisUtil::isEnable()) { $VKrDJ = "Atomic:{$HRrYd}"; RedisUtil::delete($VKrDJ); } else { ModelUtil::delete('atomic', array('name' => $HRrYd)); } } }