• 技术文章 >后端开发 >php教程

    predis set方法很慢很慢,这是什么原因?

    2016-09-11 11:34:15原创717

    这是我的代码:

    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;
            }
        }
    }

    排查只会,发现这个$this->redis->set方法特别慢,什么原因。。。。

    回复内容:

    这是我的代码:

    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;
            }
        }
    }

    排查只会,发现这个$this->redis->set方法特别慢,什么原因。。。。

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    专题推荐:php
    上一篇:php $_SESSION好像是个引用? 下一篇:自己动手写 PHP MVC 框架(40节精讲/巨细/新人进阶必看)

    相关文章推荐

    • PHP反序列化入门总结(小白必看)• 一文详解PHP用流方式实现下载文件(附代码示例)• PHP原生类的总结分享• 聊聊PHP escapeshellarg函数使用的中文问题• 分享PHP函数使用小工具(附代码示例)
    1/1

    PHP中文网