Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using PHP, MySQL and Redis

I'm trying to understand how to cache databases in a good way and what to do and what not to do. Though I want to make sure that I've understood everything correctly so far. Maybe you can help me?

At the moment I don't have any caching and I do everything locally using XAMPP. For storing data I use MySQL/PHPMyAdmin and to grab, add and update data I simply make Mysqli queries in PHP. As I said, I don't have any caching as of now, so I started looking for the best ways to do it.

After a quick search I found Redis. So my idea is to store data using MySQL and cache it with Redis. Is this a good way to do it or have I understood everything completely wrong?

If I'm right, how do I implement Redis? How do I cache data? Do I make a check in PHP if the data I want is cached, if it is then take it from the cache, else take it from the MySQL database.

Would really like to know if I'm on the right track.

like image 944
Oskar Persson Avatar asked Oct 03 '12 16:10

Oskar Persson


People also ask

How connect PHP to Redis?

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("tutorial-name", "Redis tutorial"); // Get the stored data and print it echo "Stored string in redis:: " .

How connect MySQL to Redis?

Connect to Redis through the SQL GatewayIn MySQL Workbench, click to add a new MySQL connection. Name the connection (CData SQL Gateway for Redis). Set the Hostname, Port, and Username parameters to connect to the SQL Gateway. Click Store in Vault to set and store the password.

Is Redis better than MySQL?

Overall, you don't really compare Redis and MySQL : if you want reliable relational storage, you use MySQL (or another DBMS) if you can afford to lose a few seconds of data in case of crash and you only need to fast store and retrieve key-value pairs, then Redis might be interesting.


1 Answers

Redis is an open source data structure server with an in-memory dataset that does much more than simple key/value storage thanks to its built-in data types.

If your requirement is to cache bulk data, you can go for it and here is a quick tutor for it

If you just want to achieve caching you can use memcache

memcache is good for storing slow queries that return small data sets [1 - 50 results, depending on the average row weight]

memcache is not so efficient for any query that returns large data sets [100 - ∞, depending on the average row weight]

Here are some of the links where you can find more info on memcache

Memcache introduction

using memcache with php and mysql

and also, last but not least

Mysql also caches results of queries, may be you can increase mysql query cache size? Or cache the result of big query in a standalone table.

More information on Mysql query caching is provided in many of the SO postings but the best one I felt to present is the one here

Mysql query caching

The above SO posting may help you out

like image 90
Aravind.HU Avatar answered Oct 09 '22 05:10

Aravind.HU