Before you start using Redis in your PHP programs, you need to make sure that you have Redis PHP driver and PHP set up on the machine.
Installation
Now, let us check how to set up Redis PHP driver.
You need to download the phpredis from github repository https://github.com/nicolasff/phpredis. Once you’ve downloaded it, extract the files to phpredis directory. On Ubuntu, install the following extension.
cd phpredis sudo phpize sudo ./configure sudo make sudo make install
Now, copy and paste the content of “modules” folder to the PHP extension directory and add the following lines in php.ini.
extension = redis.so
Now, your Redis PHP installation is complete
Connect to Redis Server
<?php //Connecting to Redis server on localhost $redis = new Redis(); $redis->connect('127.0.0.1', 6379); echo "Connection to server sucessfully"; //check whether server is running or not echo "Server is running: ".$redis->ping(); ?>
When the program is executed, it will produce the following result.
Connection to server sucessfully Server is running: PONG
Redis PHP String Example
<?php //Connecting to Redis server on localhost $redis = new Redis(); $redis->connect('127.0.0.1', 6379); echo "Connection to server sucessfully"; //set the data in redis string $redis->set("Adglob-name", "Redis Adglob"); // Get the stored data and print it echo "Stored string in redis:: " .$redis→get("Adglob-name"); ?>
When the above program is executed, it will produce the following result.
Connection to server sucessfully Stored string in redis:: Redis Adglob
Redis php List Example
<?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("Adglob-list", "Redis"); $redis->lpush("Adglob-list", "Mongodb"); $redis->lpush("Adglob-list", "Mysql"); // Get the stored data and print it $arList = $redis->lrange("Adglob-list", 0 ,5); echo "Stored string in redis:: "; print_r($arList); ?>
When the above program is executed, it will produce the following result.
Connection to server sucessfully Stored string in redis:: Redis Mongodb Mysql
Redis PHP Keys Example
<?php //Connecting to Redis server on localhost $redis = new Redis(); $redis->connect('127.0.0.1', 6379); echo "Connection to server sucessfully"; // Get the stored keys and print it $arList = $redis->keys("*"); echo "Stored keys in redis:: " print_r($arList); ?>
When the program is executed, it will produce the following result.
Connection to server sucessfully Stored string in redis:: Adglob-name Adglob-list
What’s up, just wanted to mention, I liked
this blog post. It was funny. Keep on posting!
Thanks for your appreciation.
I think this is a real great post.Really thank you! Much obliged.
Great article.Thanks Again. Keep writing.
Thanks so much for the article post.Thanks Again. Great.
Im obliged for the blog.Really thank you! Awesome.
I cannot thank you enough for the article post.Much thanks again. Really Great.
Im thankful for the blog post. Fantastic.
I really liked your post.Thanks Again. Really Cool.
wow, awesome post.Much thanks again. Cool.
Major thanks for the blog. Really Great.
Really enjoyed this blog article. Keep writing.
Very neat article.Really looking forward to read more. Fantastic.
I truly appreciate this blog.Really thank you! Will read on…
I value the post. Cool.
I think this is a real great blog post.Much thanks again.
Thanks-a-mundo for the post.Thanks Again. Really Great.
I value the blog post.Much thanks again. Cool.
A round of applause for your post.Thanks Again.
Thank you for your blog article.Much thanks again. Will read on…
Awesome post.Much thanks again. Keep writing.
Looking forward to reading more. Great blog article.Really looking forward to read more. Will read on…
I think this is a real great blog post.Thanks Again. Really Cool.
I really liked your blog post.Thanks Again. Fantastic.
Appreciate you sharing, great blog post.Really looking forward to read more. Really Cool.
I appreciate you sharing this blog.Thanks Again. Much obliged.
Thank you ever so for you article. Fantastic.
Major thanks for the article.Really looking forward to read more. Want more.
I really liked your article.Much thanks again. Fantastic.
I really like and appreciate your article post.Thanks Again. Will read on…
Very informative post.Much thanks again. Will read on…
Thanks a lot for the blog post.Really looking forward to read more. Want more.
Im grateful for the blog article.Much thanks again. Awesome.
Say, you got a nice blog post.Much thanks again. Really Cool.
Thanks for sharing, this is a fantastic article.Really thank you! Cool.
Enjoyed every bit of your blog. Want more.
Very neat article post.Really thank you! Want more.
Say, you got a nice blog. Cool.
Great article.Really thank you!
Looking forward to reading more. Great blog.Thanks Again. Great.
Really enjoyed this post.Thanks Again.
Awesome article.Really thank you! Really Great.
Thanks for sharing, this is a fantastic article post.Thanks Again. Fantastic.
I think this is a real great article.Thanks Again. Want more.
Thanks-a-mundo for the article.Really thank you! Cool.
I really liked your blog post.Thanks Again. Really Cool.
I think this is a real great blog post.Really thank you! Great.
Very neat article.Really thank you! Fantastic.
Thanks again for the article.Thanks Again. Much obliged.
Looking forward to reading more. Great article.Really looking forward to read more. Keep writing.
Thanks again for the post.Much thanks again. Cool.
Awesome article. Fantastic.
Great post.Thanks Again. Fantastic.
Appreciate you sharing, great blog article.Much thanks again. Awesome.
Im grateful for the article.Really looking forward to read more. Keep writing.
I am so grateful for your article post.Thanks Again.
Awesome article post.Much thanks again. Much obliged.
Im grateful for the blog.Thanks Again. Fantastic.
A round of applause for your article.Really looking forward to read more. Will read on…
Very good article.Thanks Again. Fantastic.
Thank you ever so for you blog post.Thanks Again. Much obliged.
Im grateful for the blog post.Really thank you! Cool.
Very neat blog.Really looking forward to read more. Want more.
I cannot thank you enough for the article post.Really thank you! Want more.
I really like and appreciate your article.Much thanks again.
Major thankies for the post.Much thanks again. Much obliged.
Enjoyed every bit of your article post.Really thank you! Want more.
I really like and appreciate your post.Thanks Again.
Thanks so much for the article post.Thanks Again. Fantastic.
A round of applause for your blog article.Really thank you! Really Great.
wow, awesome blog post.Much thanks again. Really Cool.
Major thanks for the article.Much thanks again. Will read on…
I value the article.Much thanks again. Cool.
A round of applause for your blog.Really thank you! Will read on…
I really enjoy the article post.Much thanks again. Cool.
Thanks for sharing, this is a fantastic blog post. Much obliged.
Really appreciate you sharing this blog article.Much thanks again. Awesome.
Thanks so much for the article post. Much thanks again. Awesome.
Say, you got a nice blog. Cool.
I really like and appreciate your blog article. Much thanks again.
I really liked your article post.Really thank you! Will read on…
Thanks again for the article post.Really thank you! Want more.
Hey, thanks for the blog article. Really looking forward to read more. Will read on…
wow, awesome blog article.Much thanks again. Want more.
Looking forward to reading more. Great blog article.Thanks Again. Will read on…
Muchos Gracias for your post. Really looking forward to read more. Keep writing.
Thanks so much for the article post. Really Cool.
Thanks so much for the article post.Much thanks again.
Thanks a lot for the article post.Much thanks again. Really Cool.
Thank you ever so for you article post.Really thank you! Cool.
Thanks for the article.Really looking forward to read more. Cool.
I really like and appreciate your blog post.Much thanks again.
This is one awesome blog article.Thanks Again. Really Great.
Pretty! This has been an extremely wonderful article. Thanks for supplying this information.
That is a good tip especially to those fresh to the blogosphere. Short but very accurate information… Many thanks for sharing this one. A must read post!
Howdy! I could have sworn I’ve visited your blog before but after browsing through some of the posts I realized it’s new to me. Regardless, I’m certainly delighted I stumbled upon it and I’ll be bookmarking it and checking back regularly!
Everyone loves it when individuals get together and share thoughts. Great website, continue the good work!
Aw, this was a really good post. Spending some time and actual effort to make a good article… but what can I say… I put things off a whole lot and don’t manage to get anything done.
You are so awesome! I don’t think I’ve truly read something like this before. So great to find somebody with a few genuine thoughts on this topic. Seriously.. thank you for starting this up. This site is one thing that is required on the internet, someone with a bit of originality.
This site definitely has all the info I wanted concerning this subject and didn’t know who to ask.
I used to be able to find good advice from your blog posts.
This site was… how do you say it? Relevant!! Finally I’ve found something that helped me. Cheers!
Excellent post. I definitely appreciate this website. Keep it up!
This web site definitely has all the information and facts I wanted concerning this subject and didn’t know who to ask.
It’s difficult to find educated people on this subject, however, you sound like you know what you’re talking about! Thanks
Aw, this was an incredibly nice post. Taking a few minutes and actual effort to create a superb article… but what can I say… I hesitate a whole lot and never seem to get anything done.
Very good info. Lucky me I discovered your site by chance (stumbleupon). I’ve book marked it for later!
bookmarked!!, I love your web site!
I love it whenever people get together and share views. Great website, stick with it.
This site really has all of the info I needed concerning this subject and didn’t know who to ask.
This is a topic that is near to my heart… Cheers! Where can I find the contact details for questions?
I like reading a post that will make people think. Also, thanks for permitting me to comment.
You have made some really good points there. I checked on the net for additional information about the issue and found most people will go along with your views on this site.
I like reading through a post that will make people think. Also, many thanks for allowing me to comment.
Aw, this was a really good post. Taking the time and actual effort to produce a really good article… but what can I say… I procrastinate a lot and don’t manage to get anything done.
A round of applause for your post.Thanks Again. Really Cool.
Everything is very open with a very clear explanation of the challenges. It was really informative. Your site is useful. Many thanks for sharing!
Pretty! This was an incredibly wonderful article. Thanks for providing this information.
This site was… how do I say it? Relevant!! Finally I have found something that helped me. Appreciate it!
I really like and appreciate your blog post. Much obliged.
Very good information. Lucky me I discovered your site by chance (stumbleupon). I’ve saved it for later!
Excellent blog you have got here.. It’s difficult to find excellent writing like yours these days. I truly appreciate people like you! Take care!!
Howdy! I just would like to offer you a big thumbs up for the great info you have right here on this post. I’ll be returning to your web site for more soon.
I like it when folks get together and share thoughts. Great blog, keep it up.
Great info. Lucky me I recently found your blog by accident (stumbleupon). I have book-marked it for later!
There is certainly a great deal to find out about this issue. I really like all of the points you’ve made.
This is a topic that’s near to my heart… Thank you! Exactly where are your contact details though?
This website was… how do I say it? Relevant!! Finally I have found something which helped me. Kudos!
There’s certainly a lot to find out about this issue. I like all of the points you made.
There’s certainly a great deal to find out about this subject. I really like all of the points you have made.
There’s definately a great deal to learn about this topic. I love all the points you’ve made.
Howdy! I simply want to give you a big thumbs up for the great info you have here on this post. I’ll be coming back to your website for more soon.
This site was… how do I say it? Relevant!! Finally I’ve found something that helped me. Kudos.
bookmarked!!, I really like your site.
Very good article. I definitely love this site. Keep writing!
Great article! We are linking to this great post on our website. Keep up the good writing.
I used to be able to find good info from your content.