最近在看緩存相關的內容,現在有點搞不懂該怎么正確的使用緩存,在什么場景使用緩存。求各位大佬指點一下。我先舉個例子:假如現在我有一個查詢比較頻繁而且時間比較久,我想緩存這個查詢。 未添加緩存的代碼: public function test(array $array1, array $array2)
{ return Table1::select('a', 'b','c')
->with([ 'with1' => function ($query) use($array1) {
$query->select('c', 'd')
->join('table2', 'table2.c', '=', 'table1.c')
->where($array1);
}
])
->whereIn('a', $array2)
->get();
}下面是我寫的添加了緩存的代碼,但是不知道對不對。public function test(array $array1, array $array2)
{
$str1 = implode(",", sort($array1));
$str2 = implode(',', sort($array2));
$test = \Cache::remember("test$str1$str2",10, function () use ($array1, $array2) { return Table1::select('a', 'b','c')
->with([ 'with1' => function ($query) use($array1) {
$query->select('c', 'd')
->join('table2', 'table2.c', '=', 'table1.c')
->where($array1);
}
])
->whereIn('a', $array2)
->get();
}); return $test;
}像上述的情況應該怎么去緩存。還有就是應該在什么場景去使用緩存。求教....
- 2 回答
- 0 關注
- 459 瀏覽
添加回答
舉報
0/150
提交
取消