This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

How to Use Redis Cache With Custom PHP Sites on Cloudways

Updated on April 22, 2022

6 Min Read

Short for REmote DIctionary Server, Redis is a open-source (BSD licensed), in-memory data structure store used as a database, cache, and message broker.. It runs in the memory, and you can use it to communicate over any network.

PHP Redis has a slight advantage over other in-memory caches in that it allows a user to store pieces of data in a much more structured, durable, and robust manner. For example, it allows for replication and strong durability options, something most caching systems don’t support.

Redis shines when you want to use data structures such as lists, sets, strings, hashes, sorted sets with range queries, geospatial indexes, hyperloglogs, bitmaps, and streams. Redis has built-in replication, Lua scripting, LRU eviction, transactions, and different levels of on-disk persistence, and provides high availability through Redis Sentinel and automatic partitioning with Redis Cluster.

Redis Data Types:

These are some of the most common Redis data types:

String: This data structure is similar to Strings in PHP. Strings are the most basic kind of Redis value. Redis Strings in Redis are binary safe, meaning they have a known length not determined by any special terminating characters.

Hash: This data type acts as a map between string fields and string values. Redis Hashes are maps between string fields and string values. Hence, Hashes are the ideal data type to represent objects.

Sorted Set: Redis Sorted Sets are similar to Redis Sets. However, each member of a sorted set has a score associated with it, and members are ordered from the smallest score to the largest. While members are unique, the scores may be repeated.

List: A list is a one-dimensional array in which we can push, pop, shift and unshift elements. You can add elements to a Redis List on the head or the tail.

Set: Redis Sets are an unordered collection of strings. Sets are similar to lists; however, elements in a set may appear only once, and a set has no order.

Get Ready for Core Web Vitals Update

Ebook to Speed Up Your Website Before You Start Losing Traffic.

Thank You

Your list is on it’s Way to Your Inbox.

How To Install Redis On Cloudways

We all know that Cloudways servers come pre-installed with Redis, irrespective of your application framework. You need to enable it with one click on your server. If you are using any PHP framework or any PHP-based CMS, many php redis extensions and plugins help you connect Redis with your framework. However, what if you want to use Redis cache in a PHP site built without any framework or CMS? Well, it is possible. And to do so, you need to follow the steps mentioned below in this PHP Redis example.

First, sign in to your Cloudways account if you don’t have one, signup now for free.

Step 1: Choose a Server

Select your server and custom PHP Stack application. Select PHP server size as per your website traffic and location according to your preferred region.

iaas

Step 2: Launch PHP

After completing all these steps, you can finally navigate the application tab and select the PHP stack application. Click on it to find further details inside:

servers

Step 03: Turning On Redis On Cloudways Platform

By default, Redis is “disabled” on Cloudways, and you need to “enable” it to use it. For this, go to your server management tab and navigate to Settings and Packages and click on Packages to open it. Over there, you will see Redis is Uninstalled. Now click on the Install.

install redis

Wait for the server to load your new package.

server package

Now go to the same tab, and you will see Redis is now installed.

redis installed

How to Install PHP Redis Extension

Step 1: Installing PHP Redis Client Library

Predis is a PHP Redis Client Library that helps you to connect to Redis API very easily. Through this, you can perform all the Redis commands. Now let us install this.

Now, to configure Redis with PHP application, Move on to the “Server Management tab” to launch “SSH Terminal,” located in the “Master Credentials.” Here you can find your login credentials for the SSH terminal.

database credentials

You have to open the SSH terminal to run the composer command. You can either use the Putty terminal or Cloudways SSH terminal as per the requirements.

Now I would access the public_html folder with the following command.

LS command is used to list files or directories in Linux
CD use for change directory

Now install Predis PHP in this folder by the following command.

wget https://github.com/predis/predis/archive/refs/heads/v1.1.zip
// Unzip the downloaded file
unzip v1.1.zip 

ssh-predis

Predis is successfully installed on your folder.

Step 2: Using Predis To Connect Custom PHP With Redis

Now let us edit our index.php file on our SSH terminal by typing the following command.

vi index.php

You will see that your file has been viewed on your terminal. Now let’s start editing it.
Press the insert key on your keyboard. Move your cursor to:

front page

Paste the following code in it by clicking the right button of your mouse and selecting paste from the browser.


After pasting, your terminal should look like this:

predis code

Now press the ESC key and type :w this will save the index.php and now press the ESC key and type :q this will exit the viewer.

In the code above, we create a new variable, “hello_world” for our redis cache and setting its value to "Hi from php". After that, we are getting the value of our variable from the Redis cache to check whether Redis is working or not.

Note: You can also edit the files by connecting to SFTP in Server Management tab under Master Credentials using FTP client. (That would be easier)

Step 3: Running It:

Type the below command on the SSH terminal and hit Enter. This command will let us know whether the Redis cache in our PHP site is working or not.

redis-cli monitor

When you run the above command, you will see an OK message. Leave the SSH terminal window open and go to the Application Management tab on your Cloudways panel and click on the application URL. You will see the following result on the newly open tab.

hi php

Now go to the SSH terminal tab, and you will find the following result on your redis-cli, which we have let’s run.

redis monitor

If you see the above window, then this means you have configured PHP with Redis cache successfully.

Conclusion:

Redis cache is one of the best ways to make your site load faster. By following the PHP Redis example above, you connect Redis cache with PHP easily. If you need to know more about the Predis PHP API, then you can visit this page. If you have any issues with the PHP redis installation procedure or get stuck at any step, please ask me through the comments section below.

Share your opinion in the comment section. COMMENT NOW

Share This Article

Customer Review at

“Cloudways hosting has one of the best customer service and hosting speed”

Sanjit C [Website Developer]

Shahzeb Ahmed

Shahzeb is a Digital Marketer with a Software Engineering background, works as a Community Manager — PHP Community at Cloudways. He is growth ambitious and aims to learn & share information about PHP & Laravel Development through practice and experimentation. He loves to travel and explore new ideas whenever he finds time. Get in touch with him at [email protected]

×

Get Our Newsletter
Be the first to get the latest updates and tutorials.

Thankyou for Subscribing Us!

×

Webinar: How to Get 100% Scores on Core Web Vitals

Join Joe Williams & Aleksandar Savkovic on 29th of March, 2021.

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!