【问题标题】:PHP connection to RedisPHP 连接到 Redis
【发布时间】:2018-05-19 19:50:54
【问题描述】:

我正在使用 phpredis 连接到 Redis 服务器 (https://github.com/phpredis/phpredis)。我为我的应用程序编写了一个非常基本的 redis 客户端,它应该只接受 env 参数并连接到 redis 服务器。我的 redis 客户端如下所示:

<?php

use \Redis

class Redis
{
    /** @var Redis */
    private $redis;


    /**
     * @param RedisServer $redis
     * @param string $redisHost
     * @param string $redisPort
     */
      public function __construct(RedisServer $redis, string $redisHost, string $redisPort)
      {
          $this->redis = $redis;
          if (!$this->redis->connect($redisHost, $redisPort)) {
              throw new ConnectionException('Failed to connection to redis server', 500);
          }
      }

      /**
       * Close connection when Redis service is destroyed
       */
      public function __destruct()
      {
          $this->redis->close();
          $this->redis = null;
      }

      //....

}

是否需要在 __destruct() 中显式关闭 Redis 连接?

提前致谢!

【问题讨论】:

    标签: php redis phpredis


    【解决方案1】:

    你是我的男人 :) 是的,不是的!使用 __destruct() 是保存(如在 java 中)关闭连接的安全方法。在 php 中这工作不同。远程连接(如 db、fs...)将在调用自定义析构函数之前关闭。但是对于未来:继续保持下去!它是一种正确的方法,“启动”不是瓶颈,但是:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-22
      • 1970-01-01
      • 2021-03-13
      • 2020-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多