<?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($L1N0l, $fo71I, $KfcNX = 3600) { if (RedisUtil::isEnable()) { goto lj5Jw; Rhyhd: RedisUtil::expire($BW6Lj, $KfcNX); goto bUJJt; lj5Jw: $BW6Lj = "Atomic:{$L1N0l}"; goto knTAU; knTAU: RedisUtil::set($BW6Lj, $fo71I); goto Rhyhd; bUJJt: } else { if (ModelUtil::exists('atomic', array('name' => $L1N0l))) { ModelUtil::update('atomic', array('name' => $L1N0l), array('value' => $fo71I, 'expire' => time() + $KfcNX)); } else { ModelUtil::insert('atomic', array('name' => $L1N0l, 'value' => $fo71I, 'expire' => time() + $KfcNX)); } self::autoCleanDB(); } } public static function consume($L1N0l) { if (RedisUtil::isEnable()) { goto xl5Hd; xl5Hd: $BW6Lj = "Atomic:{$L1N0l}"; goto fe2GG; WbKYL: return false; goto vIMwv; fe2GG: if (RedisUtil::decr($BW6Lj) >= 0) { return true; } goto WbKYL; vIMwv: } else { goto sc3QA; ge31g: ModelUtil::update('atomic', array('name' => $L1N0l), array('value' => $CAaU2['value'] - 1)); goto EKbPT; N14GK: $CAaU2 = ModelUtil::getWithLock('atomic', array('name' => $L1N0l)); goto qzN1J; yvYev: if ($CAaU2['expire'] < time() || $CAaU2['value'] < 0) { goto k_iRc; AvkcO: ModelUtil::transactionCommit(); goto x8uwq; x8uwq: return false; goto AEXIk; k_iRc: ModelUtil::delete('atomic', array('name' => $L1N0l)); goto AvkcO; AEXIk: } goto ge31g; EKbPT: ModelUtil::transactionCommit(); goto UZS0l; pgxAC: ModelUtil::transactionBegin(); goto N14GK; qzN1J: if (empty($CAaU2)) { ModelUtil::transactionCommit(); return false; } goto yvYev; UZS0l: return true; goto CoWIx; sc3QA: self::autoCleanDB(); goto pgxAC; CoWIx: } } public static function remove($L1N0l) { if (RedisUtil::isEnable()) { $BW6Lj = "Atomic:{$L1N0l}"; RedisUtil::delete($BW6Lj); } else { ModelUtil::delete('atomic', array('name' => $L1N0l)); } } public static function acquire($L1N0l, $KfcNX = 30) { if (RedisUtil::isEnable()) { goto Z73yf; SiEAb: return false; goto yjamG; XjkdM: if (RedisUtil::setnx($UtKOH, time() + $KfcNX)) { RedisUtil::expire($UtKOH, $KfcNX); return true; } goto U8mG4; Z73yf: $UtKOH = "Atomic:{$L1N0l}"; goto XjkdM; U8mG4: $J2adr = RedisUtil::get($UtKOH); goto p03v5; p03v5: if ($J2adr < time()) { RedisUtil::delete($UtKOH); return self::acquire($L1N0l, $KfcNX); } goto SiEAb; yjamG: } else { goto VkOcN; KbZ07: ModelUtil::transactionCommit(); goto OgctM; j0lKC: if ($CAaU2['expire'] < time()) { goto OFZUZ; x5eeK: ModelUtil::transactionCommit(); goto YdHwo; OFZUZ: ModelUtil::update('atomic', array('name' => $L1N0l), array('value' => 1, 'expire' => $J2adr)); goto x5eeK; YdHwo: return true; goto SsDcE; SsDcE: } goto KbZ07; EctRE: $J2adr = time() + $KfcNX; goto p2VtY; PE2pB: ModelUtil::transactionBegin(); goto LZnz3; OgctM: return false; goto POsbx; VkOcN: self::autoCleanDB(); goto PE2pB; p2VtY: if (empty($CAaU2)) { goto P12Yh; P12Yh: ModelUtil::insert('atomic', array('name' => $L1N0l, 'value' => 1, 'expire' => $J2adr)); goto tUeSh; uQuBa: return true; goto UdQvl; tUeSh: ModelUtil::transactionCommit(); goto uQuBa; UdQvl: } goto j0lKC; LZnz3: $CAaU2 = ModelUtil::getWithLock('atomic', array('name' => $L1N0l)); goto EctRE; POsbx: } } public static function release($L1N0l) { if (RedisUtil::isEnable()) { $UtKOH = "Atomic:{$L1N0l}"; RedisUtil::delete($UtKOH); } else { ModelUtil::delete('atomic', array('name' => $L1N0l)); } } }