Summary of the sort sort function in Php-redis _php skills

Source: Internet
Author: User
Tags redis

Many people regard Redis as a kind of database, in fact, use Redis to construct the database model, have that kind of database flavor. But the relationship between how to build or key and value is not the same as the real relational database.

High efficiency, inconvenient, convenient, inefficient, convenient, efficient and expensive to spend.

Php-redis inside the sort function, when doing the web to fetch data is more convenient, a bit of a relational database flavor. Before you say sort, let's say some of the more commonly used functions that are missing from the front.

1) Keys

Description: Finds key that matches a given pattern
Parameters: Matching mode
Return value: A key list that conforms to the given pattern

2) Mset

Description: Sets one or more key-value pairs at the same time. When a key with the same name is found, Mset overwrites the old value with the new value, and if you do not want to overwrite the key with the same name, use the MSETNX command. Mset is an atomic (atomic) operation in which all the given keys are set at the same time, and it is not possible for some given key to be updated and some other given key does not change.
Parameters: Arrays
Return value: Always return OK (because Mset cannot fail)

3) Mget

Description: Returns the value of all (one or more) given key. If a specified key does not exist, then return the special value nil. Therefore, the command never fails.
Parameters: Array of key
Return value: A list of values that contain all the given key

Example:

Copy Code code as follows:

<?php
$redis = new Redis ();
$redis->connect (' 192.168.1.108 ', 6379);
$redis->flushall ();

$array =array (' Tank ' => ' 1 '),
' Zhang ' => ' 2 ',
' Ying ' => ' 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")); Results: Array ([0] => 1 [1] => 3)
?>

4) sort

Description: Access to data by condition
Parameters:

Copy Code code as follows:

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

Returns or saves the sorted elements in the given list, collection, and ordered collection key.

General sort

Copy Code code as follows:

<?php
$redis = new Redis ();
$redis->connect (' 192.168.1.108 ', 6379);
$redis->flushall ();
$redis->lpush (' Test ', 1);
$redis->lpush (' Test ', 10);
$redis->lpush (' Test ', 8);

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

Alphabetical sort

Copy Code code as follows:

<?php
$redis = new Redis ();
$redis->connect (' 192.168.1.108 ', 6379);
$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] => a)
Print_r ($redis->sort (' Test ', Array (' ALPHA ' =>true)); Result: Array ([0] => a [1] => b [2] => D)
?>

Sort fetch Partial data

Copy Code code as follows:

<?php
$redis = new Redis ();
$redis->connect (' 192.168.1.108 ', 6379);
$redis->flushall ();
$redis->lpush (' Test ', 31);
$redis->lpush (' Test ', 5);
$redis->lpush (' Test ', 2);
$redis->lpush (' Test ', 23);

$array = Array (' LIMIT ' =>array (0,3), "SORT" => ' DESC ');
Print_r ($redis->sort (' Test ', $array)); Results: Array ([0] => [1] => [2] => 5)
?>

Use external key to sort

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

Suppose the user table data is now as follows:

Copy Code code as follows:

ID Name Score
-------------------------------
1 Tank 89
2 Zhang 40
4 Ying 70
3 FXXK 90

The ID data is saved in the list with the key name ID.
Name data is saved in the list of key named Name_{id}
Score data is stored in the key of Score_{id}.

Copy Code code as follows:

<?php
$redis = new Redis ();
$redis->connect (' 192.168.1.108 ', 6379);
$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);

/**
* Press score to sort from big to small, get ID
*/
$sort =array (' by ' => ' score_* '),
' SORT ' => ' DESC '
);
Print_r ($redis->sort (' id ', $sort)); Results: Array ([0] => 3 [1] => 1 [2] => 4 [3] => 2)

/**
* Press score to sort from big to small, get name
*/
$sort =array (' by ' => ' score_* '),
' SORT ' => ' DESC ',
' Get ' => ' name_* '
);
Print_r ($redis->sort (' id ', $sort)); Results: Array ([0] => fxxk [1] => tank [2] => Ying [3] => Zhang)

/**
* According to score from small to large sort, get Name,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
))
*/

/**
* According to score from small to large sort, 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
[A] => Zhang
[One] => 40
)
*/
?>

Related Article

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.