php - predis set方法很慢很慢,这是什么原因?
ringa_lee
ringa_lee 2017-04-11 10:18:28
[PHP讨论组]

这是我的代码:

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

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

ringa_lee
ringa_lee

ringa_lee

全部回复(0)
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号