06.10.2016 Views

laravel-5

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Cache 284<br />

10 public function decrement($key, $value = 1) {}<br />

11 public function forever($key, $value) {}<br />

12 public function forget($key) {}<br />

13 public function flush() {}<br />

14 public function getPrefix() {}<br />

15 }<br />

We just need to implement each of these methods using a MongoDB connection. Once our<br />

implementation is complete, we can finish our custom driver registration:<br />

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

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

3 });<br />

Once your extension is complete, simply update your config/cache.php configuration file’s driver<br />

option to the name of your extension.<br />

If you’re wondering where to put your custom cache driver code, consider making it available on<br />

Packagist! Or, you could create an Extensions namespace within your app directory. However, keep<br />

in mind that Laravel does not have a rigid application structure and you are free to organize your<br />

application according to your preferences.<br />

Events<br />

To execute code on every cache operation, you may listen for the events fired by the cache. Typically,<br />

you should place these event listeners within your EventServiceProvider:<br />

1 /**<br />

2 * The event listener mappings for the application.<br />

3 *<br />

4 * @var array<br />

5 */<br />

6 protected $listen = [<br />

7 'Illuminate\Cache\Events\CacheHit' => [<br />

8 'App\Listeners\LogCacheHit',<br />

9 ],<br />

10<br />

11 'Illuminate\Cache\Events\CacheMissed' => [

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

Saved successfully!

Ooh no, something went wrong!