<?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($aVT3z, $qQcPQ, $TmEg4 = 3600) { if (RedisUtil::isEnable()) { goto xzTOj; xzTOj: $ApvS1 = "Atomic:{$aVT3z}"; goto kUHBr; CfLBs: RedisUtil::expire($ApvS1, $TmEg4); goto chT0t; kUHBr: RedisUtil::set($ApvS1, $qQcPQ); goto CfLBs; chT0t: } else { if (ModelUtil::exists('atomic', array('name' => $aVT3z))) { ModelUtil::update('atomic', array('name' => $aVT3z), array('value' => $qQcPQ, 'expire' => time() + $TmEg4)); } else { ModelUtil::insert('atomic', array('name' => $aVT3z, 'value' => $qQcPQ, 'expire' => time() + $TmEg4)); } self::autoCleanDB(); } } public static function consume($aVT3z) { if (RedisUtil::isEnable()) { goto E7Hog; hjclv: return false; goto ya9sp; E7Hog: $ApvS1 = "Atomic:{$aVT3z}"; goto Rt21h; Rt21h: if (RedisUtil::decr($ApvS1) >= 0) { return true; } goto hjclv; ya9sp: } else { goto JwYoQ; MNiYx: $jXe_5 = ModelUtil::getWithLock('atomic', array('name' => $aVT3z)); goto vcGOM; PzrcR: ModelUtil::update('atomic', array('name' => $aVT3z), array('value' => $jXe_5['value'] - 1)); goto jaap0; Q31Vq: if ($jXe_5['expire'] < time() || $jXe_5['value'] < 0) { goto SlmV7; SlmV7: ModelUtil::delete('atomic', array('name' => $aVT3z)); goto wOu5V; RrHA4: return false; goto dSW8o; wOu5V: ModelUtil::transactionCommit(); goto RrHA4; dSW8o: } goto PzrcR; jaap0: ModelUtil::transactionCommit(); goto gg2Dq; vcGOM: if (empty($jXe_5)) { ModelUtil::transactionCommit(); return false; } goto Q31Vq; gg2Dq: return true; goto NbTDB; JwYoQ: self::autoCleanDB(); goto off4Y; off4Y: ModelUtil::transactionBegin(); goto MNiYx; NbTDB: } } public static function remove($aVT3z) { if (RedisUtil::isEnable()) { $ApvS1 = "Atomic:{$aVT3z}"; RedisUtil::delete($ApvS1); } else { ModelUtil::delete('atomic', array('name' => $aVT3z)); } } public static function acquire($aVT3z, $TmEg4 = 30) { if (RedisUtil::isEnable()) { goto x_y6B; vVamu: if ($M3Qja < time()) { RedisUtil::delete($OSU2M); return self::acquire($aVT3z, $TmEg4); } goto R2hye; x_y6B: $OSU2M = "Atomic:{$aVT3z}"; goto xs5f2; xs5f2: if (RedisUtil::setnx($OSU2M, time() + $TmEg4)) { RedisUtil::expire($OSU2M, $TmEg4); return true; } goto Ft1hD; R2hye: return false; goto AgEqe; Ft1hD: $M3Qja = RedisUtil::get($OSU2M); goto vVamu; AgEqe: } else { goto bHMa0; bHMa0: self::autoCleanDB(); goto GcXjW; UDFYH: return false; goto Gxt2Z; nFBuN: if ($jXe_5['expire'] < time()) { goto IarVo; I_utR: ModelUtil::transactionCommit(); goto rS9ni; rS9ni: return true; goto Y1PRe; IarVo: ModelUtil::update('atomic', array('name' => $aVT3z), array('value' => 1, 'expire' => $M3Qja)); goto I_utR; Y1PRe: } goto x6fjP; GcXjW: ModelUtil::transactionBegin(); goto HdR4t; HdR4t: $jXe_5 = ModelUtil::getWithLock('atomic', array('name' => $aVT3z)); goto Pdx6p; x6fjP: ModelUtil::transactionCommit(); goto UDFYH; Pdx6p: $M3Qja = time() + $TmEg4; goto nflZQ; nflZQ: if (empty($jXe_5)) { goto fUyAr; fUyAr: ModelUtil::insert('atomic', array('name' => $aVT3z, 'value' => 1, 'expire' => $M3Qja)); goto zVaK_; zVaK_: ModelUtil::transactionCommit(); goto jpG8t; jpG8t: return true; goto iZY8p; iZY8p: } goto nFBuN; Gxt2Z: } } public static function release($aVT3z) { if (RedisUtil::isEnable()) { $OSU2M = "Atomic:{$aVT3z}"; RedisUtil::delete($OSU2M); } else { ModelUtil::delete('atomic', array('name' => $aVT3z)); } } }