<?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($TTiWe, $VRDYW, $duruy = 3600) { if (RedisUtil::isEnable()) { goto GNNiu; aUJGx: RedisUtil::set($UIQRD, $VRDYW); goto fUSVX; GNNiu: $UIQRD = "Atomic:{$TTiWe}"; goto aUJGx; fUSVX: RedisUtil::expire($UIQRD, $duruy); goto lC631; lC631: } else { if (ModelUtil::exists('atomic', array('name' => $TTiWe))) { ModelUtil::update('atomic', array('name' => $TTiWe), array('value' => $VRDYW, 'expire' => time() + $duruy)); } else { ModelUtil::insert('atomic', array('name' => $TTiWe, 'value' => $VRDYW, 'expire' => time() + $duruy)); } self::autoCleanDB(); } } public static function consume($TTiWe) { if (RedisUtil::isEnable()) { goto MR0py; MR0py: $UIQRD = "Atomic:{$TTiWe}"; goto DJ8lA; jqdR0: return false; goto zVU1Z; DJ8lA: if (RedisUtil::decr($UIQRD) >= 0) { return true; } goto jqdR0; zVU1Z: } else { goto RRviR; PkSJ1: if ($rXNvx['expire'] < time() || $rXNvx['value'] < 0) { goto bHt9w; bHt9w: ModelUtil::delete('atomic', array('name' => $TTiWe)); goto lRt1N; lRt1N: ModelUtil::transactionCommit(); goto eBSRU; eBSRU: return false; goto qHiqK; qHiqK: } goto oNJE1; kMQcE: ModelUtil::transactionBegin(); goto baskj; oNJE1: ModelUtil::update('atomic', array('name' => $TTiWe), array('value' => $rXNvx['value'] - 1)); goto zZ_rj; baskj: $rXNvx = ModelUtil::getWithLock('atomic', array('name' => $TTiWe)); goto s1Zh8; rWXTV: return true; goto EBFwq; zZ_rj: ModelUtil::transactionCommit(); goto rWXTV; RRviR: self::autoCleanDB(); goto kMQcE; s1Zh8: if (empty($rXNvx)) { ModelUtil::transactionCommit(); return false; } goto PkSJ1; EBFwq: } } public static function remove($TTiWe) { if (RedisUtil::isEnable()) { $UIQRD = "Atomic:{$TTiWe}"; RedisUtil::delete($UIQRD); } else { ModelUtil::delete('atomic', array('name' => $TTiWe)); } } public static function acquire($TTiWe, $duruy = 30) { if (RedisUtil::isEnable()) { goto I2KXh; I2KXh: $FYJFh = "Atomic:{$TTiWe}"; goto aAgVz; ZejyF: return false; goto mGnHw; aAgVz: if (RedisUtil::setnx($FYJFh, time() + $duruy)) { RedisUtil::expire($FYJFh, $duruy); return true; } goto b0JR8; b0JR8: $ai0lP = RedisUtil::get($FYJFh); goto AtwBZ; AtwBZ: if ($ai0lP < time()) { RedisUtil::delete($FYJFh); return self::acquire($TTiWe, $duruy); } goto ZejyF; mGnHw: } else { goto SfEAP; ul2nu: return false; goto bMk2W; s883L: if ($rXNvx['expire'] < time()) { goto alc94; alc94: ModelUtil::update('atomic', array('name' => $TTiWe), array('value' => 1, 'expire' => $ai0lP)); goto lCTb9; lCTb9: ModelUtil::transactionCommit(); goto JPQvJ; JPQvJ: return true; goto C2lzp; C2lzp: } goto YLuih; IZyba: if (empty($rXNvx)) { goto ngvP2; s5MVV: ModelUtil::transactionCommit(); goto DTG_V; ngvP2: ModelUtil::insert('atomic', array('name' => $TTiWe, 'value' => 1, 'expire' => $ai0lP)); goto s5MVV; DTG_V: return true; goto q7f1B; q7f1B: } goto s883L; SfEAP: self::autoCleanDB(); goto Ng0zu; YLuih: ModelUtil::transactionCommit(); goto ul2nu; yRtU7: $rXNvx = ModelUtil::getWithLock('atomic', array('name' => $TTiWe)); goto oUKxi; Ng0zu: ModelUtil::transactionBegin(); goto yRtU7; oUKxi: $ai0lP = time() + $duruy; goto IZyba; bMk2W: } } public static function release($TTiWe) { if (RedisUtil::isEnable()) { $FYJFh = "Atomic:{$TTiWe}"; RedisUtil::delete($FYJFh); } else { ModelUtil::delete('atomic', array('name' => $TTiWe)); } } }