fbpx
Search
Close this search box.

Use PHP Redis Cache With Custom PHP Sites

Table of Contents

Get up to 50% off now

Become a partner with CyberPanel and gain access to an incredible offer of up to 50% off on CyberPanel add-ons. Plus, as a partner, you’ll also benefit from comprehensive marketing support and a whole lot more. Join us on this journey today!

In this tutorial, we will learn how to use PHP Redis Cache with custom PHP sites.

Redis is a speedy in-memory database and cache that is built in C and tuned for speed. It is open source under the BSD license. “Remote DIctionary Server” is how Redis got its name.

Because its main data types are similar to those used in programming languages, such as strings, lists, dictionaries (or hashes), sets, and sorted sets, Redis is often referred to as a data structure server. It also includes a plethora of different data structures and features for approximate counting, geolocation, and stream processing.

What is Redis?

Millions of developers utilize this open-source in-memory data store as a database, cache, streaming engine, and message broker.

Redis is typically used as a cache to keep frequently accessed data in memory, allowing applications to respond quickly to consumers. Redis provides a variety of sophisticated caching patterns by allowing you to choose how long you want to hold data and which data to evict first.

Intelligent caching serves a variety of purposes and has a significant impact on user experiences, productivity, bounce rates, and retail sales.

Tech Delivered to Your Inbox!

Get exclusive access to all things tech-savvy, and be the first to receive 

the latest updates directly in your inbox.

What are different Redis Data Types?

Strings:

Strings are the most fundamental type of Redis value. Redis Strings are binary safe, which implies that they can hold any type of data, such as a JPEG image or a serialized Ruby object.

The maximum length of a String value is 512 Megabytes.

Lists:

Redis Lists are simply strings sorted in order of insertion. It is possible to add new entries to a Redis List by pushing them to the list’s head (on the left) or tail (on the right).

Sets:

Redis Sets are a collection of Strings that are not ordered.
Redis Sets offer the advantage of not permitting duplicate members. When the same element is added several times, the set will only have one copy of it.

Hashes:

Because Redis Hashes are mappings between string fields and string values, they’re ideal for representing objects.

Hashes are mostly used to represent objects, but because they can store a large number of items, they can be used for a variety of other purposes.

Sorted sets:

Redis Sorted Sets are non-repeating collections of Strings, similar to Redis Sets. The distinction is that every member of a Sorted Set has a score that is used to keep the Sorted Set in order, from lowest to highest score. Scores may be duplicated even though members are unique.

Sorted Sets allow you to quickly add, remove, or update pieces (in a time proportional to the logarithm of the number of elements). Because elements are stored in sequence and are not reordered, you can quickly obtain ranges by score or rank (position).

How to install Redis on CyberPanel?

Redis daemon can be easily installed if you are using CyberPanel, click here to see how you can easily install CyberPanel on your server.

Create a new website

Before using Redis on your PHP site you need to create it in CyberPanel, also make sure to note the PHP version you select while creating the site, because we will use this version to later install Redis PHP Cache extension.

Enhance Your CyerPanel Experience Today!
Discover a world of enhanced features and show your support for our ongoing development with CyberPanel add-ons. Elevate your experience today!

Creating a website is so simple on CyberPanel that anyone can do it. Creating a website is only 4 steps procedure.

  • Login to your to your CyberPanel Dashboard
  • Click on Websites -> Create Website from the left hand side menu
  • Enter all the relevant information, make sure to note down which php you are entering here
  • Click on “Create Website”

Install PHP Redis Cache on CyberPanel

Installing Redis on CyberPanel is really very easy and it only involves a couple of steps. So let us go through them.

  • Login to your CyberPanel dashboard
  • Click on “Manage Services” -> “Applications” from the left hand side menu
php redis cache
  • Click on “Install” in front of “Redis”

Install Redis PHP Extension

Before you can do PHP programming with Redis, you need to install Redis PHP Extension which will communicate with Redis daemon we just installed above

  • Login to SSH using Bitvise and open the command prompt
  • Enter the following command and make sure to enter your php after lsphp that you have selected while creating your website
apt-get install lsphp74-redis -y
  • Now you have to enter a command that will restart php so it could read extension configuration
killall lsphp
  • Go into your site’s file manager using CyberPanel
  • Create a file “redis.php” in public_html
  • Open the code mirror of your new file. Enter the following code and click save



<?php 
   //Connecting to Redis server on localhost 
   $redis = new Redis(); 
   $redis->connect('127.0.0.1', 6379); 
   echo "Connection to server sucessfully"; 
   //store data in redis list 
   $redis->lpush("tutorial-list", "Redis"); 
   $redis->lpush("tutorial-list", "Mongodb"); 
   $redis->lpush("tutorial-list", "Mysql");  
   
   // Get the stored data and print it 
   $arList = $redis->lrange("tutorial-list", 0 ,5); 
   echo "Stored string in redis:: "; 
   print_r($arList); 
?>
  • Open your php file in the browser and you will see that your code is communicating with Redis daemon.
  • Open your SSH enter the command prompt and enter the command
redis-cli monitor
  • ..and it will show your data and you can also check it again in the browser

Conclusion

Faster loading sites can be accomplished using the Redis cache. You can integrate Redis cache with PHP easily by following the PHP Redis Cache example above. 

Please note that data in redis is not stored permanently as it is in-memory cache, so make sure you also have a mechanism in your app to permanently store data for which you need to use MySQL.

Related Content: What Is WordPress Multisite

How To Easily Setup WordPress Staging Site Using CyberPanel?

Editorial Team
The CyberPanel editorial team, under the guidance of Usman Nasir, is composed of seasoned WordPress specialists boasting a decade of expertise in WordPress, Web Hosting, eCommerce, SEO, and Marketing. Since its establishment in 2017, CyberPanel has emerged as the leading free WordPress resource hub in the industry, earning acclaim as the go-to "Wikipedia for WordPress."
Unlock Benefits

Become a Community Member

SIMPLIFY SETUP, MAXIMIZE EFFICIENCY!
Setting up CyberPanel is a breeze. We’ll handle the installation so you can concentrate on your website. Start now for a secure, stable, and blazing-fast performance!