Summary of sort functions in Php-redis, php-redissort_php tutorial

Source: Internet
Author: User

Summary of sort functions in Php-redis, Php-redissort


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 how to build or key and value is not the same as a real relational database.

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: Find keys that match a given pattern
Parameters: Matching mode
Return value: List of keys that conform 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 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)

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

Example:
Copy the 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")); Result: Array ([0] = 1 [1] = 3)
?>

4) sort

Description: Get data by criteria
Parameters:
Copy the Code code as follows:
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
Copy the 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 ')); Result: Array ([0] = 1 [1] = 8 [2] = 10)
?>

Alphabetical order

Copy the 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 and fetch part of the data

Copy the 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)); 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:

Copy the Code code as follows:
ID Name Score
-------------------------------
1 Tank 89
2 Zhang 40
4 Ying 70
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}.

Copy CodeThe code is 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);

/**
* Sort by score from big to small, get 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 from big to small, get 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, 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
))
*/

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

http://www.bkjia.com/PHPjc/1028970.html www.bkjia.com true http://www.bkjia.com/PHPjc/1028970.html techarticle Summary of sort sorting functions in Php-redis, php-redissort many people use Redis as a database, in fact, using Redis to construct a database model, with that kind of database flavor. But in ...

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