Home > Backend Development > PHP Tutorial > PHP method to implement redis database specified library number migration_PHP tutorial

PHP method to implement redis database specified library number migration_PHP tutorial

WBOY
Release: 2016-07-13 10:01:48
Original
951 people have browsed it

How to migrate the designated library number of the redis database in PHP

This article mainly introduces the method of migrating the designated library number of the redis database in PHP, involving the operation skills of the redis database. It is of great practical value, friends in need can refer to it

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:

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:

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/971927.htmlTechArticleHow to migrate the specified library number of the redis database in php. This article mainly introduces the migration of the specified library number of the redis database in php. The method involves the operation skills of redis database, which is very useful...
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