Home >Backend Development >PHP Tutorial >The predis set method is very slow. What is the reason?

The predis set method is very slow. What is the reason?

WBOY
WBOYOriginal
2016-09-11 11:34:151207browse

This is my code:

<code><?php
/**
 * User: mh
 * Date: 2016/9/9
 * Time: 19:12
 */

namespace Session;

use SessionHandlerInterface;

class RedisSession implements SessionHandlerInterface {
    private $redis;
    // 连接Redis
    private function connect() {
        if(!$this->redis) {
            $cfg = [
                'scheme' => env('REDIS_SCHEME', 'tcp'),
                'host' => env('REDIS_HOST', '127.0.0.1'),
                'port' => env('REDIS_PORT', 6379)
            ];
            $this->redis = new \Predis\Client($cfg);
        }
    }
    /**
     * Close the session
     * @since 5.4.0
     */
    public function close()
    {
        return true;
    }

    /**
     * Destroy a session
     * @return boolean
     * @since 5.4.0
     */
    public function destroy($session_id)
    {
        $this->connect();
        $this->redis->del($session_id);
        return true;
    }

    /**
     * Cleanup old sessions
     * @return boolean
     * @since 5.4.0
     */
    public function gc($maxlifetime)
    {
        return true;
    }

    /**
     * Initialize session
     * @since 5.4.0
     */
    public function open($save_path, $sessionName)
    {
        return true;
    }

    /**
     * Read session data
     * @return string
     * @since 5.4.0
     */
    public function read($session_id)
    {
        $this->connect();
        if($this->redis->exists($session_id)) {
            $value = $this->redis->get($session_id);
            return $value ? ($value) : '';
        } else {
            $expire = configure('Ymf.Account.expire');
            $this->redis->setex($session_id, $expire, '');
            return '';
        }
        return '';
    }

    /**
     * Write session data
     * @return boolean
     * @since 5.4.0
     */
    public function write($session_id, $sessionData)
    {
        $this->connect();
        file_put_contents(__DIR__ . '/../../write.log', "写入{$sessionData}\r\n", FILE_APPEND);
        if($this->redis->set("flf", "hfl")) {
            return true;
        } else {
            return false;
        }
    }
}</code>

After troubleshooting, I found that the $this->redis->set method is particularly slow. Why? . . .

Reply content:

This is my code:

<code><?php
/**
 * User: mh
 * Date: 2016/9/9
 * Time: 19:12
 */

namespace Session;

use SessionHandlerInterface;

class RedisSession implements SessionHandlerInterface {
    private $redis;
    // 连接Redis
    private function connect() {
        if(!$this->redis) {
            $cfg = [
                'scheme' => env('REDIS_SCHEME', 'tcp'),
                'host' => env('REDIS_HOST', '127.0.0.1'),
                'port' => env('REDIS_PORT', 6379)
            ];
            $this->redis = new \Predis\Client($cfg);
        }
    }
    /**
     * Close the session
     * @since 5.4.0
     */
    public function close()
    {
        return true;
    }

    /**
     * Destroy a session
     * @return boolean
     * @since 5.4.0
     */
    public function destroy($session_id)
    {
        $this->connect();
        $this->redis->del($session_id);
        return true;
    }

    /**
     * Cleanup old sessions
     * @return boolean
     * @since 5.4.0
     */
    public function gc($maxlifetime)
    {
        return true;
    }

    /**
     * Initialize session
     * @since 5.4.0
     */
    public function open($save_path, $sessionName)
    {
        return true;
    }

    /**
     * Read session data
     * @return string
     * @since 5.4.0
     */
    public function read($session_id)
    {
        $this->connect();
        if($this->redis->exists($session_id)) {
            $value = $this->redis->get($session_id);
            return $value ? ($value) : '';
        } else {
            $expire = configure('Ymf.Account.expire');
            $this->redis->setex($session_id, $expire, '');
            return '';
        }
        return '';
    }

    /**
     * Write session data
     * @return boolean
     * @since 5.4.0
     */
    public function write($session_id, $sessionData)
    {
        $this->connect();
        file_put_contents(__DIR__ . '/../../write.log', "写入{$sessionData}\r\n", FILE_APPEND);
        if($this->redis->set("flf", "hfl")) {
            return true;
        } else {
            return false;
        }
    }
}</code>

After troubleshooting, I found that the $this->redis->set method is particularly slow. Why? . . .

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn