Home > Backend Development > PHP Tutorial > How to export Redis data to another Redis in PHP_PHP tutorial

How to export Redis data to another Redis in PHP_PHP tutorial

WBOY
Release: 2016-07-13 10:36:02
Original
831 people have browsed it

This article mainly introduces the method of using PHP to export Redis data to another Redis. Friends in need can refer to it

PHP script to export data from one Redis db to another Redis db: ​ The code is as follows: $from = '127.0.0.1:6200/6'; $to = '127.0.0.1:6200/8'; ​ $from_redis = redis_init($from); $to_redis = redis_init($to); ​ $keys = $from_redis->keys('*'); $count = 0; $total = count($keys); foreach($keys as $key){ if(++$count % 100 == 1){ echo "$count/$totaln"; } $type = $from_redis->type($key); switch($type){ case Redis::REDIS_STRING:                   $val = $from_redis->get($key);                  $to_redis->set($key, $val);                     break; case Redis::REDIS_LIST: $list = $from_redis->lRange($key, 0, -1);               foreach($list as $val){                                                             $to_redis->rPush($key, $val);           }                     break; case Redis::REDIS_HASH: $hash = $from_redis->hGetAll($key);                   $to_redis->hMSet($key, $hash);                     break; case Redis::REDIS_ZSET:                 $zset = $from_redis->zRange($key, 0, -1, true);               foreach($zset as $val=>$score){                                                     $to_redis->zAdd($key, $score, $val);           }                     break; } } ​ function redis_init($conf){ $redis = new Redis(); Preg_match('/^([^:]+)(:[0-9]+)?\/(.+)?/', $conf, $ms); $host = $ms[1]; $port = trim($ms[2], ':'); $db = $ms[3]; $redis->connect($host, $port); $redis->select($db); Return $redis; }

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/740821.htmlTechArticleThis article mainly introduces the method of using PHP to export Redis data to another Redis. Friends who need it can refer to it. The following is a PHP script to export data from a Redis db to another Redis db:...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template