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.
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 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.
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).
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.
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.
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.
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
- 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
lsphpthat 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
- 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
//Connecting to Redis server on localhost
$redis = new Redis();
echo "Connection to server sucessfully";
//store data in redis list
// Get the stored data and print it
$arList = $redis->lrange("tutorial-list", 0 ,5);
echo "Stored string in redis:: ";
- 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
- ..and it will show your data and you can also check it again in the browser
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