Jul 6, 2016 | laravel, laravel 5.3

The new cache() global helper in Laravel 5.3

Series

This is a series of posts on New Features in Laravel 5.3.

!
Warning: This post is over a year old. I don't always update old posts with new information, so some of this information may be out of date.

Time for another new feature in Laravel 5.3!

Shortcut global helpers in Laravel

As I was writing my book I noticed a pattern in the global helper functions like session() and, in some ways, cookie(). There are three primary functions that they can perform: get a value, put a value, or return an instance of their backing service.

For example:

  • session('abc', null) gets the value of abc, or an optional fallback of null.
  • session(['abc' => 'def']) sets the value of abc to def.
  • session() returns an instance of the SessionManager.

The third option means you can use session()->all() (or any other methods) just like you would Session::all().

I mentioned that it seems like there should be a cache() helper, and before I could even think much more about it, Jeffrey (Way) had already written one up. So! Behold! The global cache() helper, new in Laravel 5.3.

The cache() global helper

Like session(), the cache() global helper can perform three primary functions: get, put, or return an instance of the backing service.

For example:

  • cache('abc', null) gets the cached value of abc, or an optional fallback of null.
  • cache(['abc' => 'def'], 5) sets the value of abc to def, for the duration of 5 minutes.
  • cache() returns an instance of the CacheManager.

The third option means you can use cache()->forever() (or any other methods) just like you would Cache::forever().

That's it! Enjoy!


Comments? I'm @stauffermatt on Twitter


Tags: laravel  •  laravel 5.3


This is part of a series of posts on New Features in Laravel 5.3:

  1. Jun 16, 2016 | laravel, laravel 5.3, echo, websockets
  2. Jun 27, 2016 | laravel, laravel 5.3
  3. Jun 29, 2016 | laravel, laravel 5.3, eloquent
  4. Jul 6, 2016 | laravel, laravel 5.3
  5. Jul 8, 2016 | laravel, laravel 5.3, eloquent
  6. Jul 25, 2016 | laravel, laravel 5.3
  7. Jul 26, 2016 | laravel, laravel 5.3
  8. Jul 27, 2016 | laravel, laravel 5.3, routing
  9. Jul 27, 2016 | laravel, laravel 5.3, laravel scout, laravel passport, mailable
  10. Jul 29, 2016 | laravel, laravel 5.3, laravel scout
  11. Jul 30, 2016 | laravel, laravel 5.3, laravel passport, oauth
  12. Aug 5, 2016 | laravel, laravel 5.3, mail, laravel mailables
  13. Aug 8, 2016 | laravel, laravel 5.3
  14. Oct 19, 2016 | laravel, laravel 5.3, laravel notifications, notifications
  15. Dec 21, 2016 | laravel, laravel 5.3, vuejs, vueify, authorization
  16. Dec 21, 2016 | laravel, laravel 5.3, queues
  17. Jan 30, 2017 | laravel, laravel 5.3, artisan

Subscribe

For quick links to fresh content, and for more thoughts that don't make it to the blog.