CodeIgniter4 caching with Redis in a sample CRUD (Create, Read, Update, Delete) project Emman, October 2, 2023October 2, 2023 CodeIgniter 4’s caching with Redis in a sample CRUD (Create, Read, Update, Delete) project. CodeIgniter 4 is a PHP framework that provides a set of tools to build web applications more efficiently. Redis is an in-memory data structure store that can be used as a caching layer to improve the performance of web applications. In this example, We’ll walk through the process of setting up a basic CRUD project using CodeIgniter 4 and utilizing Redis for caching. I’ll assume you have some basic familiarity with PHP and web development concepts. Step 1: Setup CodeIgniter 4 Project Install Composer if you haven’t already. Open a terminal and navigate to the directory where you want to create your project. Run the following command to create a new CodeIgniter 4 project: composer create-project codeigniter4/appstarter ci4_redis_crud Step 2: Configure Redis Make sure you have a Redis server running locally or on a remote server. In the app/Config/Cache.php file, set the 'redis' cache handler as your default cache handler: public $handler = 'redis'; Configure the Redis connection settings in the app/Config/Redis.php file: public $default = [ 'hostname' => '127.0.0.1', // Redis server IP or hostname 'port' => 6379, // Redis server port 'password' => '', // Redis server password 'database' => 0, // Redis database number ]; Step 3: Create a Sample CRUD Controller Create a new controller by running the following command in your project’s root directory: php spark make:controller Users In the newly created Users.php controller file (app/Controllers/Users.php), add methods for CRUD operations: namespace App\Controllers; use App\Models\UserModel; use CodeIgniter\Controller; class Users extends Controller { public function index() { // Load the Redis cache $cache = \Config\Services::cache('redis'); // Try to get the users from cache $users = $cache->get('cached_users'); if ($users === null) { // If not cached, fetch users from the database $userModel = new UserModel(); $users = $userModel->findAll(); // Cache the users for 5 minutes $cache->save('cached_users', $users, 300); } return view('users/index', ['users' => $users]); } // Add other CRUD methods (create, edit, update, delete) here... } Step 4: Create the User Model Create a model by running the following command: php spark make:model UserModel In the UserModel.php file (app/Models/UserModel.php), define your database table and other methods for CRUD operations. Step 5: Create Views Create the necessary views (app/Views/users/index.php, app/Views/users/create.php, app/Views/users/edit.php, app/Views/users/view.php) for your CRUD operations. Step 6: Routing Define routes in app/Config/Routes.php to map the CRUD actions to your controller methods. Step 7: Run the Project Run the development server using: php spark serve Access your CRUD application in a web browser and navigate to the Users section to see the cached user data being used to enhance the performance of your application. Please note that this is a basic example and does not cover all aspects of a production-ready application, such as input validation, security considerations, and error handling. Always follow best practices when building real-world applications. Share this:FacebookX Related Discover more from Code Concepts Snippets Subscribe to get the latest posts sent to your email. Type your email… Subscribe CodeIgniter CodeIgniter4 PHP