Redis is an open-source, advanced key-value store. It’s mostly used for caching purposes. In this blog, we are going to learn how to implement Redis caching with Laravel API. So, What are you waiting for let’s Start:
Install Laravel Project
To Install Fresh Laravel Project Write Below Command In Your CMD/Terminal:
“composer create-project laravel/laravel RedisDemo”
Write Below Command In Your CMD/Terminal for the installation of Redis :
composer require predis/predis
sudo apt-get install redis
Type redis-cli after successful installation, to enter in the Redis command line. After that type keys * to get all the keys. Right now there are no keys so we got an empty array.
Configure your Redis setting via the config/database.php
.ENV File Configurations:-
Set / Get / Delete Redis Keys
To use Redis we have created a RedisTrait.php file in app/http/Traits so our Redis code is in one place & we can use this trait to set & get the API result in our code.
cacheAllow(): This function will verify whether the caching is allowed or not.
cacheExist(): This function will check whether the caching is allowed or not with the existence of the Redis key.
setCache(): This function will set the details in Redis as a key-value pair with the expiry time. The expiry time is optional, if we do not set the expiry time then the key will exist in the cache for a lifetime.
getCache(): This function will return the cache details by using the key name.
deleteCache(): This function will delete the Redis key using the key name.
setMetaCache(): This function will set the value in the proper structure as we want for API response. You can set any type of structure you want.
getRedisKey(): This function helps to generate the Redis keys. These keys can be set to get the details in Redis.
getMetaCount(): This function will give the count details from the Redis key as we set it in getMetaCache.
getCacheData(): This function will be used to get the actual data to form the Redis key as we set in setMetaCache.
This function will remove the Redis key that matches the pattern.
Apply Caching In API
Add API route in routes/api.php file:-
Remove Key When Add / Update / Delete
Whenever the details are added, deleted, or updated then we need the latest details in API. We will use the boot method of the model to remove old keys from the cache.
- Before Apply Caching:–
Then it will fire 2 queries to get the details from the database if there are no keys in the cache. After that, it will store the details in caching.
- After Caching:-
After caching the details will call the API. It will check whether the cache is allowed & key exists, if yes then it will return the details from the caching. There is no query fired at this time & details come from the caching.
We can check the available keys in redis-cli & if you want to check the cache details of the particular key then type the below command in TERMINAL / CLI.
redis-cli : To open the Redis command line.
keys * : To get all the key names that are stored in the cache.
get key_name : To get the details of the key.
flushall : To remove all the keys.
Hire Our Worthy Web Developers
Explore the Benefits of Our Web Get Expert Assistance
Hope this blog might be useful to understand Redis Caching With Laravel. In future I will publish similar blogs on Redis and Laravel that you can implement in your next project.