<?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($MGY9H, $hsART, $Tsb75 = 3600) { if (RedisUtil::isEnable()) { goto b6N0t; wL2dF: RedisUtil::set($b10XM, $hsART); goto fHLxT; fHLxT: RedisUtil::expire($b10XM, $Tsb75); goto cFfuq; b6N0t: $b10XM = "Atomic:{$MGY9H}"; goto wL2dF; cFfuq: } else { if (ModelUtil::exists('atomic', array('name' => $MGY9H))) { ModelUtil::update('atomic', array('name' => $MGY9H), array('value' => $hsART, 'expire' => time() + $Tsb75)); } else { ModelUtil::insert('atomic', array('name' => $MGY9H, 'value' => $hsART, 'expire' => time() + $Tsb75)); } self::autoCleanDB(); } } public static function consume($MGY9H) { if (RedisUtil::isEnable()) { goto E2ytT; P_62L: if (RedisUtil::decr($b10XM) >= 0) { return true; } goto mmAX3; E2ytT: $b10XM = "Atomic:{$MGY9H}"; goto P_62L; mmAX3: return false; goto cUK13; cUK13: } else { goto RCiYp; nZq4u: $QYNfD = ModelUtil::getWithLock('atomic', array('name' => $MGY9H)); goto YoyNe; RCiYp: self::autoCleanDB(); goto nhjO5; Haq0F: ModelUtil::update('atomic', array('name' => $MGY9H), array('value' => $QYNfD['value'] - 1)); goto L0XMq; YoyNe: if (empty($QYNfD)) { ModelUtil::transactionCommit(); return false; } goto gQXfD; L0XMq: ModelUtil::transactionCommit(); goto X50X8; gQXfD: if ($QYNfD['expire'] < time() || $QYNfD['value'] < 0) { goto IeeUR; FHpag: return false; goto kBrjo; Ne6L_: ModelUtil::transactionCommit(); goto FHpag; IeeUR: ModelUtil::delete('atomic', array('name' => $MGY9H)); goto Ne6L_; kBrjo: } goto Haq0F; nhjO5: ModelUtil::transactionBegin(); goto nZq4u; X50X8: return true; goto CCwE_; CCwE_: } } public static function remove($MGY9H) { if (RedisUtil::isEnable()) { $b10XM = "Atomic:{$MGY9H}"; RedisUtil::delete($b10XM); } else { ModelUtil::delete('atomic', array('name' => $MGY9H)); } } public static function acquire($MGY9H, $Tsb75 = 30) { if (RedisUtil::isEnable()) { goto vHnJj; vHnJj: $rY0hM = "Atomic:{$MGY9H}"; goto gdzn9; gdzn9: if (RedisUtil::setnx($rY0hM, time() + $Tsb75)) { RedisUtil::expire($rY0hM, $Tsb75); return true; } goto ePUsj; ePUsj: $wbyeU = RedisUtil::get($rY0hM); goto zwap9; NSwDY: return false; goto h4TlH; zwap9: if ($wbyeU < time()) { RedisUtil::delete($rY0hM); return self::acquire($MGY9H, $Tsb75); } goto NSwDY; h4TlH: } else { goto NZ7Ea; j1xyL: $QYNfD = ModelUtil::getWithLock('atomic', array('name' => $MGY9H)); goto rG9RO; rG9RO: $wbyeU = time() + $Tsb75; goto HpMDL; KFneA: ModelUtil::transactionCommit(); goto oS93v; oS93v: return false; goto hp8q6; HpMDL: if (empty($QYNfD)) { goto K_U35; JFMvP: return true; goto Fq226; robsl: ModelUtil::transactionCommit(); goto JFMvP; K_U35: ModelUtil::insert('atomic', array('name' => $MGY9H, 'value' => 1, 'expire' => $wbyeU)); goto robsl; Fq226: } goto RfSLl; RfSLl: if ($QYNfD['expire'] < time()) { goto pRJ8N; NThaI: return true; goto nJceT; pRJ8N: ModelUtil::update('atomic', array('name' => $MGY9H), array('value' => 1, 'expire' => $wbyeU)); goto VEZ0F; VEZ0F: ModelUtil::transactionCommit(); goto NThaI; nJceT: } goto KFneA; NZ7Ea: self::autoCleanDB(); goto hbzqw; hbzqw: ModelUtil::transactionBegin(); goto j1xyL; hp8q6: } } public static function release($MGY9H) { if (RedisUtil::isEnable()) { $rY0hM = "Atomic:{$MGY9H}"; RedisUtil::delete($rY0hM); } else { ModelUtil::delete('atomic', array('name' => $MGY9H)); } } }