Monday 20 July 2015

Active Record Caching



While not "true" caching, Active Record enables you to save (or "cache") certain parts of your queries for reuse at a later point in your script's execution. Normally, when an Active Record call is completed, all stored information is reset for the next call. With caching, you can prevent this reset, and reuse information easily.
Cached calls are cumulative. If you make 2 cached select() calls, and then 2 uncached select() calls, this will result in 4 select() calls. There are three Caching functions available:

$this->db->start_cache()

 

This function must be called to begin caching. All Active Record queries of the correct type (see below for supported queries) are stored for later use.

$this->db->stop_cache()

 

This function can be called to stop caching.

$this->db->flush_cache()

 

This function deletes all items from the Active Record cache.
Here's a usage example:

$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');

//Generates: SELECT `field1`, `field2` FROM (`tablename`)

$this->db->flush_cache();

$this->db->select('field2');
$this->db->get('tablename');

//Generates: SELECT `field2` FROM (`tablename`)


Note: The following statements can be cached: select, from, join, where, like, group_by, having, order_by, set

No comments:

Post a Comment