I've been studying Key-value's storage for a while, and simply remember how it feels. The installation and use of some memcache and Redis will not be mentioned. Just simply say the difference between the two options. Some impressions and test results may not be enough to explain the problem, please correct me if there is anything wrong with you. Because these two days in the process of learning to find that they have been correcting the shortcomings of their own understanding, every day, will negate the idea of the morning before. All right, crap less. A study of 500,000 data stores found that: Number of single instruction executions per second Memcache approx. 30,000 times Redis approx. 10,000 times Moreover, one of the great advantages of memcache is that it is possible to set the expiration time directly through a function, and redis needs two functions to set both the key value pair and the expiration time, that is, the efficiency of redis at this point becomes the original half, that is, 5,000 times, which for most of the demand, a bit too slow. The test code for Memcache is as follows: <?php $mem = new Memcache; $mem->connect ("127.0.0.1", 11211); $time _start = Microtime_float (); Save data for ($i = 0; $i < 100000; $i + +) { $mem->set ("key$i", $i, 0, 3); } $time _end = Microtime_float (); $run _time = $time _end-$time _start; echo "spents $run _time s \ n"; function Microtime_float () { List ($usec, $sec) = Explode ("", Microtime ()); return (float) $usec + (float) $sec); } ?> The test code for Redis is as follows: redis1.php This code takes about 10 seconds <?php Connection $redis = new Redis (); $redis->connect (' 127.0.0.1 ', 6379); $time _start = Microtime_float (); Save data for ($i = 0; $i < 100000; $i + +) { $redis->sadd ("key$i", $i); } $time _end = Microtime_float (); $run _time = $time _end-$time _start; echo "spents $run _time s \ n"; Close connection $redis->close (); function Microtime_float () { List ($usec, $sec) = Explode ("", Microtime ()); return (float) $usec + (float) $sec); } ?> If you need to set the expiration time while setting the key value, it takes about 20 seconds to execute, and the test code is as follows: redis2.php <?php Connection $redis = new Redis (); $redis->connect (' 127.0.0.1 ', 6379); $time _start = Microtime_float (); Save data for ($i = 0; $i < 100000; $i + +) { $redis->sadd ("key$i", $i); $redis->expire ("Key$i", 3); } $time _end = Microtime_float (); $run _time = $time _end-$time _start; echo "spents $run _time s \ n"; Close connection $redis->close (); function Microtime_float () { List ($usec, $sec) = Explode ("", Microtime ()); return (float) $usec + (float) $sec); } ?> Later on the internet discovered that Redis has a magical function called transactions, through the multi atom of a block of code executed sequentially, so as to achieve a complete function module execution. Unfortunately, the test found that the execution of the code in the multi way did not reduce the number of requests, instead of executing the multi instruction and the EXEC command to send the request, thus turning the run time to four times times the original, that is, four instructions run time. The test code is as follows: redis3.php <?php Connection $redis = new Redis (); $redis->connect (' 127.0.0.1 ', 6379); $time _start = Microtime_float (); Save data for ($i = 0; $i < 100000; $i + +) { $redis->multi (); $redis->sadd ("key$i", $i); $redis->expire ("Key$i", 3); $redis->exec (); } $time _end = Microtime_float (); $run _time = $time _end-$time _start; echo "spents $run _time s \ n"; Close connection $redis->close (); function Microtime_float () { List ($usec, $sec) = Explode ("", Microtime ()); return (float) $usec + (float) $sec); } ?> Problems have bottlenecks, there are a lot of companies need massive data processing, 5,000 times per second far from meeting demand, and then because the Redis master-slave server than memcache have a greater advantage, for the sake of future data, have to use Redis, this time there is a new way, That is, Phpredis provides the Pipline function, this function can truly encapsulate a few pieces of code as a request, which greatly improves the speed of operation, 500,000 times the data execution only 58 seconds. The test code is as follows: redis4.php <?php Connection $redis = new Redis (); $redis->connect (' 127.0.0.1 ', 6379); $time _start = Microtime_float (); Save data for ($i = 0; $i < 100000; $i + +) { $pipe = $redis->pipeline (); $pipe->sadd ("key$i", $i); $pipe->expire ("Key$i", 3); $replies = $pipe->execute (); } $time _end = Microtime_float (); $run _time = $time _end-$time _start; echo "spents $run _time s \ n"; Close connection $redis->close (); function Microtime_float () { List ($usec, $sec) = Explode ("", Microtime ()); return (float) $usec + (float) $sec); } ?> This operation can be very perfect to package the assignment operation and set expiration time operation to a request to execute, greatly improve the efficiency of operation. Redis Installation: http://mwt198668.blog.163.com/blog/static/48803692201132141755962/ Memcache Installation: http://blog.csdn.net/barrydiu/article/details/3936270 Redis Settings Master-slave server: http://www.jzxue.com/fuwuqi/fuwuqijiqunyuanquan/201104/15-7117.html Memcache setting up a master-slave server: http://www.cnblogs.com/yuanermen/archive/2011/05/19/2051153.html |