CodeIgniter Forums
How to reuse a query - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Model-View-Controller (https://forum.codeigniter.com/forumdisplay.php?fid=10)
+--- Thread: How to reuse a query (/showthread.php?tid=80489)



How to reuse a query - JerryCode - 11-08-2021

The user table has 20 rows.
PHP Code:
//1.create a query
$result $this->news_model->where('id>',10);//10 results
//2.get the count
$count = $result->countAllResults(); //10 results
//3.return the result(There should be only 10 results)
$all = $result->findAll(); //20 results 
How to reuse the query ?

My solution is to use caching

cache()->save('result',$result); // save cache

$count = cache(‘result’)->countAllResults(); //10 results, use cache

$all = $result->findAll(); //10 results , if use pagination, can not use cache data here

cache()->delete('result'); //delete cache