PHP method to implement migration of specified library number in redis database, redis database_PHP tutorial

WBOY
Release: 2016-07-13 10:09:42
Original
872 people have browsed it

php method to implement migration of specified library number in redis database, redis database

The example in this article describes the method of migrating the specified library number of the redis database in PHP, and shares it with everyone for your reference. The details are as follows:

Redis ordinary database migration can only save the entire redis, or use master-slave. Of course, you can also install a redis-dump, but it is more troublesome. Here is a php script to realize the migration of the specified library number. In fact, it is also It just traverses according to the storage type, reads it out, and inserts it into the new library. The effect is like this:

Copy code The code is as follows:
[root@localhost ~]# php 1.php
1/407
101/407
201/407
301/407
401/407

The PHP example code is as follows:
Copy code The code is as follows:
$from = '10.0.2.52:6379/7';
$to = '127.0.0.1:6379/7';
$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;
}
?>

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/942988.htmlTechArticleHow to migrate the designated library number of the redis database in php. The example of this article tells about the implementation of the designated library number of the redis database in php. The migration method is shared with everyone for your reference. Specifically...
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!