Using Cache from Laravel

Asked

Viewed 263 times

0

I wonder if the way I am working with cache is correct and if you have something more practical for it.

Follows the code:

<?php
namespace App\Modules\Admin\Controllers;

use Cache;
use Auth;

class Network extends Controller
{
    public function teste()
    {
        if( ! Cache::has('user') ) {
            $user = Auth::user();
            Cache::store('file')->put('user', $user, 10);
        } else {
            $user = Cache::get('user');
        }

        return view('cms::user', [
            'user' => $user
        ]);
    }
}

Grateful for the understanding.

  • 1

    That’s right @Raank! , only changes a little when you want to use database data ! $value = Cache::get('key', function() {&#xA; return DB::table(...)->get();&#xA;}); that would be so!

  • I see no problem in using the cache, the only thing I would do is change the location where you do this check and apply the PSR-4 defaults.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.