Home >Backend Development >PHP Tutorial >After php set the session storage path to redis, php's fpm crashed.

After php set the session storage path to redis, php's fpm crashed.

WBOY
WBOYOriginal
2016-09-09 08:28:011048browse

<code><?php
namespace Session;

use SessionHandlerInterface;

class MySession implements SessionHandlerInterface {
    private $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()
    {
        $this->redis->quit();
        return true;
    }

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

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

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

    /**
     * Read session data
     * @since 5.4.0
     */
    public function read($session_id)
    {
        $this->connect();
        return $this->redis->get($session_id);
    }

    /**
     * Write session data
     * @since 5.4.0
     */
    public function write($session_id, $session_data)
    {
        $this->connect();
        $expire  =  configure('Ymf.Account.expire');
        if(is_int($expire) && $expire > 0) {
            $result = $this->redis->setex($session_id, $expire, $session_data);
            $re = $result ? 'true' : 'false';
        }else{
            $result = $this->redis->set($session_id, $session_data);
            $re = $result ? 'true' : 'false';
        }
        return $re;
    }
}</code>

This is my code, how to debug what went wrong

Reply content:

<code><?php
namespace Session;

use SessionHandlerInterface;

class MySession implements SessionHandlerInterface {
    private $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()
    {
        $this->redis->quit();
        return true;
    }

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

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

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

    /**
     * Read session data
     * @since 5.4.0
     */
    public function read($session_id)
    {
        $this->connect();
        return $this->redis->get($session_id);
    }

    /**
     * Write session data
     * @since 5.4.0
     */
    public function write($session_id, $session_data)
    {
        $this->connect();
        $expire  =  configure('Ymf.Account.expire');
        if(is_int($expire) && $expire > 0) {
            $result = $this->redis->setex($session_id, $expire, $session_data);
            $re = $result ? 'true' : 'false';
        }else{
            $result = $this->redis->set($session_id, $session_data);
            $re = $result ? 'true' : 'false';
        }
        return $re;
    }
}</code>

This is my code, how to debug what went wrong

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