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