<?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($TTiWe, $VRDYW, $duruy = 3600) { if (RedisUtil::isEnable()) { goto zIEoE; zIEoE: $UIQRD = "Atomic:{$TTiWe}"; goto UBMrw; VRs1V: RedisUtil::expire($UIQRD, $duruy); goto AesIz; UBMrw: RedisUtil::set($UIQRD, $VRDYW); goto VRs1V; AesIz: } 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 ewXeM; ewXeM: $UIQRD = "Atomic:{$TTiWe}"; goto INg5K; INg5K: if (RedisUtil::decr($UIQRD) >= 0) { return true; } goto Vdq14; Vdq14: return false; goto phfFn; phfFn: } else { goto mhjSj; hJnFb: if ($rXNvx['expire'] < time() || $rXNvx['value'] < 0) { goto OIsMy; OIsMy: ModelUtil::delete('atomic', array('name' => $TTiWe)); goto pnWq_; pnWq_: ModelUtil::transactionCommit(); goto ik1wa; ik1wa: return false; goto HFlrQ; HFlrQ: } goto AfCFi; j0SwN: ModelUtil::transactionCommit(); goto PuzYV; mhjSj: self::autoCleanDB(); goto SaGxr; SImkg: if (empty($rXNvx)) { ModelUtil::transactionCommit(); return false; } goto hJnFb; Ac10X: $rXNvx = ModelUtil::getWithLock('atomic', array('name' => $TTiWe)); goto SImkg; AfCFi: ModelUtil::update('atomic', array('name' => $TTiWe), array('value' => $rXNvx['value'] - 1)); goto j0SwN; SaGxr: ModelUtil::transactionBegin(); goto Ac10X; PuzYV: return true; goto u9tri; u9tri: } } 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 dxv4z; dxv4z: $FYJFh = "Atomic:{$TTiWe}"; goto esK8_; uI6pJ: if ($ai0lP < time()) { RedisUtil::delete($FYJFh); return self::acquire($TTiWe, $duruy); } goto CG5Cr; wKObM: $ai0lP = RedisUtil::get($FYJFh); goto uI6pJ; esK8_: if (RedisUtil::setnx($FYJFh, time() + $duruy)) { RedisUtil::expire($FYJFh, $duruy); return true; } goto wKObM; CG5Cr: return false; goto rue7j; rue7j: } else { goto mp23Q; c0IAv: if (empty($rXNvx)) { goto xVXIO; xVXIO: ModelUtil::insert('atomic', array('name' => $TTiWe, 'value' => 1, 'expire' => $ai0lP)); goto x8EOJ; x8EOJ: ModelUtil::transactionCommit(); goto thlQM; thlQM: return true; goto b7rUj; b7rUj: } goto frCPk; NtZka: ModelUtil::transactionBegin(); goto Jd6Xt; frCPk: if ($rXNvx['expire'] < time()) { goto Qd28p; KZBNf: return true; goto qAZZ2; cafVh: ModelUtil::transactionCommit(); goto KZBNf; Qd28p: ModelUtil::update('atomic', array('name' => $TTiWe), array('value' => 1, 'expire' => $ai0lP)); goto cafVh; qAZZ2: } goto QvN_N; QvN_N: ModelUtil::transactionCommit(); goto B9qQG; mp23Q: self::autoCleanDB(); goto NtZka; Jd6Xt: $rXNvx = ModelUtil::getWithLock('atomic', array('name' => $TTiWe)); goto pbSvT; pbSvT: $ai0lP = time() + $duruy; goto c0IAv; B9qQG: return false; goto zJfKs; zJfKs: } } public static function release($TTiWe) { if (RedisUtil::isEnable()) { $FYJFh = "Atomic:{$TTiWe}"; RedisUtil::delete($FYJFh); } else { ModelUtil::delete('atomic', array('name' => $TTiWe)); } } }