06.10.2016 Views

laravel-5

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Cache 283<br />

13 *<br />

14 * @return void<br />

15 */<br />

16 public function boot()<br />

17 {<br />

18 Cache::extend('mongo', function($app) {<br />

19 return Cache::repository(new MongoStore);<br />

20 });<br />

21 }<br />

22<br />

23 /**<br />

24 * Register bindings in the container.<br />

25 *<br />

26 * @return void<br />

27 */<br />

28 public function register()<br />

29 {<br />

30 //<br />

31 }<br />

32 }<br />

The first argument passed to the extend method is the name of the driver. This will correspond to<br />

your driver option in the config/cache.php configuration file. The second argument is a Closure<br />

that should return an Illuminate\Cache\Repository instance. The Closure will be passed an $app<br />

instance, which is an instance of the service container.<br />

The call to Cache::extend could be done in the boot method of the default App\Providers\AppServiceProvider<br />

that ships with fresh Laravel applications, or you may create your own service provider to house<br />

the extension - just don’t forget to register the provider in the config/app.php provider array.<br />

To create our custom cache driver, we first need to implement the Illuminate\Contracts\Cache\Store<br />

contract contract. So, our MongoDB cache implementation would look something like this:<br />

1

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!