Differences
1. Redis is a storage database. Memcache can also cache photos.
Redis and Memcache store data in memory and are memory databases. However, Memcache can also cache other things like photos and videos. Redis not only supports simple k/v type data, but also provides storage of data structures such as list, set, and hash.
Expiration strategy, memcache is specified when setting. For example, setkey1008 never expires. redis can be set via expire. For example, expirename10.
Storage security, after memcache is closed, redis data disappears and can be regularly saved on the disk.
Disaster recovery. After the redis data is lost and the data cannot be recovered after memcache hangs up, it can be restored through aof.
Redis supports data backup, that is, data backup in master-slave mode.
Different application scenarios:
2. Redis can create nosql database, news queue, etc. Memcache can also cache SQL statements.
Redis can not only make NoSQL databases, but also news queues, data stacks, data caches, etc. Memcache is suitable for caching SQL statements, data sets, user temporary data, delayed query data, sessions, etc.
Example
Connect to Redis service
<?php $redis = new redis(); //Generate an object of the redis class. After generation, you can use the method in this class $redis->connect('127.0.0.1',6379); //The IP address and port number to connect to redis$ redis->set('redistest','666666'); // Assign the value to redistest to 666666 echo $redis->get('redistest'); //Get the value of redistest?>
Use of Memcached
<?php $memcache = new Memcache; $memcache->connect("127.0.0.1",11211) or die("Memcached connected failed"); echo "Memcached's version: " . $memcache->getVersion() . "<br />"; $data = array( 'url' => "http://www.cnblogs.com/wujuntian/", 'name' => "Programmer, at the end of the world" ); $memcache -> set("info",$data,0,10); $info = $memcache->get("info"); echo '<pre>'; print_r($info); ?>
The above is the difference between Redis and Memcached in php. I hope it will be helpful to everyone.