Welcome Guest, Not a member yet? Register   Sign In
CodeIgniter 4 Query Caching
#1

Hi all.
I'm trying to upgrade CI3 to CI4, and I have to to use query caching in CI3 to reuse my query.
Here is my code:

$sql = $this->db->start_cache()
->select(<<some_items>>)
->from(<<items_table>>)
->where(<<condition>>)
->group_by(<<group_by_condition>>)
->order_by(<<order_by_condition>>)
->stop_cache();

$total = $sql->get()->num_rows();

$sql= $this->db->limit(10, 10)->get();

$data = $sql->result_array();
$total_per_limit = $sql->num_rows();

$this->db->flush_cache();

How can I do it in CI4?

Thanks in advance for help.
Reply
#2

$result= $this->builder() .... ->get()->getResultArray();



save cache

  cache()->save("{$cachename}", $found, 300);



load cache

$result= cache("{$cachename}")


... function ...

if ( null === ($result= cache("{$cachename}")))
{

  $result= $this->builder() .... ->get()->getResultArray(); // do query

  cache()->save("{$cachename}", $found, 300); //save to cache

}

return $result;
Reply




Theme © iAndrew 2016 - Forum software by © MyBB