Redis Zrangeコマンド-指定した区間内のメンバーを、インデックス区間を介して順序セットを返して合成します.

2236 ワード

文字創の自己増減操作により原子性を保証し、戻り値に基づいて指定したデータ内容を取得します.
/**
     *     
     * @param $coinId
     * @param $coinHashInfo
     * @return mixed
     */
    public function getCoin($coinSn,$coinHashInfo) {
        $key = $coinSn.$coinHashInfo['max_num'];
        $creatInfo = $this->_redis->get($key);
        if ($creatInfo === false) {//      0       
            $this->_redis->set($key,0);
            $location = 0;
        } else {
            $location = $this->_redis->incr($key);
        }
        if ($location == $coinHashInfo['max_num']) {//       
            ApiException::throwException(ApiException::COIN_NOT_RECEIVE, '', array(__FUNCTION__.' fail'));
        }
        //             0  
        $res = $this->_redis->zRange('coin_split_list_'.$coinSn,$location,$location,true);

        if (empty($res)) {
            Core::write( '---message:    reids  get '.',---data:'.json_encode($res),Core::LEVEL_REDIS_ERROR);
            ApiException::throwException(ApiException::COIN_GET_FAIL, '', array(__FUNCTION__.' fail'));
        }
        $value = array_values($res);
        $key = array_keys($res);
        $split_array = explode('_', $key[0]);

        $getInfo = [
            'money' => $split_array[1], //     
            'coin_split_id' => $value[0], //     id
        ];
        return $getInfo; //       
    }

質問:上記のget setは高合併時に超発的な現象が発生しやすく、原子操作に修正すべきである.
        $key = $coinSn.'_create_num';
        $location = $this->_redis->decr($key);
        if ($location < 0) {
            ApiException::throwException(ApiException::COIN_NOT_RECEIVE, '', array(__FUNCTION__.' fail'));
        }
        $res = $this->_redis->zRange('coin_split_list_'.$coinSn,$location,$location,true);

        if (empty($res)) {
            Core::write( '---message:    reids  get '.',---data:'.json_encode($res),Core::LEVEL_REDIS_ERROR);
            ApiException::throwException(ApiException::COIN_GET_FAIL, '', array(__FUNCTION__.' fail'));
        }
      decr         ,