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



Redis cluster mode deploys Redis instances to multiple servers through sharding, improving scalability and availability. The construction steps are as follows: Create odd Redis instances with different ports; Create 3 sentinel instances, monitor Redis instances and failover; configure sentinel configuration files, add monitoring Redis instance information and failover settings; configure Redis instance configuration files, enable cluster mode and specify the cluster information file path; create nodes.conf file, containing information of each Redis instance; start the cluster, execute the create command to create a cluster and specify the number of replicas; log in to the cluster to execute the CLUSTER INFO command to verify the cluster status; make

The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.

How to clear Redis data: Use the FLUSHALL command to clear all key values. Use the FLUSHDB command to clear the key value of the currently selected database. Use SELECT to switch databases, and then use FLUSHDB to clear multiple databases. Use the DEL command to delete a specific key. Use the redis-cli tool to clear the data.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

To read a queue from Redis, you need to get the queue name, read the elements using the LPOP command, and process the empty queue. The specific steps are as follows: Get the queue name: name it with the prefix of "queue:" such as "queue:my-queue". Use the LPOP command: Eject the element from the head of the queue and return its value, such as LPOP queue:my-queue. Processing empty queues: If the queue is empty, LPOP returns nil, and you can check whether the queue exists before reading the element.

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.
