Summary of sort sorting functions in php-redis

Source: Internet
Author: User
This article mainly introduces the summary of the sort sorting function in php-redis. This article describes sorting by letter, sorting part of data, sorting by external keys, and other sorting methods. It also provides code examples, if you need redis, you can refer to a lot of people who think of redis as a database. In fact, redis is used to construct a database model, which has the taste of a database. However, the relationship between key and value is still different from that of a real relational database.

High efficiency, inconvenient; convenient, low efficiency; convenient, high efficiency requires money.

The sort function in php-redis is easy to retrieve data while making the web. it is a bit of a relational database. Before talking about sort, let's first talk about some of the frequently used functions leaked earlier.

1) keys

Description: searches for keys that match the specified mode.
Parameter: matching mode
Returned value: the list of keys that match the specified mode.

2) mset

Description: One or more key-value pairs are set at the same time. If a key with the same name exists, MSET overwrites the old value with the new value. if you do not want to overwrite the key with the same name, use the MSETNX command. MSET is an atomic operation. all given keys are set at the same time. some given keys are updated while others are not changed.
Parameters: Array
Returned value: always returns OK (because MSET cannot fail)

3) mget

Description: return the values of all (one or more) given keys. If a specified key does not exist, a special value nil is returned. Therefore, this command never fails.
Parameter: array of keys
Returned value: a list containing the values of all given keys

Example:

The code is as follows:


<? Php
$ Redis = new redis ();
$ Redis-> connect ('192. 168.1.108 ', 192 );
$ Redis-> flushall ();

$ Array = array ('tank' => '1 ',
'Zhang '=> '2 ',
'In' => '3 ',
'Test' => '4 ');
$ Redis-> mset ($ array );
Print_r ($ redis-> keys ('* s *'); // result: Array ([0] => test)
Print_r ($ redis-> keys ('Y ??? '); // Result: Array ([0] => ying)
Print_r ($ redis-> keys ('t [e] * '); // result: Array ([0] => test)
Print_r ($ redis-> keys ('*'); // result: array ([0] => ying [1] => test [2] => zhang [3] => tank)

Print_r ($ redis-> mget (array ("tank", "ying"); // result: Array ([0] => 1 [1] => 3)
?>

4) sort

Description: data is obtained based on conditions.
Parameters:

The code is as follows:


Array (
'By' => 'pattern', // matching mode
'Limit' => array (0, 1 ),
'Get' => 'pattern'
'Sort '=> 'asc' or 'desc ',
'Alpha' => TRUE,
'Store' => 'External-key'
)


Returns or stores the sorted elements in the given list, set, and sorted set key.

General sorting

The code is as follows:


<? Php
$ Redis = new redis ();
$ Redis-> connect ('192. 168.1.108 ', 192 );
$ Redis-> flushall ();
$ Redis-> lpush ('test', 1 );
$ Redis-> lpush ('test', 10 );
$ Redis-> lpush ('test', 8 );

Print_r ($ redis-> sort ('test'); // result: Array ([0] => 1 [1] => 8 [2] => 10)
?>

Letter Sorting

The code is as follows:


<? Php
$ Redis = new redis ();
$ Redis-> connect ('192. 168.1.108 ', 192 );
$ Redis-> flushall ();
$ Redis-> lpush ('test', 'A ');
$ Redis-> lpush ('test', 'D ');
$ Redis-> lpush ('test', 'B ');

Print_r ($ redis-> sort ('test'); // result: Array ([0] => B [1] => d [2] =>)
Print_r ($ redis-> sort ('test', array ('alpha' => TRUE); // result: array ([0] => a [1] => B [2] => d)
?>

Sort part of data

The code is as follows:


<? Php
$ Redis = new redis ();
$ Redis-> connect ('192. 168.1.108 ', 192 );
$ Redis-> flushall ();
$ Redis-> lpush ('test', 31 );
$ Redis-> lpush ('test', 5 );
$ Redis-> lpush ('test', 2 );
$ Redis-> lpush ('test', 23 );

$ Array = array ('limit' => array (), "SORT" => 'desc ');
Print_r ($ redis-> sort ('test', $ array); // result: array ([0] => 31 [1] => 23 [2] => 5)
?>

Sort by external key

Sometimes you want to use an external key as the weight to compare elements, instead of the default comparison method.

Assume that the user table data is as follows:

The code is as follows:


Id name score
-------------------------------
1 tank 89
2 zhang 40
4 ying 70
3 fXXK 90

Id data is saved in the list with the key name id.
Name data is saved in the list of key names: name _ {id}
Score data is stored in the key of score _ {id.

The code is as follows:


<? Php
$ Redis = new redis ();
$ Redis-> connect ('192. 168.1.108 ', 192 );
$ Redis-> flushall ();

$ Redis-> lpush ('id', 1 );
$ Redis-> set ('name _ 1', 'tank ');
$ Redis-> set ('score _ 1', 89 );

$ Redis-> lpush ('id', 2 );
$ Redis-> set ('name _ 2', 'Zhang ');
$ Redis-> set ('score _ 2', 40 );

$ Redis-> lpush ('id', 4 );
$ Redis-> set ('name _ 4', 'ying ');
$ Redis-> set ('score _ 4', 70 );

$ Redis-> lpush ('id', 3 );
$ Redis-> set ('name _ 3', 'fxxk ');
$ Redis-> set ('score _ 3', 90 );

/**
* Sort by score in ascending order and obtain the id.
*/
$ Sort = array ('by' => 'score _*',
'Sort '=> 'desc'
);
Print_r ($ redis-> sort ('id', $ sort); // result: array ([0] => 3 [1] => 1 [2] => 4 [3] => 2)

/**
* Sort by score in ascending order and obtain the name
*/
$ Sort = array ('by' => 'score _*',
'Sort '=> 'desc ',
'Get' => 'name _*'
);
Print_r ($ redis-> sort ('id', $ sort); // result: array ([0] => fXXK [1] => tank [2] => ying [3] => zhang)

/**
* Sort by score from small to large to get name and score
*/
$ Sort = array ('by' => 'score _*',
'Sort '=> 'desc ',
'Get' => array ('name _ * ', 'Score _*')
);
Print_r ($ redis-> sort ('id', $ sort ));
/**
* Result: Array
(
[0] => fXXK
[1] => 90
[2] => tank
[3] => 89
[4] => ying
[5] => 70
[6] => zhang
[7] => 40
))
*/

/**
* Sort by score from small to large to get id, name, score
*/
$ Sort = array ('by' => 'score _*',
'Sort '=> 'desc ',
'Get' => array ('#', 'Name _ * ', 'Score _*')
);
Print_r ($ redis-> sort ('id', $ sort ));
/**
* Result: Array
(
[0] => 3
[1] => fXXK
[2] => 90
[3] => 1
[4] => tank
[5] => 89
[6] => 4
[7] => ying
[8] => 70
[9] => 2
[10] => zhang
[11] => 40
)
*/
?>

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.