Laravel 5.1 Redis cache -
i'm trying implement basic caching mechanism laravel app.
i installed redis, started via terminal (src/redis-server) , changed cache file redis in laravel's config file, takes longer (1s vs 2s) regular query when use cache.
am missing here? want cache query 10 minutes.
here's feedcontroller.php
namespace app\http\controllers\frontend\feed; use illuminate\http\request; use auth; use app\http\requests; use app\http\controllers\controller; use app\models\company; use redis; use cache; class feedcontroller extends controller { public function index() { if (cache::has('companies')) { // cache exists. $companies = cache::get("companies"); } else { // cache doesn't exist, create new 1 $companies = cache::remember("companies",10, function() { return company::all(); }); cache::put("companies", $companies, 10); } return view('index')->with('companies', $companies) }
my view
@foreach($companies $company) {{$company->name}} @endforeach
first of all, caching isn't faster. second, you're double checking cache.
you can use:
$companies = cache::remember("companies",10, function() { return company::all(); });
it checks of cache item exist, , if not execute closure , cache result in specified key. cache:has if/else unnecessary , slow down.
Comments
Post a Comment