How To Cache Database Query Using CodeIgniter Cache?

Reading Time: 3 minutes
131 Views

Caching database queries is a crucial optimization technique for improving the performance of web applications.

In this tutorial, we will explore how to efficiently cache database queries using the caching features provided by CodeIgniter. CodeIgniter, a robust PHP framework, offers a straightforward approach to caching, allowing developers to store and retrieve frequently requested data, thereby reducing the load on the database and enhancing overall application speed.

Caching allows you to store the results of expensive database queries temporarily, ensuring that subsequent requests for the same data can be served quickly from the cache rather than re-executing the query. This is particularly beneficial in scenarios where certain database queries are resource-intensive or where the data changes infrequently.

Read More: How To Integrate ChatGPT API in CodeIgniter 4 Tutorial

Let’s get started.

CodeIgniter 4 Installation

To create a CodeIgniter 4 setup run this given command into your shell or terminal. Please make sure composer should be installed.

composer create-project codeigniter4/appstarter codeigniter-4

Assuming you have successfully installed application into your local system.

CodeIgniter Cache Using “cache()” Helper Function

To store database queries or data into cache you have to use,

Syntax

cache()->save("Your_Cache_Key", "Your_Cache_Data", 300);

300 is ttl Time To Live, in seconds (default 60)

To get Cache data,

Syntax

cache("Your_Cache_Key")

Usage of CodeIgniter “cache()” Helper Function

Complete code to store data and get data from cache,

<?php

namespace App\Controllers;

use App\Controllers\BaseController;

class EmailController extends BaseController
{
    private $db;
  
    public function __construct(){

        $this->db = db_connect();
    }

    public function getAppUsers()
    {
        if (($result = cache("Cache_Users_Data")) == null ) // Check Users in Cache
        {
            $tableObject = $this->db->table("users");

            $result= $tableObject->select("*")->get()->getResultArray(); // Run Query for Users

            cache()->save("Cache_Users_Data", $result, 300); // Save Users data To Cache
        }

        // If cache exists then users from cache else from database query
        return $this->response->setJSON([
            'users' => $result
        ]);
    }
}

Read More: How To Connect CodeIgniter 4 with Multiple Databases?

That’s it.

We hope this article helped you to learn about How To Cache Database Query Using CodeIgniter Cache Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more