Laravel - Cache - Retrieve Store

Sometimes you may wish to retrieve an item from the cache, but also store a default value if the requested item doesn't exist. For example, you may wish to retrieve all users from the cache or, if they don't exist, retrieve them from the database and add them to the cache. You may do this using the Cache::remember method:

    
    $value = Cache::remember('users', $seconds, function () {
        return DB::table('users')->get();
    });
	

If the item does not exist in the cache, the closure passed to the remember method will be executed and its result will be placed in the cache.

You may use the rememberForever method to retrieve an item from the cache or store it forever if it does not exist:

    
    $value = Cache::rememberForever('users', function () {
        return DB::table('users')->get();
    });