我正在尝试在我的Laravel应用程序中实现一个非常基本的缓存机制。
我安装了Redis,通过终端(src/ redis -server)启动它,并在Laravel的配置文件中将缓存从文件更改为redis,但与使用缓存时的常规查询相比,它需要更长的时间(1svs2s)。
我是不是漏掉了什么?我只想缓存一个查询10分钟。
这是我的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 a new one
$companies = Cache::remember("companies",10, function() {
return Company::all();
});
Cache::put("companies", $companies, 10);
}
return view('index')->with('companies', $companies)
}我的观点
@foreach($companies as $company)
{{$company->name}}
@endforeach发布于 2015-10-03 10:05:00
首先,缓存并不总是更快。第二,你在反复检查缓存。
你可以用:
$companies = Cache::remember("companies",10, function() {
return Company::all();
});它检查缓存项是否存在,如果不存在,它将执行闭包并将结果缓存在指定的键中。cache:has / The是不必要的,只会减慢它的速度。
https://stackoverflow.com/questions/32920589
复制相似问题