-
-
Save tortuetorche/979683e86fa97103bdce to your computer and use it in GitHub Desktop.
Revisions
-
tillkruss created this gist
Aug 23, 2015 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,72 @@ <?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 hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1 @@ Add `App\Providers\RedisServiceProvider::class` to `'providers'` in `config/app.php`. This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,31 @@ <?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 hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,33 @@ <?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']); }); } }