Created
November 23, 2016 17:58
-
-
Save alibo/6400ea8c73b6c19bf04466468514b32f to your computer and use it in GitHub Desktop.
Using Redis Cluster with Normal Redis instances in Laravel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Providers; | |
use Illuminate\Support\ServiceProvider; | |
use Illuminate\Redis\Database; | |
class AppServiceProvider extends ServiceProvider | |
{ | |
/** | |
* Bootstrap any application services. | |
* | |
* @return void | |
*/ | |
public function boot() | |
{ | |
// | |
} | |
/** | |
* Register any application services. | |
* | |
* @return void | |
*/ | |
public function register() | |
{ | |
$this->app->singleton('redis-cluster', function ($app) { | |
return new Database($app['config']['redis-cluster']); | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App; | |
class Example | |
{ | |
private $redis; | |
public function __construct() { | |
$this->redis = app('redis-cluster'); | |
} | |
public function get($key) { | |
return $this->redis->get($key); | |
} | |
public function set($key, $value) { | |
return $this->redis->set($key, $value); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// config/redis-cluster.php | |
return [ | |
'cluster' => true, | |
'options' => [ | |
'cluster' => 'redis', | |
], | |
'master-1' => [ | |
'host' => 'master1.example.com', | |
'port' => 6380, | |
'alias' => 'master-1', | |
], | |
'master-2' => [ | |
'host' => 'master2.example.com', | |
'port' => 6381, | |
'alias' => 'master-2', | |
], | |
'master-3' => [ | |
'host' => 'master3.example.com', | |
'port' => 6382, | |
'alias' => 'master-3', | |
], | |
'slave-1' => [ | |
'host' => 'slave1.example.com', | |
'port' => 7380, | |
'alias' => 'slave-1', | |
], | |
'slave-2' => [ | |
'host' => 'slave2.example.com', | |
'port' => 7381, | |
'alias' => 'slave-2', | |
], | |
'slave-3' => [ | |
'host' => 'slave3.example.com', | |
'port' => 7382, | |
'alias' => 'slave-3', | |
], | |
]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment