PHP

搭配ccORM的redis連接操作類代碼

字號+ 編輯: 国内TP粉 修訂: 听风就是我 來源: 原创 2023-09-11 我要說兩句(0)

搭配orm的Redis操作庫代碼。需要搭配Yaf框架。

Redis连接类代码, 注意, 本代码是阻塞的,需要搭配Yaf配置文件。

class Redis_Worker
    {
        /**
         * 业务逻辑用长连接线程
         * @var  array
         */
        private static $_redisp = [];
 
        /**
         * 启动连接
         * @access  public   static
         * @param   integer  $dbIndex
         * @return  object
         */
        public static function connect(int $dbIndex = 0)
        {
            $redis = new \Redis();
            $config = Yaf\Registry::get('config')->redis;
            $redis->connect($config->host, (int) $config->port);
            $redis->auth($config->auth);
            $redis->select($dbIndex);
            return $redis;
        }
 
        /**
         * 长连接句柄
         * 根据库序号产生多个句柄容器
         * @access  public
         * @param   integer  $dbIndex  库号
         * @return  object
         */
        public static function pconnect(int $dbIndex = 0)
        {
            if (!isset(self::$_redisp[$dbIndex])) {
                self::$_redisp[$dbIndex] = new \Redis();
            }
            $redis = &self::$_redisp[$dbIndex];
            $config = Yaf\Registry::get('config')->redis;
            $redis->pconnect($config->host, (int) $config->port);
            $redis->auth($config->auth);
            $redis->select($dbIndex);
            return self::$_redisp[$dbIndex];
        }
    }

为什么不封装其他数据操作方法?

简直多余。不想浪费别人脑细胞,phpredis库本身已经封装的很好了, 还封装个屁啊。。。

閲完此文,您的感想如何?
  • 有用

    0

  • 沒用

    0

  • 開心

    0

  • 憤怒

    0

  • 可憐

    0

1.如文章侵犯了您的版權,請發郵件通知本站,該文章將在24小時内刪除;
2.本站標注原創的文章,轉發時煩請注明來源;
3.交流群: PHP+JS聊天群

相關課文
  • mac開發接入微信公衆號接口返回報錯 cURL error 56: SSLRead() return error -9806

  • pecl安裝程序時報錯Array and string offset access syntax with curly braces is no longer supported

  • PHP的換行符是什麽

  • 由於商家傳入的H5交易參數有誤,該筆交易暫時無法完成,請聯系商家解決

我要說說
網上賓友點評