Home > Database > Redis > How to use PHP+Redis to implement ranking list

How to use PHP+Redis to implement ranking list

PHPz
Release: 2023-05-31 13:46:06
forward
882 people have browsed it

Implement a small functional ranking list through php and redis. The data type used is an ordered set: zrevrange ascending sorting, zrange descending sorting

/**
     * 排行榜     */public function rank()
    {// $this->zrem($this->cachekey);$this->redis->del($this->cachekey);$dataOne = [];for ($i=0; $i < 5; $i++) { 
          // 生成随机数$num = rand(0,100);// 生成随机字符串$str = $this->get_random(6,&#39;abcdefghijklmnopqrstuvwxyzABCDEFJHIJKLMNOPQRSTUVWXYZ&#39;);            $this->redis->zadd($this->cachekey,$num,json_encode([&#39;name&#39;=>$str]));// 由大到小排序$dataOne = $this->redis->ZREVRANGE($this->cachekey, 0, -1, true);// 由小到大排序$dataTow = $this->redis->ZRANGE($this->cachekey, 0, -1, true);
        }echo "<pre class="brush:php;toolbar:false">";print_r($dataOne);print_r($dataTow);

    }// 生成随机字符串public function get_random($len,$chars)
    {$hash = "";$max = strlen($chars) - 1;for ($i=0; $i < $len; $i++) { $hash .= $chars[mt_rand(0,$max)];
        }return $hash;
    }
Copy after login

How to use PHP+Redis to implement ranking list

The above is the detailed content of How to use PHP+Redis to implement ranking list. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template