Redis php Sort function

Source: Internet
Author: User

Many people think of Redis as a database, in fact, the use of Redis to construct a database model, with that kind of database flavor. But the relationship between the key and value is how it is built. The root of a real relational database is still different. High efficiency, inconvenient, convenient, inefficient, convenient, efficient and expensive. Php-redis inside the sort function, in doing the web time to take data is more convenient, a bit relational database flavor. Before you say sort, let's talk about some of the more commonly used functions in front of the leak.

1,keys

Description: Finds keys that match a given pattern.
Parameters: Matching mode
Return value: A list of keys that conform to the given pattern.

Example 1

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 where all given keys are set at the same time, some given key is updated and some other given key does not change, it cannot happen.
Parameters: Arrays
Return value: Always return OK (because Mset cannot fail)

Example 1

3,mget

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

<?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);p Rint_r ($redis->keys (' *s* ')); Result: Array ([0] = test) Print_r ($redis->keys (' y??? ')); Results: Array ([0] = ying) print_r ($redis->keys (' t[e]* ')); Result: Array ([0] = test) Print_r ($redis->keys (' * ')); Result: Array ([0] = ying [1] = = Test [2] = = [3] = tank) print_r ($redis->mget (Array ("Tank", "Ying")) ;  Result: Array ([0] = 1 [1] = 3)?>

  

4,sort

Description: Get data by criteria
Parameters:
Array
' By ' + ' pattern ',//matching 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

<?php$redis = new Redis (), $redis->connect (' 192.168.1.108 ', 6379); $redis->flushall (); $redis->lpush (' Test ', 1), $redis->lpush (' Test '), $redis->lpush (' Test ', 8);p Rint_r ($redis->sort (' Test ')); Result: Array ([0] = 1 [1] = 8 [2] = +)?>

  

Alphabetical order

<?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 ');p Rint_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 and fetch part of the data

<?php$redis = new Redis (), $redis->connect (' 192.168.1.108 ', 6379); $redis->flushall (); $redis->lpush (' Test ', +), $redis->lpush (' Test ', 5), $redis->lpush (' Test ', 2), $redis->lpush (' Test ', ")  ; $array = Array (' LIMIT ' =>array (0,3), ' SORT ' = ' DESC ');p rint_r ($redis->sort (' Test ', $array));  Result: Array ([0] = [1] = [2] = 5)?>

  

Sort using external key

Sometimes you will want to use external keys as weights to compare elements instead of the default contrast method.

Suppose you now have user table data as follows:

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

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

<?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 ',); $redis->lpush (' id ', 2) ; $redis->set (' name_2 ', ' Zhang '), $redis->set (' score_2 ', +); $redis->lpush (' id ', 4); $redis->set (' Name_4 ', ' Ying '); $redis->set (' score_4 '); $redis->lpush (' id ', 3); $redis->set (' name_3 ', ' fxxk '); $redis->set (' Score_3 ', 90);/** * Sort by score from large to small, get ID */$sort =array (' by ' + ' score_* ', ' sort ' = ' DESC ');p Rint_ R ($redis->sort (' id ', $sort));             Results: Array ([0] = 3 [1] = 1 [2] = 4 [3] = 2)/** * Score from large to small, get name */$sort =array (' by ' = ' score_* ', ' SORT ' = ' DESC ', ' GET ' = ' name_* ');p rint_r ($redis->sort (' id ', $sort)); Results: Array ([0] = fxxk [1] = tank [2] = ying [3] + Zhang)/** * score from small to large, get Name,score */$sort =array       (' by ' = ' score_* ', ' SORT ' = ' DESC ',     ' GET ' =>array (' name_* ', ' score_* '));p Rint_r ($redis->sort (' id ', $sort));/** * Result: Array (            [0] = fxxk [1] = [2] = tank [3] = [4] = ying [5] = [6] = Zhang [7] = 40)) *//** * Press score from small to large, get id,name,score */$s            Ort=array (' by ' = ' = ' score_* ', ' SORT ' = ' DESC ', ' GET ' =>array (' # ', ' name_* ', ' score_* ')            );p Rint_r ($redis->sort (' id ', $sort));/** * Results: Array ([0] = 3 [1] = Fxxk [2] = [3] = 1 [4] = tank [5] = [6] = 4 [7 ] = ying [8] = [9] = 2 [Ten] = Zhang [one] = 40) */?>

  

Redis php Sort function

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.