Codeigniter Database Caching Class


Codeigniter Database Caching Class – This database reference provides many functions that are used to caching the database results, this class permits you to cache your queries as text files for reducing the database load. This class is automatically loaded by the database driver when caching is enable. Do not load this class manually. Here in this tutorial, we are going to explain how to use Database Caching Class.


Codeigniter Database Caching Class | Example.

Let us understand how Database Caching Class works in codeigniter with examples.

Functions:-

There are following functions available in Database Caching Class. Now we will explain one by one.

  1. Enabling caching.
  2. How does caching work.
  3. How are cache files stored?.
  4. Managing your cache files.
  5. Function reference.

1. Enabling caching.

Caching is enabled in three steps:-

  • Create a writable on your server where the cache file can be stored.
  • Set the path to your cache folder in your application/config/database.php file.
  • Enable the caching feature, or globally by setting the preference in your application/config/database.php file.

Once enabled, caching will happened automatically whenever a page is loaded than contain database queries.

2. How Does Caching Work?.

Codeigniter query caching system happen dynamically when your page is viewed. When caching is enabled, the first time a web page is loaded, the query result will be serialized and stored in a text file on your server. Next time page is loaded the cache file will be used instead of accessing your database.

Only read type(SELECT) queries can be cached since these are the only type of queries that produce a result. Write type(INSERT, UPDATE, etc..) queries, since they don’t generate the result, will not be generated by the system.

Cache file DO NOT expire until you delete them.

3. How are Cache Files Stored?.

Codeigniter place the result of EACH query into in own cache file. Set of cache file are further organized into sub-folders corresponding to your controller function. To be precise, the sub-folder are named identically to the first two segment of your URI.

For example, let’s say you have a controller called blog with the function called comments that contain three queries. The caching system will create a cache folder called blog+comments, into which it will write three cache file.

4. Managing your Cache Files.

Cache file does not expire, you will need to build deletion routines into your application.

Not all database function working with caching.

The following functions are not available when using a cached result object.

  • num_fields()
  • fields_name()
  • field_data()
  • free_result()

The two database resource(result_id and conn_id) are not available when caching, since result resource only belong to run-time operations.

5. Function Reference.

Here is the simple demo of function reference

$this->db->cache_on()/$this->db->cache_on()

It is used to enables/disables caching.

$this->db->cache_on();
$query = $this->db->query("SELECT * FROM blog");

// Turn caching off for this one query
$this->db->cache_off();
$query = $this->db->query("SELECT * FROM blog WHERE member_id = '$current_user'");

// Turn caching back on
$this->db->cache_on();
$query = $this->db->query("SELECT * FROM admin");

$this->db->cache_delete()

It is used to deletes the cache files associated with a particular page.

$this->db->cache_delete('blog', 'comments');

$this->db->cache_delete_all()

It is used to clears all existing cache files.

$this->db->cache_delete_all();

Advertisements

Add Comment

📖 Read More