PHP implementation of Redis database Specify library number migration Method _php Tutorial

Source: Internet
Author: User

PHP implementation of REDIS database specified library number migration method


This article mainly introduces the implementation of the Redis database library number migration method, involving the operation of the Redis database skills, very practical value, the need for friends can refer to the following

The example of this paper describes the implementation of the Redis database to specify the library number migration method, share to everyone for your reference. Specific as follows:

Redis Normal Database migration, only the entire Redis save, or use master and slave, of course, can also install a redis-dump, but more trouble, here is a PHP script to achieve the specified library number of the migration, in fact, the traversal according to the storage type, read out, insert a new library, The effect is this:

The code is as follows:

[Root@localhost ~]# php 1.php
1/407
101/407
201/407
301/407
401/407


The PHP instance 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/$total \ n";
}
$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 is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/971927.html www.bkjia.com true http://www.bkjia.com/PHPjc/971927.html techarticle PHP to implement the method of specifying library number migration for Redis database This article mainly introduces the method of PHP to realize the transfer of database number of Redis databases, and it involves the operation technique of Redis database.

  • 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.