Methods for exporting Redis data to another redis in PHP

Source: Internet
Author: User
Tags foreach hash php script redis

  This article mainly introduces the method of using PHP to export Redis data to another redis, you need friends can refer to the following

Export data from one Redis db to another Redis db PHP script:   Code 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% = 1) {        echo "$count/$totaln";   & nbsp }     $type = $from _redis->type ($key);     Switch ($type) {        redis::redis_string:           &NBS P $val = $from _redis->get ($key);             $to _redis->set ($key, $val);             break;         case redis::redis_list:             $list = $from _redis->lran GE ($key, 0,-1);             foreach ($list as $val) {                $to _reDis->rpush ($key, $val);                         break;         case Redis::redis_hash:             $hash = $from _redis->hget All ($key);             $to _redis->hmset ($key, $hash);             break;         case Redis::redis_zset:             $zset = $from _redis->zran GE ($key, 0,-1, true);             foreach ($zset as $val => $score) {            &NB Sp   $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; }  
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.