Home >Backend Development >PHP Tutorial >Summary of sort sorting function in php-redis, php-redissort_PHP tutorial
Many people regard redis as a kind of database. In fact, it is a model that uses redis to construct a database. There are some Database flavor. But how to build the relationship between key and value is still different from a real relational database.
High efficiency, but inconvenient; convenient, but not efficient; convenient, efficient but expensive.
The sort function in php-redis is more convenient for fetching data when doing web work, and it has a bit of a relational database flavor. Before talking about sort, let’s talk about some of the more commonly used functions that were missed earlier.
1) keys
Description: Find keys matching a given pattern
Parameters: match pattern
Return value: list of keys matching the given pattern
2) mset
Description: Set one or more key-value pairs at the same time. When a key with the same name is found, MSET will overwrite the old value with the new value. If you do not want to overwrite the key with the same name, please use the MSETNX command. MSET is an atomic operation. All given keys are set at the same time. It is impossible for some given keys to be updated while other given keys have not changed.
Parameter: Array
Return value: Always returns OK (because MSET cannot fail)
3) mget
Description: Returns the values of all (one or more) given keys. If a specified key does not exist, the special value nil is returned. Therefore, the command never fails.
Parameters: array of keys
Return value: a list containing all values of the given key
Example:
Copy code The code is as follows:
$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 based on conditions
Parameters:
Copy code The code is as follows:
array(
'by' => 'pattern', //matching pattern
'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, set, or ordered set key.
General sorting
Copy code The code is as follows:
$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 code The code is as follows:
$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 get some data
Copy code The code is as follows:
$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] => 31 [1] => 23 [2] => 5 )
?>
Use external key to sort
Sometimes you will want to use an external key as a weight to compare elements instead of the default comparison method.
Assume that the user table data is as follows:
Copy code The code is as follows:
id name score
----------------------------------
1 tank 89
2 Zhang 40
4 70
3 fXXK 90
The id data is stored in a list with the key named id.
The name data is stored in a list with the key name name_{id}
Score data is stored in the key of score_{id}.
复制代码 代码如下:
$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 large to small and get the id
*/
$sort=array('BY'=>'score_*',
'SORT'=>'DESC'
);
print_r($redis->sort('id',$sort)); //结果:Array ( [0] => 3 [1] => 1 [2] => 4 [3] => 2 )
/**
* * Sort by score from large to small and get name
*/
$sort=array('BY'=>'score_*',
'SORT'=>'DESC',
'GET'=>'name_*'
);
print_r($redis->sort('id',$sort)); //结果: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));
/**
*结果: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));
/**
* 结果: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
)
*/
?>