<?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($SeTC_, $sKrwM, $PuZZz = 3600) { if (RedisUtil::isEnable()) { goto kIj9M; kIj9M: $rTbLI = "Atomic:{$SeTC_}"; goto iTy3W; iz3Gr: RedisUtil::expire($rTbLI, $PuZZz); goto fkwtH; iTy3W: RedisUtil::set($rTbLI, $sKrwM); goto iz3Gr; fkwtH: } else { if (ModelUtil::exists('atomic', array('name' => $SeTC_))) { ModelUtil::update('atomic', array('name' => $SeTC_), array('value' => $sKrwM, 'expire' => time() + $PuZZz)); } else { ModelUtil::insert('atomic', array('name' => $SeTC_, 'value' => $sKrwM, 'expire' => time() + $PuZZz)); } self::autoCleanDB(); } } public static function consume($SeTC_) { if (RedisUtil::isEnable()) { goto O0Ei8; Jl5QG: return false; goto AKtmT; O0Ei8: $rTbLI = "Atomic:{$SeTC_}"; goto WiwqL; WiwqL: if (RedisUtil::decr($rTbLI) >= 0) { return true; } goto Jl5QG; AKtmT: } else { goto RQ7rR; P3YS7: if ($H5vnz['expire'] < time() || $H5vnz['value'] < 0) { goto YgfaV; YgfaV: ModelUtil::delete('atomic', array('name' => $SeTC_)); goto sN0V5; PWEks: return false; goto OIQMj; sN0V5: ModelUtil::transactionCommit(); goto PWEks; OIQMj: } goto oVtK8; RQ7rR: self::autoCleanDB(); goto CMXZW; oVtK8: ModelUtil::update('atomic', array('name' => $SeTC_), array('value' => $H5vnz['value'] - 1)); goto XE3L0; FbKrS: if (empty($H5vnz)) { ModelUtil::transactionCommit(); return false; } goto P3YS7; b4GT5: $H5vnz = ModelUtil::getWithLock('atomic', array('name' => $SeTC_)); goto FbKrS; CMXZW: ModelUtil::transactionBegin(); goto b4GT5; rowe1: return true; goto U25x8; XE3L0: ModelUtil::transactionCommit(); goto rowe1; U25x8: } } public static function remove($SeTC_) { if (RedisUtil::isEnable()) { $rTbLI = "Atomic:{$SeTC_}"; RedisUtil::delete($rTbLI); } else { ModelUtil::delete('atomic', array('name' => $SeTC_)); } } public static function acquire($SeTC_, $PuZZz = 30) { if (RedisUtil::isEnable()) { goto m2iJT; yIa69: if (RedisUtil::setnx($ySsOv, time() + $PuZZz)) { RedisUtil::expire($ySsOv, $PuZZz); return true; } goto yQ35a; yQ35a: $d2lSi = RedisUtil::get($ySsOv); goto kSoJ0; kSoJ0: if ($d2lSi < time()) { RedisUtil::delete($ySsOv); return self::acquire($SeTC_, $PuZZz); } goto dBg4x; m2iJT: $ySsOv = "Atomic:{$SeTC_}"; goto yIa69; dBg4x: return false; goto ixPL2; ixPL2: } else { goto i_fv4; oJBBr: if ($H5vnz['expire'] < time()) { goto P83Sx; BNBgk: return true; goto iphlp; NANYA: ModelUtil::transactionCommit(); goto BNBgk; P83Sx: ModelUtil::update('atomic', array('name' => $SeTC_), array('value' => 1, 'expire' => $d2lSi)); goto NANYA; iphlp: } goto qxue0; ZhVe6: if (empty($H5vnz)) { goto Dfglu; pcA3K: return true; goto O3t7E; XFJRp: ModelUtil::transactionCommit(); goto pcA3K; Dfglu: ModelUtil::insert('atomic', array('name' => $SeTC_, 'value' => 1, 'expire' => $d2lSi)); goto XFJRp; O3t7E: } goto oJBBr; ZWYoL: ModelUtil::transactionBegin(); goto XV6QD; i_fv4: self::autoCleanDB(); goto ZWYoL; XV6QD: $H5vnz = ModelUtil::getWithLock('atomic', array('name' => $SeTC_)); goto i377K; i377K: $d2lSi = time() + $PuZZz; goto ZhVe6; qxue0: ModelUtil::transactionCommit(); goto lQlmk; lQlmk: return false; goto iRknc; iRknc: } } public static function release($SeTC_) { if (RedisUtil::isEnable()) { $ySsOv = "Atomic:{$SeTC_}"; RedisUtil::delete($ySsOv); } else { ModelUtil::delete('atomic', array('name' => $SeTC_)); } } }