<?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($zO6V5, $jkY57, $Jd4L6 = 3600) { if (RedisUtil::isEnable()) { goto QmwDc; yNoyk: RedisUtil::expire($c2rbB, $Jd4L6); goto o0lTt; QmwDc: $c2rbB = "Atomic:{$zO6V5}"; goto JEG5f; JEG5f: RedisUtil::set($c2rbB, $jkY57); goto yNoyk; o0lTt: } 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 RfiyU; a3ujH: return false; goto YhprP; w6JKB: if (RedisUtil::decr($c2rbB) >= 0) { return true; } goto a3ujH; RfiyU: $c2rbB = "Atomic:{$zO6V5}"; goto w6JKB; YhprP: } else { goto PHo66; f6rrC: $AI4Cy = ModelUtil::getWithLock('atomic', array('name' => $zO6V5)); goto gGfrr; yScL6: ModelUtil::update('atomic', array('name' => $zO6V5), array('value' => $AI4Cy['value'] - 1)); goto OXyE1; gGfrr: if (empty($AI4Cy)) { ModelUtil::transactionCommit(); return false; } goto iIE4f; t4CHx: ModelUtil::transactionBegin(); goto f6rrC; iIE4f: if ($AI4Cy['expire'] < time() || $AI4Cy['value'] < 0) { goto DIn28; DIn28: ModelUtil::delete('atomic', array('name' => $zO6V5)); goto hXo2Z; hXo2Z: ModelUtil::transactionCommit(); goto gIxF0; gIxF0: return false; goto k8DNY; k8DNY: } goto yScL6; P3vca: return true; goto IlW6x; OXyE1: ModelUtil::transactionCommit(); goto P3vca; PHo66: self::autoCleanDB(); goto t4CHx; IlW6x: } } 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 mU680; GyzRj: if (RedisUtil::setnx($sFwJk, time() + $Jd4L6)) { RedisUtil::expire($sFwJk, $Jd4L6); return true; } goto DIT6K; mU680: $sFwJk = "Atomic:{$zO6V5}"; goto GyzRj; b0bT7: if ($j_2EL < time()) { RedisUtil::delete($sFwJk); return self::acquire($zO6V5, $Jd4L6); } goto UKpwe; UKpwe: return false; goto sczEF; DIT6K: $j_2EL = RedisUtil::get($sFwJk); goto b0bT7; sczEF: } else { goto G1ErR; x5NpC: return false; goto FOU_u; G1ErR: self::autoCleanDB(); goto Fkpkb; Fkpkb: ModelUtil::transactionBegin(); goto A3VOy; AGS0p: if ($AI4Cy['expire'] < time()) { goto dVdJO; dVdJO: ModelUtil::update('atomic', array('name' => $zO6V5), array('value' => 1, 'expire' => $j_2EL)); goto r9Nyk; yQPGO: return true; goto dvh0v; r9Nyk: ModelUtil::transactionCommit(); goto yQPGO; dvh0v: } goto IFCf8; gQJoR: $j_2EL = time() + $Jd4L6; goto Q2rnV; Q2rnV: if (empty($AI4Cy)) { goto gdi20; gdi20: ModelUtil::insert('atomic', array('name' => $zO6V5, 'value' => 1, 'expire' => $j_2EL)); goto Gjt6r; Gjt6r: ModelUtil::transactionCommit(); goto l8Zmm; l8Zmm: return true; goto qY2_F; qY2_F: } goto AGS0p; IFCf8: ModelUtil::transactionCommit(); goto x5NpC; A3VOy: $AI4Cy = ModelUtil::getWithLock('atomic', array('name' => $zO6V5)); goto gQJoR; FOU_u: } } public static function release($zO6V5) { if (RedisUtil::isEnable()) { $sFwJk = "Atomic:{$zO6V5}"; RedisUtil::delete($sFwJk); } else { ModelUtil::delete('atomic', array('name' => $zO6V5)); } } }