Add App\Providers\RedisServiceProvider::class
to 'providers'
in config/app.php
.
-
-
Save it-can/be7e9d5ac55045709dd5 to your computer and use it in GitHub Desktop.
[Laravel 5.1] Use Redis PECL/HHVM extension
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\Repositories; | |
use Illuminate\Cache\Repository; | |
class CacheRepository extends Repository | |
{ | |
public function has($key) | |
{ | |
$value = $this->get($key); | |
return ! is_null($value) && $value !== false; | |
} | |
public function get($key, $default = null) | |
{ | |
$value = $this->store->get($key); | |
if (is_null($value) || $value === false) { | |
$this->fireCacheEvent('missed', [$key]); | |
$value = value($default); | |
} else { | |
$this->fireCacheEvent('hit', [$key, $value]); | |
} | |
return $value; | |
} | |
public function add($key, $value, $minutes) | |
{ | |
if (method_exists($this->store, 'add')) { | |
return $this->store->add($key, $value, $this->getMinutes($minutes)); | |
} | |
$currentValue = $this->get($key); | |
if (is_null($currentValue) || $currentValue === false) { | |
$this->put($key, $value, $minutes); | |
return true; | |
} | |
return false; | |
} | |
public function remember($key, $minutes, Closure $callback) | |
{ | |
$value = $this->get($key); | |
if (! is_null($value) && $value !== false) { | |
return $value; | |
} | |
$this->put($key, $value = $callback(), $minutes); | |
return $value; | |
} | |
public function rememberForever($key, Closure $callback) | |
{ | |
$value = $this->get($key); | |
if (! is_null($value) && $value !== false) { | |
return $value; | |
} | |
$this->forever($key, $value = $callback()); | |
return $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 | |
namespace App\Services; | |
use Redis; | |
use Illuminate\Redis\Database; | |
use Illuminate\Contracts\Redis\Database as DatabaseContract; | |
class RedisDatabase extends Database implements DatabaseContract | |
{ | |
protected function createSingleClients(array $servers, array $options = []) | |
{ | |
$clients = []; | |
$servers = array_except($servers, ['cluster']); | |
foreach ($servers as $key => $server) { | |
$redis = new Redis(); | |
$redis->connect($server['host'], $server['port']); | |
if (isset($server['password'])) { | |
$redis->auth($server['password']); | |
} | |
$redis->select($server['database']); | |
$clients[$key] = $redis; | |
} | |
return $clients; | |
} | |
} |
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 Cache; | |
use Redis; | |
use App\Repositories\CacheRepository; | |
use App\Services\RedisDatabase; | |
use Illuminate\Cache\RedisStore; | |
use Illuminate\Redis\RedisServiceProvider as ServiceProvider; | |
class RedisServiceProvider extends ServiceProvider | |
{ | |
public function boot() | |
{ | |
Cache::extend('redis', function ($app) { | |
$repository = new CacheRepository(new RedisStore($app->make('redis'))); | |
if ($app->bound('Illuminate\Contracts\Events\Dispatcher')) { | |
$repository->setEventDispatcher($app['Illuminate\Contracts\Events\Dispatcher']); | |
} | |
return $repository; | |
}); | |
} | |
public function register() | |
{ | |
$this->app->singleton('redis', function ($app) { | |
return new RedisDatabase($app['config']['database.redis']); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment