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.
http://www.bkjia.com/PHPjc/942988.htmlwww.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...