Php: how to migrate the specified database number of a redis database _ PHP Tutorial

Source: Internet
Author: User
Php implements the migration of the specified database number of the redis database. Php: how to migrate the specified database number of a redis database this article mainly introduces how to migrate the specified database number of a redis database in php, involving the operation skills of the redis database, methods for migrating the specified database number of apsaradb for redis using php

This article mainly introduces how to migrate the specified database number of the redis database using php, which involves the operation skills of the redis database and is of great practical value. For more information, see

The example in this article describes how to migrate the specified database number of the redis database using php. This article is for your reference. The details are as follows:

For general redis database migration, you can only save the entire redis database, or use the master and slave databases. of course, you can also install a redis-dump, but it is quite troublesome. Here we provide a php script, to migrate the specified database number, you can traverse the database according to the storage type, read the database, and insert the new database. The effect is as follows:

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: 8080 ';
$ From_redis = redis_init ($ from );
$ To_redis = redis_init ($ );
$ 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 will help you with php programming.

This article describes how to migrate the specified database number of apsaradb for redis in php. it involves the operation skills of apsaradb for redis...

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.