{"id":220,"date":"2022-12-12T20:14:45","date_gmt":"2022-12-12T19:14:45","guid":{"rendered":"https:\/\/chigisoft.com\/blog\/?p=220"},"modified":"2023-07-01T18:10:40","modified_gmt":"2023-07-01T17:10:40","slug":"redis-using-laravel","status":"publish","type":"post","link":"https:\/\/chigisoft.com\/blog\/redis-using-laravel\/","title":{"rendered":"REDIS using Laravel"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><br>In my <a href=\"https:\/\/chigisoft.com\/blog\/redis-om-developer-experience-using-node-js\/\" data-type=\"post\" data-id=\"55\">previous article<\/a>, I wrote about how to implement Redis OM (Object Mapper) using NodeJs. In this article, we will be learning how to implement Redis with the Laravel framework.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/laravel.com\/\" target=\"_blank\" data-type=\"URL\" data-id=\"https:\/\/laravel.com\/\" rel=\"noreferrer noopener\">Laravel<\/a> is an open-source robust framework embedded in the PHP programming language, used for developing scalable, high-performance web applications. We are using the Laravel framework to develop APIs for storing and retrieving records on the Redis Database.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This tutorial provides a basic understanding of the commonly used Redis commands.<\/p>\n\n\n\n<h4 id=\"what-is-redis\" class=\"wp-block-heading\">What is Redis?<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Redis is an open-source in-memory key\/value pair database. This is to say that data sets are stored in RAM instead of the disk (SSD, HDD) in a key and value format. Some of the pros of using Redis are its speed and high availability. The major disadvantage of using Redis is memory consumption. Due to the fact data are stored in memory you may require large memory space to store your records if your dataset is large. That brings us to <strong>Redis Enterprise<\/strong>.<\/p>\n\n\n\n<h4 id=\"what-is-redis-enterprise\" class=\"wp-block-heading\">What is Redis Enterprise<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Redis Cloud also known as <a href=\"https:\/\/redis.com\/\" target=\"_blank\" rel=\"noopener\">Redis Enterprise<\/a> is a cloud service that manages your Redis dataset in a highly available and scalable manner. Redis Enterprise takes the burden of purchasing large Memory space off your shoulders so you can focus on the core development of your applications. On Redis Enterprise you can create a free account and if you are not too familiar with the platform you can kick off with the free 30MB storage that is assigned to your account after a successful registration. When you start working with large datasets you are required to start a paid plan.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Before we start setting up the development environment there are prerequisite tools that must be in place.<\/p>\n\n\n\n<h4 id=\"prerequisite\" class=\"wp-block-heading\">Prerequisite<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>PHP<\/li>\n\n\n\n<li>Composer<\/li>\n\n\n\n<li>IDE<\/li>\n\n\n\n<li>Docker<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h4 id=\"redis-cloud-account-setup\" class=\"wp-block-heading\">Redis Cloud Account Setup<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Use this link: <a href=\"https:\/\/redis.com\/\" target=\"_blank\" rel=\"noopener\">Redis Enterprise<\/a> to create a free account. After creating your account, you can either create a paid subscription or utilise the free 30Mb assigned to your account. If you want to use the free 30Mb (for test purposes) then go ahead and create a database. After creating an account you should have an interface similar to the image below.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/drive.google.com\/uc?export=view&id=1qZFaJOx-TjPaqq4ZNjcl7-RuBN_5EBjr\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">For this tutorial, we are using the free subscription.<br>To create a database, click on the <code>New database<\/code> button. After clicking on the button enter a database name, and you should have an interface similar to the image below.<br><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/drive.google.com\/uc?export=view&id=1CyXz13V6mFZxBx2Ty8Mx0s3loslWoF31\" alt=\"\"\/><figcaption class=\"wp-element-caption\">image<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">At the top right is the <code>Activate database<\/code> button, go ahead and activate your database.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Click on your database name and it will display the database credentials that you will be using later in this tutorial.<br><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/drive.google.com\/uc?export=view&id=1tOQyQeXHSqXHLSCVa1SX-OTzuyYmbuff\" alt=\"\"\/><\/figure>\n\n\n\n<h4 id=\"local-setup\" class=\"wp-block-heading\">Local Setup<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">If you want to set up Redis Server on your local machine, follow the instructions below.<\/p>\n\n\n\n<h5 id=\"local-setup-with-docker\" class=\"wp-block-heading\">Local Setup With Docker<\/h5>\n\n\n\n<p class=\"wp-block-paragraph\">Is time to download the Redis docker container. Before we do that ensure the <a href=\"https:\/\/www.docker.com\/\" target=\"_blank\" rel=\"noopener\">Docker<\/a> engine is already installed and functioning correctly on your system. The essence of downloading the Redis Docker container is to enable our Redis server to run in an isolated environment. The command below will download the Redis container and get it running:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">docker run -d --name redis -p 6379:6379 redis<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">To ensure your Docker container is up and running. Use the command below, it will display all running containers:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">docker ps<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s get into our Redis docker container shell.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">docker exec -t redis sh<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Run the Redis CLI<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">redis-cli<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You will see the Redis prompt on your terminal or cmd:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">127.0.0.1:6379> <\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If you have the Redis prompt that means you are good but if you don\u2019t please go through the instructions again.<\/p>\n\n\n\n<h5 id=\"local-set-up\" class=\"wp-block-heading\">Local Set Up<\/h5>\n\n\n\n<p class=\"wp-block-paragraph\">If you don\u2019t want to use docker, use this <a href=\"https:\/\/redis.io\/docs\/getting-started\/\" target=\"_blank\" rel=\"noopener\">Redis<\/a> link to install it on your local machine. Once you have the Redis server installed on your local machine. Open your terminal or CMD and run this command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ redis-cli<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You should have the Redis prompt like the one below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>127.0.0.1:6379><\/code><\/pre>\n\n\n\n<h5 id=\"laravel-setup\" class=\"wp-block-heading\">Laravel Setup<\/h5>\n\n\n\n<p class=\"wp-block-paragraph\">As I mentioned earlier, you must have installed PHP and composer on your machine before you can create your Laravel project. You can use this <a href=\"https:\/\/laravel.com\/docs\/9.x\/installation\" target=\"_blank\" rel=\"noopener\">Laravel<\/a> link to read up on Laravel and its requirements for installation.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Open your command line (i.e your terminal, cmd, etc), and use the command below to create a Laravel project.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">composer create-project laravel\/laravel redis-with-laravel<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">My project name is <code>redis-with-laravel<\/code> , you can name yours whatever you want.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If your project is created successfully then you should have the message below on your command line (CLI)<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/drive.google.com\/uc?export=view&id=12GYhcUirwKYwcFbJ_RrPlkR_J7dFa2PY\" alt=\"redis-with-laravel\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">We are implementing some Redis commands. On this application, we will perform the CRUD functionality. CD (change directory) into your project directory:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">cd redis-with-laravel<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Open your project on your test editor. Start your project with this command.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">php artisan serve<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Go to your browser and run the url:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">http:\/\/localhost:8000<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If you see the Laravel home page. Bravo! You are halfway through.<\/p>\n\n\n\n<h5 id=\"redis-client\" class=\"wp-block-heading\">Redis Client<\/h5>\n\n\n\n<p class=\"wp-block-paragraph\">We are installing the Redis client for PHP. In this tutorial, we are using the <code>Predis<\/code> client. You can check it out <a href=\"https:\/\/github.com\/predis\/predis\" target=\"_blank\" rel=\"noopener\">here<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Open your terminal and ensure you are in the root folder of your project:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># sample file-path\n\/v\/w\/h\/redis-with-laravel><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Use composer to install the client on your project:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">composer require predis\/predis<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">After installing the Redis Client, open your <code>.env<\/code> file and update your Redis environment variable.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you are using Redis Server on your local machine then update your Redis environment variables with these lines:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">REDIS_HOST=127.0.0.1<br>REDIS_PASSWORD=null<br>REDIS_PORT=6379<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">For Redis Cloud \u2013 go to your Redis cloud account, and click on your database name, it will lead to a page that contains your database credentials. Copy your public endpoint.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When copying your endpoint stop right before the colon.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/drive.google.com\/uc?export=view&id=1fz5A2OAq5b4o4FTmKEaDMjTANUJEbWIo\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Scroll down to <code>Security<\/code> and copy your database password.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/drive.google.com\/uc?export=view&id=1rmUfGaG6YoPjkdy4brGI7Qau6yKJgpnT\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Your port number is the digit after the colon on your endpoint. The last variable on the <code>.env<\/code> file is to tell our application to use the <code>Predis<\/code> client:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">REDIS_HOST=redis-14457.c256.us-east-1-2.ec2.cloud.redislabs.com<br>REDIS_PASSWORD=j**2**N99V**o**a**9Zc**3**X9**Q**4<br>REDIS_PORT=14457<br>REDIS_CLIENT=predis<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Or you can go to your <code>config\/database.php<\/code> file and change your Redis database connection from <code>phpredis<\/code> to <code>Predis<\/code>. By default Laravel uses <code>phpredis<\/code> client.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">'redis' => [<br>'client' => env('REDIS_CLIENT', 'phpredis'),<br>...<br>]<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Change to:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">'redis' => [<br>'client' => env('REDIS_CLIENT', 'predis'),<br>...<br>]<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Stop and restart your application to update your application with the changes on your <code>.env<\/code> file:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">php artisan serve<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">On your terminal (still on your project root directory) create a controller.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ php artisan make:controller UrlController<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Go to your <code>app\/Http\/Controllers<\/code> you will see your <code>UrlController<\/code> script.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Import Redis facades and create functions that will store the records on the Redis database.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><?php\n\nnamespace App\\Http\\Controllers;\n\nuse Illuminate\\Http\\Request;\nuse Illuminate\\Support\\Facades\\Redis;\n\nclass UrlController extends Controller\n{\n   public function storeUrl()\n   {\n\t\t\/\/\n   }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s assume we have lists of URLs we want to store on the database. The <code>SET<\/code> command enables us to store the record as a key\/value on the database.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><?php<br>\u200b<br>namespace App\\Http\\Controllers;<br>\u200b<br>use Illuminate\\Http\\Request;<br>use Illuminate\\Support\\Facades\\Redis;<br>\u200b<br>class UrlController extends Controller<br>{<br> \u00a0 public function storeUrl()<br> \u00a0 {<br> \u00a0 \u00a0 \u00a0try {<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \/\/ the first string is the key and the second string url is the value<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0$res = Redis::set('url:1', 'http:\/\/localhost:8000\/example.com');<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0return response()->json([<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0'status' => 'successful',<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0'data' => $res<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  ]);<br> \u00a0 \u00a0 \u00a0  } catch (\\Throwable $e) {<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0return response()->json([<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0'status' => 'error',<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0'message' => $e->getMessage(),<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  ]);<br> \u00a0 \u00a0 \u00a0  }<br> \u00a0 }<br>}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s fetch our record using the <code>SET<\/code> command<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><?php<br>\u200b<br>namespace App\\Http\\Controllers;<br>\u200b<br>use Illuminate\\Http\\Request;<br>use Illuminate\\Support\\Facades\\Redis;<br>\u200b<br>class UrlController extends Controller<br>{<br> \u00a0 \u00a0public function GetUrl()<br> \u00a0  {<br> \u00a0 \u00a0 \u00a0 \u00a0try {<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 $res = Redis::get('url:2');<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0return response()->json([<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0'status' => 'successful',<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0'data' => $res<br> \u00a0 \u00a0 \u00a0 \u00a0  ]);<br> \u00a0 \u00a0 \u00a0  } catch (\\Throwable $e) {<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0return response()->json([<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0'status' => 'error',<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0'message' => $e->getMessage(),<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  ]);<br> \u00a0 \u00a0 \u00a0  }<br> \u00a0  }<br>}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">We can also fetch multiple records using the <code>MGET<\/code> command. on the <code>MGET<\/code> method, we will input multiple keys and it will fetch the records that match the keys.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> public function GetUrls()<br> \u00a0  {<br> \u00a0 \u00a0 \u00a0 \u00a0try {<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0$res = Redis::mget('url:2', 'url:3', 'url:4');<br> \u00a0 \u00a0 \u00a0 \u00a0return response()->json([<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0'status' => 'successful',<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0'data' => $res<br> \u00a0 \u00a0 \u00a0  ]);<br> \u00a0 \u00a0 \u00a0  } catch (\\Throwable $e) {<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0return response()->json([<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0'status' => 'error',<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0'message' => $e->getMessage(),<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  ]);<br> \u00a0 \u00a0 \u00a0  }<br> \u00a0  }<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">We can store a record with an expiration time attached to the key using the <code>SETEX<\/code> command. This means once the time elapses the key and value is deleted from the database. Time are stored in milliseconds.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> \u00a0 \u00a0public function StorUrlWithTimeout()<br> \u00a0  {<br> \u00a0 \u00a0 \u00a0 \u00a0try {<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\/\/ key will be deleted after 60 seconds of creating the record<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0$res = Redis::setex('url:5', 60, 'http:\/\/localhost:8000\/example.com');<br> \u00a0 \u00a0 \u00a0          return response()->json([<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0      'status' => 'successful',<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0      'data' => $res<br> \u00a0 \u00a0 \u00a0      ]);<br> \u00a0 \u00a0 \u00a0  } catch (\\Throwable $e) {<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0return response()->json([<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0'status' => 'error',<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0'message' => $e->getMessage(),<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  ]);<br> \u00a0 \u00a0 \u00a0  }<br> \u00a0  }<br>\u200b<br>    \/\/ fetch record<br>    \/\/ After 60 seconds of creating the record it will be deleted<br> \u00a0 \u00a0public function GetUrlWithTimeout()<br> \u00a0  {<br> \u00a0 \u00a0 \u00a0 \u00a0try {<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0$res = Redis::get('url:5');<br> \u00a0 \u00a0 \u00a0 \u00a0return response()->json([<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0'status' => 'successful',<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0'data' => $res<br> \u00a0 \u00a0 \u00a0  ]);<br> \u00a0 \u00a0 \u00a0  } catch (\\Throwable $e) {<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0return response()->json([<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0'status' => 'error',<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0'message' => $e->getMessage(),<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  ]);<br> \u00a0 \u00a0 \u00a0  }<br> \u00a0  }<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">As you observed I used numbers to make my keys unique because when you are creating a record with an already existing key, Redis will overwrite the old record with the new one. You can use some PHP inbuilt methods to generate a unique key to store your records but ensure your keys are stored somewhere you can easily retrieve them. You can store it as a file document.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can store records with a hash by using the <code>HSET<\/code> command. Storing records using the hash is another way of creating a unique for your value.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> \u00a0 \u00a0public function StoreUrlWithHash()<br> \u00a0  {<br> \u00a0 \u00a0 \u00a0 \u00a0try {<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0$res = Redis::hset('url-hash', 'url:6', 'http:\/\/localhost:8000\/example.com');<br> \u00a0 \u00a0 \u00a0 \u00a0return response()->json([<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0'status' => 'successful',<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0'data' => $res<br> \u00a0 \u00a0 \u00a0  ]);<br> \u00a0 \u00a0 \u00a0  } catch (\\Throwable $e) {<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0return response()->json([<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0'status' => 'error',<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0'message' => $e->getMessage(),<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  ]);<br> \u00a0 \u00a0 \u00a0  }<br> \u00a0  }<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s fetch the record with its hash:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> \u00a0 \u00a0public function GetUrlWithHash()<br> \u00a0  {<br> \u00a0 \u00a0 \u00a0 \u00a0try {<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0$res = Redis::hget('url-hash', 'url:6');<br> \u00a0 \u00a0 \u00a0 \u00a0return response()->json([<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0'status' => 'successful',<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0'data' => $res<br> \u00a0 \u00a0 \u00a0  ]);<br> \u00a0 \u00a0 \u00a0  } catch (\\Throwable $e) {<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0return response()->json([<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0'status' => 'error',<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0'message' => $e->getMessage(),<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  ]);<br> \u00a0 \u00a0 \u00a0  }<br> \u00a0  }<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Go to your <code>routes\/web.php<\/code> file and setup your routes.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><?php<br>\u200b<br>use Illuminate\\Support\\Facades\\Route;<br>use App\\Http\\Controllers\\UrlController;<br>\u200b<br>Route::controller(UrlController::class)->group(function () {<br> \u00a0 \u00a0Route::get('\/urls\/store', 'storeUrl');<br> \u00a0 \u00a0Route::get('\/urls\/create', 'StoreMultipleUrl');<br> \u00a0 \u00a0Route::get('\/view', 'GetUrl');<br> \u00a0 \u00a0Route::get('\/urls', 'GetUrls');<br> \u00a0 \u00a0Route::get('\/urls\/timeout\/store', 'StorUrlWithTimeout');<br> \u00a0 \u00a0Route::get('urls\/timeout\/fetch', 'GetUrlWithTimeout');<br> \u00a0 \u00a0Route::get('\/urls\/hash', 'StoreUrlWithHash');<br> \u00a0 \u00a0Route::get('\/urls\/hash\/fetch', 'GetUrlWithHash');<br>});<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Open your REST client e.g Postman or Insomnia to test the endpoints.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/drive.google.com\/uc?export=view&id=1Syvow-fm4gOon8Azow2hZEloC7fmqX-5\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s go ahead and fetch the records<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/drive.google.com\/uc?export=view&id=1gHIHwJPhL92OSgQaXi1UoYgXGZE2foAw\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Now you have learned how to use Redis with Laravel. Try hitting other endpoints and you are free to add more functionalities. Below is an overview of the <code>UrlController<\/code> class.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><?php<br>\u200b<br>namespace App\\Http\\Controllers;<br>\u200b<br>use Illuminate\\Http\\Request;<br>use Illuminate\\Support\\Facades\\Redis;<br>\u200b<br>class UrlController extends Controller<br>{<br>\u200b<br> \u00a0 \u00a0public function storeUrl()<br> \u00a0  {<br> \u00a0 \u00a0 \u00a0 try {<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\/\/ the first string is the key and the second string url is the value<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 $res = Redis::set('url:1', 'http:\/\/localhost:8000\/example.com');<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 return response()->json([<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 'status' => 'successful',<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 'data' => $res<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 ]);<br> \u00a0 \u00a0 \u00a0 \u00a0 } catch (\\Throwable $e) {<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 return response()->json([<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 'status' => 'error',<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 'message' => $e->getMessage(),<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 ]);<br> \u00a0 \u00a0 \u00a0 \u00a0 }<br> \u00a0  }<br>\u200b<br> \u00a0 \u00a0public function GetUrl()<br> \u00a0  {<br> \u00a0 \u00a0 \u00a0 \u00a0try {<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0$x = Redis::get('url:1');<br> \u00a0 \u00a0 \u00a0 \u00a0return response()->json([<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0'status' => 'successful',<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0'data' => $x<br> \u00a0 \u00a0 \u00a0  ]);<br> \u00a0 \u00a0 \u00a0  } catch (\\Throwable $e) {<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0return response()->json([<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0'status' => 'error',<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0'message' => $e->getMessage(),<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  ]);<br> \u00a0 \u00a0 \u00a0  }<br> \u00a0  }<br> \u00a0 \u00a0<br> \u00a0 \u00a0public function StoreMultipleUrl()<br> \u00a0  {<br> \u00a0 \u00a0 \u00a0 \u00a0try {<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0$res = Redis::mset(<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0'url:2', 'http:\/\/localhost:8000\/example.com',<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0'url:3', 'http:\/\/localhost:8000\/example.com',<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0'url:4', 'http:\/\/localhost:8000\/example.com'<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  );<br> \u00a0 \u00a0 \u00a0  } catch (\\Throwable $e) {<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0return response()->json([<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0'status' => 'error',<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0'message' => $e->getMessage(),<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  ]);<br> \u00a0 \u00a0 \u00a0  }<br> \u00a0  }<br>\u200b<br> \u00a0 \u00a0public function GetUrls()<br> \u00a0  {<br> \u00a0 \u00a0 \u00a0 \u00a0try {<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0$res = Redis::mget('url:2', 'url:3', 'url:4');<br> \u00a0 \u00a0 \u00a0 \u00a0return response()->json([<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0'status' => 'successful',<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0'data' => $res<br> \u00a0 \u00a0 \u00a0  ]);<br> \u00a0 \u00a0 \u00a0  } catch (\\Throwable $e) {<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0return response()->json([<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0'status' => 'error',<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0'message' => $e->getMessage(),<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  ]);<br> \u00a0 \u00a0 \u00a0  }<br> \u00a0  }<br>\u200b<br> \u00a0 \u00a0public function StorUrlWithTimeout()<br> \u00a0  {<br> \u00a0 \u00a0 \u00a0 \u00a0try {<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0$res = Redis::setex('url:5', 30, 'http:\/\/localhost:8000\/example.com');<br> \u00a0 \u00a0 \u00a0 \u00a0return response()->json([<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0'status' => 'successful',<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0'data' => $res<br> \u00a0 \u00a0 \u00a0  ]);<br> \u00a0 \u00a0 \u00a0  } catch (\\Throwable $e) {<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0return response()->json([<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0'status' => 'error',<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0'message' => $e->getMessage(),<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  ]);<br> \u00a0 \u00a0 \u00a0  }<br> \u00a0  }<br>\u200b<br> \u00a0 \u00a0public function GetUrlWithTimeout()<br> \u00a0  {<br> \u00a0 \u00a0 \u00a0 \u00a0try {<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0$res = Redis::get('url:5');<br> \u00a0 \u00a0 \u00a0 \u00a0return response()->json([<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0'status' => 'successful',<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0'data' => $res<br> \u00a0 \u00a0 \u00a0  ]);<br> \u00a0 \u00a0 \u00a0  } catch (\\Throwable $e) {<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0return response()->json([<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0'status' => 'error',<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0'message' => $e->getMessage(),<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  ]);<br> \u00a0 \u00a0 \u00a0  }<br> \u00a0  }<br>\u200b<br> \u00a0 \u00a0public function StoreUrlWithHash()<br> \u00a0  {<br> \u00a0 \u00a0 \u00a0 \u00a0try {<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0$res = Redis::hset('url-hash', 'url:6', 'http:\/\/localhost:8000\/example.com');<br> \u00a0 \u00a0 \u00a0      return response()->json([<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0'status' => 'successful',<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0'data' => $res<br> \u00a0 \u00a0 \u00a0      ]);<br> \u00a0 \u00a0 \u00a0  } catch (\\Throwable $e) {<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0return response()->json([<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0'status' => 'error',<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0'message' => $e->getMessage(),<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  ]);<br> \u00a0 \u00a0 \u00a0  }<br> \u00a0  }<br>\u200b<br> \u00a0 \u00a0public function GetUrlWithHash()<br> \u00a0  {<br> \u00a0 \u00a0 \u00a0 \u00a0try {<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0$res = Redis::hget('url-hash', 'url:6');<br> \u00a0 \u00a0 \u00a0      return response()->json([<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0'status' => 'successful',<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0'data' => $res<br> \u00a0 \u00a0 \u00a0      ]);<br> \u00a0 \u00a0 \u00a0  } catch (\\Throwable $e) {<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0return response()->json([<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0'status' => 'error',<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0'message' => $e->getMessage(),<br> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  ]);<br> \u00a0 \u00a0 \u00a0  }<br> \u00a0  }<br>}<br>\u200b<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Learn more about <a href=\"https:\/\/redis.io\/commands\/\" target=\"_blank\" rel=\"noopener\">Redis<\/a> command<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em><strong>This post is in collaboration with <a href=\"https:\/\/redis.io\/\" target=\"_blank\" rel=\"noopener\">Redis<\/a><\/strong><\/em><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/learn.chigisoft.com\/\"><img loading=\"lazy\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/chigisoft.com\/blog\/wp-content\/uploads\/2023\/07\/Learn-product-design-with-Chigisoft-3-edited.png\" alt=\"\" class=\"wp-image-465\" srcset=\"https:\/\/chigisoft.com\/blog\/wp-content\/uploads\/2023\/07\/Learn-product-design-with-Chigisoft-3-edited.png 1200w, https:\/\/chigisoft.com\/blog\/wp-content\/uploads\/2023\/07\/Learn-product-design-with-Chigisoft-3-edited-300x169.png 300w, https:\/\/chigisoft.com\/blog\/wp-content\/uploads\/2023\/07\/Learn-product-design-with-Chigisoft-3-edited-1024x576.png 1024w, https:\/\/chigisoft.com\/blog\/wp-content\/uploads\/2023\/07\/Learn-product-design-with-Chigisoft-3-edited-768x432.png 768w, https:\/\/chigisoft.com\/blog\/wp-content\/uploads\/2023\/07\/Learn-product-design-with-Chigisoft-3-edited-390x219.png 390w, https:\/\/chigisoft.com\/blog\/wp-content\/uploads\/2023\/07\/Learn-product-design-with-Chigisoft-3-edited-820x461.png 820w, https:\/\/chigisoft.com\/blog\/wp-content\/uploads\/2023\/07\/Learn-product-design-with-Chigisoft-3-edited-1180x664.png 1180w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/a><\/figure>\n\n\n\n<h5 id=\"related\" class=\"wp-block-heading\">Related<\/h5>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><a href=\"https:\/\/redis.com\/try-free\/?utm_campaign=write_for_redis\" target=\"_blank\" rel=\"noopener\">Try Redis Cloud for free<\/a><\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><a href=\"https:\/\/redis.info\/3LC4GqB\" target=\"_blank\" rel=\"noopener\">Redis Developer Hub \u2013 tools, guides, and tutorials about Redis<\/a><\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><a href=\"https:\/\/redis.info\/3wMR7PR\" target=\"_blank\" rel=\"noopener\">RedisInsight Desktop GUI<\/a><\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><a href=\"https:\/\/redis.info\/3Ga9YII\" target=\"_blank\" rel=\"noopener\">Watch this video on the benefits of Redis Cloud over <\/a><a href=\"https:\/\/redis.info\/3Ga9YII\" target=\"_blank\" rel=\"noreferrer noopener\">other<\/a><a href=\"https:\/\/redis.info\/3Ga9YII\" target=\"_blank\" rel=\"noopener\"> <\/a><a href=\"https:\/\/redis.info\/3Ga9YII\" target=\"_blank\" rel=\"noreferrer noopener\">Redis<\/a><a href=\"https:\/\/redis.info\/3Ga9YII\" target=\"_blank\" rel=\"noopener\"> providers<\/a><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"In my previous article, I wrote about how to implement Redis OM (Object Mapper) using NodeJs. In this article, we will be learning how to implement Redis with the Laravel&hellip;\n","protected":false},"author":3,"featured_media":231,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13,14],"tags":[68,16,9,18,17],"class_list":["post-220","post","type-post","status-publish","format-standard","has-post-thumbnail","category-tech","category-devops","tag-laravel","tag-programming","tag-redis","tag-softwaredevelopment","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/chigisoft.com\/blog\/wp-json\/wp\/v2\/posts\/220","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/chigisoft.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/chigisoft.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/chigisoft.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/chigisoft.com\/blog\/wp-json\/wp\/v2\/comments?post=220"}],"version-history":[{"count":10,"href":"https:\/\/chigisoft.com\/blog\/wp-json\/wp\/v2\/posts\/220\/revisions"}],"predecessor-version":[{"id":466,"href":"https:\/\/chigisoft.com\/blog\/wp-json\/wp\/v2\/posts\/220\/revisions\/466"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/chigisoft.com\/blog\/wp-json\/wp\/v2\/media\/231"}],"wp:attachment":[{"href":"https:\/\/chigisoft.com\/blog\/wp-json\/wp\/v2\/media?parent=220"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/chigisoft.com\/blog\/wp-json\/wp\/v2\/categories?post=220"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/chigisoft.com\/blog\/wp-json\/wp\/v2\/tags?post=220"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}