<?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($L1N0l, $fo71I, $KfcNX = 3600) { if (RedisUtil::isEnable()) { goto Jfboi; H6X7I: RedisUtil::expire($BW6Lj, $KfcNX); goto rGyBC; OwaoS: RedisUtil::set($BW6Lj, $fo71I); goto H6X7I; Jfboi: $BW6Lj = "Atomic:{$L1N0l}"; goto OwaoS; rGyBC: } 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 EanhO; L32F7: if (RedisUtil::decr($BW6Lj) >= 0) { return true; } goto RP8N0; RP8N0: return false; goto Kg0Yw; EanhO: $BW6Lj = "Atomic:{$L1N0l}"; goto L32F7; Kg0Yw: } else { goto Yq8We; In2SY: return true; goto tBGdw; OqUNf: if (empty($CAaU2)) { ModelUtil::transactionCommit(); return false; } goto IUoWm; cYSLL: $CAaU2 = ModelUtil::getWithLock('atomic', array('name' => $L1N0l)); goto OqUNf; x6vOk: ModelUtil::transactionBegin(); goto cYSLL; IUoWm: if ($CAaU2['expire'] < time() || $CAaU2['value'] < 0) { goto sXjgq; lN7kn: ModelUtil::transactionCommit(); goto i1R0Q; i1R0Q: return false; goto uG9sq; sXjgq: ModelUtil::delete('atomic', array('name' => $L1N0l)); goto lN7kn; uG9sq: } goto bmMcz; HMLvm: ModelUtil::transactionCommit(); goto In2SY; bmMcz: ModelUtil::update('atomic', array('name' => $L1N0l), array('value' => $CAaU2['value'] - 1)); goto HMLvm; Yq8We: self::autoCleanDB(); goto x6vOk; tBGdw: } } 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 UtBvr; VlW95: $J2adr = RedisUtil::get($UtKOH); goto vyieJ; vyieJ: if ($J2adr < time()) { RedisUtil::delete($UtKOH); return self::acquire($L1N0l, $KfcNX); } goto YFyj4; UtBvr: $UtKOH = "Atomic:{$L1N0l}"; goto lfSP_; lfSP_: if (RedisUtil::setnx($UtKOH, time() + $KfcNX)) { RedisUtil::expire($UtKOH, $KfcNX); return true; } goto VlW95; YFyj4: return false; goto C3b1P; C3b1P: } else { goto JJ8ur; Lv_W3: ModelUtil::transactionCommit(); goto UZV9Q; XemYv: ModelUtil::transactionBegin(); goto fpjnv; iPR62: $J2adr = time() + $KfcNX; goto jKbDB; fpjnv: $CAaU2 = ModelUtil::getWithLock('atomic', array('name' => $L1N0l)); goto iPR62; UZV9Q: return false; goto FPDBz; JJ8ur: self::autoCleanDB(); goto XemYv; jKbDB: if (empty($CAaU2)) { goto jOt8H; rPhff: return true; goto ZsAFG; jOt8H: ModelUtil::insert('atomic', array('name' => $L1N0l, 'value' => 1, 'expire' => $J2adr)); goto iPFdG; iPFdG: ModelUtil::transactionCommit(); goto rPhff; ZsAFG: } goto zMmSy; zMmSy: if ($CAaU2['expire'] < time()) { goto bATxD; neyUG: ModelUtil::transactionCommit(); goto slkig; slkig: return true; goto uO0F1; bATxD: ModelUtil::update('atomic', array('name' => $L1N0l), array('value' => 1, 'expire' => $J2adr)); goto neyUG; uO0F1: } goto Lv_W3; FPDBz: } } public static function release($L1N0l) { if (RedisUtil::isEnable()) { $UtKOH = "Atomic:{$L1N0l}"; RedisUtil::delete($UtKOH); } else { ModelUtil::delete('atomic', array('name' => $L1N0l)); } } }