Skip to content

Instantly share code, notes, and snippets.

@alibo
Created November 23, 2016 17:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alibo/6400ea8c73b6c19bf04466468514b32f to your computer and use it in GitHub Desktop.
Save alibo/6400ea8c73b6c19bf04466468514b32f to your computer and use it in GitHub Desktop.
Using Redis Cluster with Normal Redis instances in Laravel
<?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']);
});
}
}
<?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);
}
}
<?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