<?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($MGY9H, $hsART, $Tsb75 = 3600) { if (RedisUtil::isEnable()) { goto I690w; YMtou: RedisUtil::set($b10XM, $hsART); goto wJG9R; I690w: $b10XM = "Atomic:{$MGY9H}"; goto YMtou; wJG9R: RedisUtil::expire($b10XM, $Tsb75); goto t1DCG; t1DCG: } 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 dz2iJ; F4ePi: return false; goto DHv6z; dz2iJ: $b10XM = "Atomic:{$MGY9H}"; goto zcQ88; zcQ88: if (RedisUtil::decr($b10XM) >= 0) { return true; } goto F4ePi; DHv6z: } else { goto pYd7N; aFmUX: if ($QYNfD['expire'] < time() || $QYNfD['value'] < 0) { goto KPzFp; KPzFp: ModelUtil::delete('atomic', array('name' => $MGY9H)); goto D22cJ; D22cJ: ModelUtil::transactionCommit(); goto bNVP7; bNVP7: return false; goto fR7J7; fR7J7: } goto d89om; ts9qF: ModelUtil::transactionCommit(); goto x1quX; BP3Wj: ModelUtil::transactionBegin(); goto nv1t5; x1quX: return true; goto JlEoO; nv1t5: $QYNfD = ModelUtil::getWithLock('atomic', array('name' => $MGY9H)); goto j5uJQ; pYd7N: self::autoCleanDB(); goto BP3Wj; d89om: ModelUtil::update('atomic', array('name' => $MGY9H), array('value' => $QYNfD['value'] - 1)); goto ts9qF; j5uJQ: if (empty($QYNfD)) { ModelUtil::transactionCommit(); return false; } goto aFmUX; JlEoO: } } 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 FcsDt; t5LeD: if ($wbyeU < time()) { RedisUtil::delete($rY0hM); return self::acquire($MGY9H, $Tsb75); } goto PQlQD; j1wre: if (RedisUtil::setnx($rY0hM, time() + $Tsb75)) { RedisUtil::expire($rY0hM, $Tsb75); return true; } goto WrWLM; PQlQD: return false; goto qfpaY; WrWLM: $wbyeU = RedisUtil::get($rY0hM); goto t5LeD; FcsDt: $rY0hM = "Atomic:{$MGY9H}"; goto j1wre; qfpaY: } else { goto PGjdq; xoqEB: $QYNfD = ModelUtil::getWithLock('atomic', array('name' => $MGY9H)); goto w0EB4; PGjdq: self::autoCleanDB(); goto olIgZ; a_asE: if ($QYNfD['expire'] < time()) { goto JUEAy; jlKQM: return true; goto EqhSG; STKlX: ModelUtil::transactionCommit(); goto jlKQM; JUEAy: ModelUtil::update('atomic', array('name' => $MGY9H), array('value' => 1, 'expire' => $wbyeU)); goto STKlX; EqhSG: } goto HZldX; olIgZ: ModelUtil::transactionBegin(); goto xoqEB; e2wjW: if (empty($QYNfD)) { goto V5f37; V5f37: ModelUtil::insert('atomic', array('name' => $MGY9H, 'value' => 1, 'expire' => $wbyeU)); goto biZ87; biZ87: ModelUtil::transactionCommit(); goto Wcnq2; Wcnq2: return true; goto SLjIp; SLjIp: } goto a_asE; xVXRq: return false; goto eFHS0; HZldX: ModelUtil::transactionCommit(); goto xVXRq; w0EB4: $wbyeU = time() + $Tsb75; goto e2wjW; eFHS0: } } public static function release($MGY9H) { if (RedisUtil::isEnable()) { $rY0hM = "Atomic:{$MGY9H}"; RedisUtil::delete($rY0hM); } else { ModelUtil::delete('atomic', array('name' => $MGY9H)); } } }