resq/src/Service/RedisClientProvider.php

56 lines
1.2 KiB
PHP

<?php
namespace App\Service;
use Predis\Client as PredisClient;
class RedisClientProvider
{
protected $redis;
protected $scheme;
protected $host;
protected $port;
protected $password;
public function __construct($scheme, $host, $port, $password)
{
$this->scheme = $scheme;
$this->host = $host;
$this->port = $port;
$this->password = $password;
$this->redis = null;
$this->connect();
}
protected function connect()
{
// already connected
if ($this->redis != null)
return $this->redis;
// if password is specified attempt connection
if (strlen($this->password) > 0)
{
$this->redis = new PredisClient([
"scheme" => $this->scheme,
"host" => $this->host,
"port" => $this->port,
"password" => $this->password]);
return $this->redis;
}
$this->redis = new PredisClient([
"scheme" => $this->scheme,
"host" => $this->host,
"port" => $this->port]);
return $this->redis;
}
public function getRedisClient()
{
return $this->redis;
}
}