In this tutorial you will learn about the CodeIgniter Active Record Caching and its application with practical example.
What Is Active Record Caching In CodeIgniter?
Active Record caching helps you to save some parts of your database queries so that you can reuse them at a later point in your script’s execution. Usually, when a query is executed all of the query related information is reset for the next query. Active record caching gives you way to prevent this reset, and reuse query information easily. Cached calls works in cumulative fashion, making 2 cached select() calls, and then 2 uncached select() calls, will result in 4 select() calls.
In CodeIgniter, we have following three active record caching functions –
$this->db->start_cache()
This function is used to start query caching.
$this->db->stop_cache()
This function is used to stop query caching.
$this->db->flush_cache()
This function is used to delete all items from the Active Record cache.
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
$this->db->start_cache() $this->db->stop_cache() $this->db->flush_cache() $this->db->start_cache(); $this->db->select('field1'); $this->db->stop_cache(); $this->db->get('tablename'); //Generates: SELECT `field1` FROM (`tablename`) $this->db->select('field2'); $this->db->get('tablename'); |
Note:- The following statements can be cached: select, from, join, where, like, group_by, having, order_by, set