yiisoft / yii2-redis

Yii 2 Redis extension.
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
452 stars 183 forks source link

Why set redirectConnectionString = null in function redirect #228

Closed yangyang880402 closed 3 years ago

yangyang880402 commented 3 years ago

Why set redirectConnectionString to null in function redirect,when the command is complete yii/redis/Connection

/**
     * @param string $redirect
     * @param string $command
     * @param array $params
     * @return mixed
     * @throws Exception
     * @throws SocketException
     */
    private function redirect($redirect, $command, $params)
    {
        $responseParts = preg_split('/\s+/', $redirect);

        $this->redirectConnectionString = ArrayHelper::getValue($responseParts, 2);

        if ($this->redirectConnectionString) {
            \Yii::info('Redirecting to ' . $this->connectionString, __METHOD__);

            $this->open();

            $response = $this->sendCommandInternal($command, $params);

            $this->redirectConnectionString = null;

            return $response;
        }

        throw new Exception('No hostname found in redis redirect (MOVED): ' . VarDumper::dumpAsString($redirect));
    }

e.g.

        $ret = \Yii::$app->redis->setex("test", 100, "test"); // true
        $test = \Yii::$app->redis->get("test"); // connection is localhost:6379 ,result is null
samdark commented 3 years ago

It was added in https://github.com/yiisoft/yii2-redis/pull/178, commit is https://github.com/yiisoft/yii2-redis/pull/178/commits/c868cc6b8d256e0b5cc22a50923cc174f4b6e747. @hofrob should know for sure.

hofrob commented 3 years ago

Hi,

This was a long time ago, but I'll try: The cluster will redirect you every single time you're doing something with redis and may redirect you to different servers every time. So as soon as you're done, the redirectConnectionString has to be discarded. It's only used temporarily while performing something on the cluster. If you don't discard this value getConnectionString() would return the same "redirect" connection every time.

As far as I remember one should always ask the master first before performing an action on the cluster.

Are you running these commands on a cluster? Which settings are you using in your cache config? See getIsCluster() and forceClusterMode.

This example is not really saying a lot to be honest. You're running \Yii::$app->redis->setex and then $redis->get. What is $redis?

yangyang880402 commented 3 years ago

I have a scene, I need in one request operation redis for many times, if according to your description, I will need to connect redis service for many times. But I don't want to connect many times in the same request. It's performance is too low.

hofrob commented 3 years ago

You will need to provide more info. As mentioned in my previous post.

hofrob commented 3 years ago

To add to that: Connection are not closed and reopened. They're kept in a pool: https://github.com/yiisoft/yii2-redis/blob/ead6d1f1a99c171449e42fe0e00bfc3fcf3a27a6/src/Connection.php#L563

yangyang880402 commented 3 years ago

e.g. $redis = \Yii::$app->redis; $ret = $redis->get("token"); if(!$ret ){ $ret = $redis->setex("token", 100, "token123"); }

$ret = $redis->setex("key1"); ........ $ret = $redis->setex("key2"); ........ $ret = $redis->setex("key2");

yangyang880402 commented 3 years ago

To add to that: Connection are not closed and reopened. They're kept in a pool:

This seems to be invalid

hofrob commented 3 years ago

As long as you don't answer the questions above, post your config, tell us your setup and write a consistent reproducible understandable bug report, I really don't know how to help you.

yii-bot commented 3 years ago

It has been 2 or more weeks with no response on our request for more information. In order for our issue tracker to be effective, we are closing this issue.

If you want it to be reopened again, feel free to supply us with the requested information.

Thanks!

This is an automated comment, triggered by adding the label expired.