<?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($zO6V5, $jkY57, $Jd4L6 = 3600) { if (RedisUtil::isEnable()) { goto DgnP7; hntn5: RedisUtil::set($c2rbB, $jkY57); goto GQ0PK; DgnP7: $c2rbB = "Atomic:{$zO6V5}"; goto hntn5; GQ0PK: RedisUtil::expire($c2rbB, $Jd4L6); goto zXZ9Y; zXZ9Y: } else { if (ModelUtil::exists('atomic', array('name' => $zO6V5))) { ModelUtil::update('atomic', array('name' => $zO6V5), array('value' => $jkY57, 'expire' => time() + $Jd4L6)); } else { ModelUtil::insert('atomic', array('name' => $zO6V5, 'value' => $jkY57, 'expire' => time() + $Jd4L6)); } self::autoCleanDB(); } } public static function consume($zO6V5) { if (RedisUtil::isEnable()) { goto a2EWj; x1meg: return false; goto adbgd; DjYKk: if (RedisUtil::decr($c2rbB) >= 0) { return true; } goto x1meg; a2EWj: $c2rbB = "Atomic:{$zO6V5}"; goto DjYKk; adbgd: } else { goto ezEH4; ab5Th: ModelUtil::transactionCommit(); goto DWJar; qBOGq: $AI4Cy = ModelUtil::getWithLock('atomic', array('name' => $zO6V5)); goto fkXVD; HDxBB: ModelUtil::update('atomic', array('name' => $zO6V5), array('value' => $AI4Cy['value'] - 1)); goto ab5Th; d1yqC: ModelUtil::transactionBegin(); goto qBOGq; rnN2B: if ($AI4Cy['expire'] < time() || $AI4Cy['value'] < 0) { goto w0W93; w0W93: ModelUtil::delete('atomic', array('name' => $zO6V5)); goto qZyyS; JKJQj: return false; goto n_Sem; qZyyS: ModelUtil::transactionCommit(); goto JKJQj; n_Sem: } goto HDxBB; DWJar: return true; goto eD1wp; fkXVD: if (empty($AI4Cy)) { ModelUtil::transactionCommit(); return false; } goto rnN2B; ezEH4: self::autoCleanDB(); goto d1yqC; eD1wp: } } public static function remove($zO6V5) { if (RedisUtil::isEnable()) { $c2rbB = "Atomic:{$zO6V5}"; RedisUtil::delete($c2rbB); } else { ModelUtil::delete('atomic', array('name' => $zO6V5)); } } public static function acquire($zO6V5, $Jd4L6 = 30) { if (RedisUtil::isEnable()) { goto TVKW7; hBjyr: if (RedisUtil::setnx($sFwJk, time() + $Jd4L6)) { RedisUtil::expire($sFwJk, $Jd4L6); return true; } goto mZknC; YnTdL: return false; goto eSUBd; dogLY: if ($j_2EL < time()) { RedisUtil::delete($sFwJk); return self::acquire($zO6V5, $Jd4L6); } goto YnTdL; TVKW7: $sFwJk = "Atomic:{$zO6V5}"; goto hBjyr; mZknC: $j_2EL = RedisUtil::get($sFwJk); goto dogLY; eSUBd: } else { goto pRRpM; I2cCa: return false; goto Jeuzh; lgdH4: ModelUtil::transactionCommit(); goto I2cCa; lC_jj: ModelUtil::transactionBegin(); goto tcvfD; Kw_O4: if ($AI4Cy['expire'] < time()) { goto dGU9A; dGU9A: ModelUtil::update('atomic', array('name' => $zO6V5), array('value' => 1, 'expire' => $j_2EL)); goto ldu3v; ldu3v: ModelUtil::transactionCommit(); goto i1QN8; i1QN8: return true; goto BV2nI; BV2nI: } goto lgdH4; pRRpM: self::autoCleanDB(); goto lC_jj; yjhLE: if (empty($AI4Cy)) { goto e7U0_; e7U0_: ModelUtil::insert('atomic', array('name' => $zO6V5, 'value' => 1, 'expire' => $j_2EL)); goto m7DJI; m7DJI: ModelUtil::transactionCommit(); goto UyXlD; UyXlD: return true; goto E38ec; E38ec: } goto Kw_O4; tcvfD: $AI4Cy = ModelUtil::getWithLock('atomic', array('name' => $zO6V5)); goto uWAeu; uWAeu: $j_2EL = time() + $Jd4L6; goto yjhLE; Jeuzh: } } public static function release($zO6V5) { if (RedisUtil::isEnable()) { $sFwJk = "Atomic:{$zO6V5}"; RedisUtil::delete($sFwJk); } else { ModelUtil::delete('atomic', array('name' => $zO6V5)); } } }