最近在看緩存相關的內(nèi)容,現(xiàn)在有點搞不懂該怎么正確的使用緩存,在什么場景使用緩存。求各位大佬指點一下。我先舉個例子:假如現(xiàn)在我有一個查詢比較頻繁而且時間比較久,我想緩存這個查詢。未添加緩存的代碼:publicfunctiontest(array$array1,array$array2){returnTable1::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();}下面是我寫的添加了緩存的代碼,但是不知道對不對。publicfunctiontest(array$array1,array$array2){$str1=implode(",",sort($array1));$str2=implode(',',sort($array2));$test=\Cache::remember("test$str1$str2",10,function()use($array1,$array2){returnTable1::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;}像上述的情況應該怎么去緩存。還有就是應該在什么場景去使用緩存。求教....
小白求問:我想緩存一個使用率高且耗時久的查詢應該怎么做?大佬們有什么好的建議?
瀟瀟雨雨
2019-08-21 11:45:40