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, the redis data that disappears can be saved in the disk regularly
Disaster recovery, after the memcache hangs up, the redis data that cannot be recovered 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 database, but also news queue, data stack, data cache, etc. Memcache is suitable for caching SQL statements, data sets, user temporary data, delayed query data, sessions, etc.
Example
Connecting to Redis service
<?php $redis = new redis(); //生成redis类的对象,生成之后可以用这个类里面的方法 $redis->connect('127.0.0.1',6379); //连接redis的ip地址端口号 $redis->set('redistest','666666'); // 给redistest赋值为666666 echo $redis->get('redistest'); //获取redistest的值 ?>
Usage 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' => "编程人,在天涯" ); $memcache -> set("info",$data,0,10); $info = $memcache->get("info"); echo '<pre class="brush:php;toolbar:false">'; print_r($info); ?>
The above is the detailed content of How to use Redis and Memcached in php. For more information, please follow other related articles on the PHP Chinese website!