<?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($SeTC_, $sKrwM, $PuZZz = 3600) { if (RedisUtil::isEnable()) { goto DdN_z; BLIo8: RedisUtil::set($rTbLI, $sKrwM); goto vcdmZ; vcdmZ: RedisUtil::expire($rTbLI, $PuZZz); goto kCjRX; DdN_z: $rTbLI = "Atomic:{$SeTC_}"; goto BLIo8; kCjRX: } 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 gSFPo; m1BPl: return false; goto ZEkqn; t1Ni9: if (RedisUtil::decr($rTbLI) >= 0) { return true; } goto m1BPl; gSFPo: $rTbLI = "Atomic:{$SeTC_}"; goto t1Ni9; ZEkqn: } else { goto lUStT; lY04v: ModelUtil::transactionCommit(); goto kufnA; Rle9u: if (empty($H5vnz)) { ModelUtil::transactionCommit(); return false; } goto p6uXc; lUStT: self::autoCleanDB(); goto fdRiH; kufnA: return true; goto VsD9D; IUKwI: ModelUtil::update('atomic', array('name' => $SeTC_), array('value' => $H5vnz['value'] - 1)); goto lY04v; qUmvS: $H5vnz = ModelUtil::getWithLock('atomic', array('name' => $SeTC_)); goto Rle9u; p6uXc: if ($H5vnz['expire'] < time() || $H5vnz['value'] < 0) { goto gOcXk; QajQZ: ModelUtil::transactionCommit(); goto HwJcs; gOcXk: ModelUtil::delete('atomic', array('name' => $SeTC_)); goto QajQZ; HwJcs: return false; goto uOG57; uOG57: } goto IUKwI; fdRiH: ModelUtil::transactionBegin(); goto qUmvS; VsD9D: } } 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 iuZNo; TN3iV: $d2lSi = RedisUtil::get($ySsOv); goto rrSmC; trQeC: return false; goto StUEI; iuZNo: $ySsOv = "Atomic:{$SeTC_}"; goto BhxSn; BhxSn: if (RedisUtil::setnx($ySsOv, time() + $PuZZz)) { RedisUtil::expire($ySsOv, $PuZZz); return true; } goto TN3iV; rrSmC: if ($d2lSi < time()) { RedisUtil::delete($ySsOv); return self::acquire($SeTC_, $PuZZz); } goto trQeC; StUEI: } else { goto jM5fd; ZnaJA: if ($H5vnz['expire'] < time()) { goto qCz81; FLRvB: return true; goto F62rF; qCz81: ModelUtil::update('atomic', array('name' => $SeTC_), array('value' => 1, 'expire' => $d2lSi)); goto nUI03; nUI03: ModelUtil::transactionCommit(); goto FLRvB; F62rF: } goto oygFK; QU34j: if (empty($H5vnz)) { goto KZj4n; qv_Hg: return true; goto rv90g; myIxW: ModelUtil::transactionCommit(); goto qv_Hg; KZj4n: ModelUtil::insert('atomic', array('name' => $SeTC_, 'value' => 1, 'expire' => $d2lSi)); goto myIxW; rv90g: } goto ZnaJA; e2nUH: ModelUtil::transactionBegin(); goto yD_q0; jM5fd: self::autoCleanDB(); goto e2nUH; qz2_I: return false; goto DS4sk; oygFK: ModelUtil::transactionCommit(); goto qz2_I; yD_q0: $H5vnz = ModelUtil::getWithLock('atomic', array('name' => $SeTC_)); goto goH4e; goH4e: $d2lSi = time() + $PuZZz; goto QU34j; DS4sk: } } public static function release($SeTC_) { if (RedisUtil::isEnable()) { $ySsOv = "Atomic:{$SeTC_}"; RedisUtil::delete($ySsOv); } else { ModelUtil::delete('atomic', array('name' => $SeTC_)); } } }