php-redis中的sort排序函数总结,php-redissort
php-redis中的sort排序函数总结,php-redissort
很多人把redis当成一种数据库,其实是利用redis来构造数据库的模型,有那种数据库的味道。但是在怎么构建还是key和value的关系,与真正的关系型数据库还是不一样的。
效率高,不方便;方便的,效率不高;又方便,效率又高的要花钱。
php-redis里面的sort函数,在做web的时候取数据还是比较方便,有点关系型数据库的味道。在说sort前,先说一下前面漏的几个比较常用的函数。
1) keys
描述:查找符合给定模式的key
参数:匹配模式
返回值:符合给定模式的key列表
2) mset
描述:同时设置一个或多个key-value对。当发现同名的key存在时,MSET会用新值覆盖旧值,如果你不希望覆盖同名key,请使用MSETNX命令。MSET是一个原子性(atomic)操作,所有给定key都在同一时间内被设置,某些给定key被更新而另一些给定key没有改变的情况,不可能发生。
参数:数组
返回值:总是返回OK(因为MSET不可能失败)
3) mget
描述:返回所有(一个或多个)给定key的值。如果某个指定key不存在,那么返回特殊值nil。因此,该命令永不失败。
参数:key的数组
返回值:一个包含所有给定key的值的列表
示例:
复制代码 代码如下:
$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*')); // 结果:Array ( [0] => test )
print_r($redis->keys('y???')); // 结果:Array ( [0] => ying )
print_r($redis->keys('t[e]*')); // 结果:Array ( [0] => test )
print_r($redis->keys('*')); // 结果:Array ( [0] => ying [1] => test [2] => zhang [3] => tank )
print_r($redis->mget(array("tank","ying"))); // 结果:Array ( [0] => 1 [1] => 3 )
?>
4) sort
描述:按条件取得数据
参数:
复制代码 代码如下:
array(
'by' => 'pattern', //匹配模式
'limit' => array(0, 1),
'get' => 'pattern'
'sort' => 'asc' or 'desc',
'alpha' => TRUE,
'store' => 'external-key'
)
返回或保存给定列表、集合、有序集合key中经过排序的元素。
一般排序
复制代码 代码如下:
$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')); //结果:Array ( [0] => 1 [1] => 8 [2] => 10 )
?>
字母排序
复制代码 代码如下:
$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')); //结果:Array ( [0] => b [1] => d [2] => a )
print_r($redis->sort('test',array('ALPHA'=>TRUE))); //结果:Array ( [0] => a [1] => b [2] => d )
?>
排序取部分数据
复制代码 代码如下:
$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)); //结果:Array ( [0] => 31 [1] => 23 [2] => 5 )
?>
使用外部key进行排序
有时候你会希望使用外部的key作为权重来比较元素,代替默认的对比方法。
假设现在有用户(user)表数据如下:
复制代码 代码如下:
id name score
-------------------------------
1 tank 89
2 zhang 40
4 ying 70
3 fXXK 90
id数据保存在key名为id的列表中。
name数据保存在key名为name_{id}的列表中
score数据保存在score_{id}的key中。
复制代码 代码如下:
$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);
/**
* 按score从大到小排序,取得id
*/
$sort=array('BY'=>'score_*',
'SORT'=>'DESC'
);
print_r($redis->sort('id',$sort)); //结果:Array ( [0] => 3 [1] => 1 [2] => 4 [3] => 2 )
/**
* 按score从大到小排序,取得name
*/
$sort=array('BY'=>'score_*',
'SORT'=>'DESC',
'GET'=>'name_*'
);
print_r($redis->sort('id',$sort)); //结果:Array ( [0] => fXXK [1] => tank [2] => ying [3] => zhang )
/**
* 按score从小到大排序,取得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
))
*/
/**
* 按score从小到大排序,取得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
)
*/
?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.
