search
Homephp教程php手册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
            )
     */
?>

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)