The new cache() global helper in Laravel 5.3

Posted on July 06, 2016

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
    Laravel Echo is a tool that makes it easy for you to bring the power of WebSockets to your Laravel applications. It simplifie...
  2. Jun 27, 2016 | laravel, laravel 5.3
    Let's take a look at another new feature that's coming in Laravel 5.3. What are Blade directives? Laravel's Blade templating ...
  3. Jun 29, 2016 | laravel, laravel 5.3, eloquent
    More Laravel 5.3 goodies! This time, it's an update to the Eloquent firstOrCreate method. If you've never used it before, you...
  4. Jul 6, 2016 | laravel, laravel 5.3
    Time for another new feature in Laravel 5.3! Shortcut global helpers in Laravel As I was writing my book I noticed a pattern ...
  5. Jul 8, 2016 | laravel, laravel 5.3, eloquent
    While Laravel has had the ability to cast your data to and from JSON since version 5.0, it was previously just a convenience&...
  6. Jul 8, 2016 | laravel, laravel 5.3
    If you want to filter a Laravel collection to only those records which meet particular criteria, you're most likely going to ...
  7. Jul 25, 2016 | laravel, laravel 5.3
    In Laravel 5.3, we have a new validation option: image dimensions for image uploads. The validation rule is called dimensions...
  8. Jul 26, 2016 | laravel, laravel 5.3
    Laravel's pagination library is brilliant, because pagination is a common task that is a surprising amount of work to impleme...
  9. Jul 27, 2016 | laravel, laravel 5.3, laravel scout, laravel passport, mailable
    THIS POST IS STILL IN ACTIVE PROGRESS Once the videos are released I'll be able to update this post with more info, since I w...
  10. Jul 27, 2016 | laravel, laravel 5.3, routing
    The last few versions of Laravel have showed the way routing works shifting around a bit. This is usually a sign that we're f...
  11. Jul 29, 2016 | laravel, laravel 5.3, laravel scout
    Search tools ElasticSearch and Algolia have gained a lot of popularity in the Laravel community in the last few years as powe...
  12. Jul 30, 2016 | laravel, laravel 5.3, laravel passport, oauth
    API authentication can be tricky. OAuth 2 is the reigning ruler of the various standards that you might consider, but it's co...
  13. Aug 5, 2016 | laravel, laravel 5.3, mail, laravel mailables
    For the longest time, sending mail in Laravel has felt clumsy compared to the relatively light APIs of most other Laravel fea...
  14. Aug 8, 2016 | laravel, laravel 5.3
    We've already covered the new routes directory and the changes that will make to the directory structure. But there's one oth...
  15. Oct 19, 2016 | laravel, laravel 5.3, laravel notifications, notifications
    In a previous blog post I've covered Laravel's new Mailable feature, which introduces some important changes to how we send m...
  16. Dec 21, 2016 | laravel, laravel 5.3, queues
    Queues are one of those tools in Laravel that everyone knows is there, but very few people understand deeply. It's understand...
  17. Dec 21, 2016 | laravel, laravel 5.3, vuejs, vueify, authorization
    In Laravel 5.3, it's easier than ever to write and use Vue components out of the box. This means 5.3 has a somewhat more opin...
  18. Jan 30, 2017 | laravel, laravel 5.3, artisan
    Before Laravel 5.3, defining an Artisan console command—something like php artisan sync:dates—required you to create a ne...

Subscribe

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