1. 程式人生 > >【thinkphp5操作redis系列教程】connect(或open)連線

【thinkphp5操作redis系列教程】connect(或open)連線

1.連線函式

    /**
     * Connects to a Redis instance.
     *
     * @param string    $host       can be a host, or the path to a unix domain socket
     * @param int       $port       optional
     * @param float     $timeout    value in seconds (optional, default is 0.0 meaning unlimited)
     * @param null      $reserved   should be null if $retry_interval is specified
     * @param int       $retry_interval  retry interval in milliseconds.
     * @return bool                 TRUE on success, FALSE on error.
     */
    public function connect( $host, $port = 6379, $timeout = 0.0, $reserved = null, $retry_interval = 0 ) {}

2.預設埠連線

<?php
namespace app\index\controller;
use Redis;
class Index
{
    public function index()
    {
        $redis = new Redis();
        $con = $redis->connect('127.0.0.1');
        var_dump($con);//bool(true)

    }

}

3.埠連線(預設埠6379)

<?php
namespace app\index\controller;
use Redis;
class Index
{
    public function index()
    {
        $redis = new Redis();
        $con = $redis->connect('127.0.0.1',6379);
        var_dump($con);//bool(true)

    }

}

4.超時放棄連線

<?php
namespace app\index\controller;
use Redis;
class Index
{
    public function index()
    {
        $redis = new Redis();
        //預設為0,即連線沒有限制,此處為短連線,超過3秒放棄連線
        $con = $redis->connect('127.0.0.1',6379,3);
        var_dump($con);//bool(true)

    }

}

備註:open()用法同connect()