<?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($aVT3z, $qQcPQ, $TmEg4 = 3600) { if (RedisUtil::isEnable()) { goto LbvzG; ktrN_: RedisUtil::set($ApvS1, $qQcPQ); goto AMZSH; LbvzG: $ApvS1 = "Atomic:{$aVT3z}"; goto ktrN_; AMZSH: RedisUtil::expire($ApvS1, $TmEg4); goto Wkl1x; Wkl1x: } else { if (ModelUtil::exists('atomic', array('name' => $aVT3z))) { ModelUtil::update('atomic', array('name' => $aVT3z), array('value' => $qQcPQ, 'expire' => time() + $TmEg4)); } else { ModelUtil::insert('atomic', array('name' => $aVT3z, 'value' => $qQcPQ, 'expire' => time() + $TmEg4)); } self::autoCleanDB(); } } public static function consume($aVT3z) { if (RedisUtil::isEnable()) { goto iuSrm; YbQNt: if (RedisUtil::decr($ApvS1) >= 0) { return true; } goto wHAU1; wHAU1: return false; goto LEaFG; iuSrm: $ApvS1 = "Atomic:{$aVT3z}"; goto YbQNt; LEaFG: } else { goto NiBJE; p8Xl1: $jXe_5 = ModelUtil::getWithLock('atomic', array('name' => $aVT3z)); goto AHNgE; RhcR9: if ($jXe_5['expire'] < time() || $jXe_5['value'] < 0) { goto rnn2m; IxFYT: return false; goto ROjqF; rnn2m: ModelUtil::delete('atomic', array('name' => $aVT3z)); goto cD25F; cD25F: ModelUtil::transactionCommit(); goto IxFYT; ROjqF: } goto Ogc2k; U5Hm3: return true; goto qcpBJ; NiBJE: self::autoCleanDB(); goto QYYwk; AHNgE: if (empty($jXe_5)) { ModelUtil::transactionCommit(); return false; } goto RhcR9; W687K: ModelUtil::transactionCommit(); goto U5Hm3; QYYwk: ModelUtil::transactionBegin(); goto p8Xl1; Ogc2k: ModelUtil::update('atomic', array('name' => $aVT3z), array('value' => $jXe_5['value'] - 1)); goto W687K; qcpBJ: } } public static function remove($aVT3z) { if (RedisUtil::isEnable()) { $ApvS1 = "Atomic:{$aVT3z}"; RedisUtil::delete($ApvS1); } else { ModelUtil::delete('atomic', array('name' => $aVT3z)); } } public static function acquire($aVT3z, $TmEg4 = 30) { if (RedisUtil::isEnable()) { goto P0lyv; V3fta: return false; goto e20A1; Zzgaj: $M3Qja = RedisUtil::get($OSU2M); goto lMSCO; lMSCO: if ($M3Qja < time()) { RedisUtil::delete($OSU2M); return self::acquire($aVT3z, $TmEg4); } goto V3fta; cRzf3: if (RedisUtil::setnx($OSU2M, time() + $TmEg4)) { RedisUtil::expire($OSU2M, $TmEg4); return true; } goto Zzgaj; P0lyv: $OSU2M = "Atomic:{$aVT3z}"; goto cRzf3; e20A1: } else { goto RiDxo; E65Hg: if ($jXe_5['expire'] < time()) { goto Prnjl; xHWK2: return true; goto IKapt; wKpux: ModelUtil::transactionCommit(); goto xHWK2; Prnjl: ModelUtil::update('atomic', array('name' => $aVT3z), array('value' => 1, 'expire' => $M3Qja)); goto wKpux; IKapt: } goto WL21f; dq30Z: ModelUtil::transactionBegin(); goto teiE5; RiDxo: self::autoCleanDB(); goto dq30Z; teiE5: $jXe_5 = ModelUtil::getWithLock('atomic', array('name' => $aVT3z)); goto NlWal; WL21f: ModelUtil::transactionCommit(); goto XyE08; ljJKR: if (empty($jXe_5)) { goto rFoUM; rFoUM: ModelUtil::insert('atomic', array('name' => $aVT3z, 'value' => 1, 'expire' => $M3Qja)); goto ZgcLA; ZgcLA: ModelUtil::transactionCommit(); goto aHX_w; aHX_w: return true; goto loJ_4; loJ_4: } goto E65Hg; NlWal: $M3Qja = time() + $TmEg4; goto ljJKR; XyE08: return false; goto T4LSA; T4LSA: } } public static function release($aVT3z) { if (RedisUtil::isEnable()) { $OSU2M = "Atomic:{$aVT3z}"; RedisUtil::delete($OSU2M); } else { ModelUtil::delete('atomic', array('name' => $aVT3z)); } } }