通过php和redis实现一个小功能排行榜,用的数据类型是有序集合:zrevrange 递增排序,zrange 递减排序
/**
* 排行榜 */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,'abcdefghijklmnopqrstuvwxyzABCDEFJHIJKLMNOPQRSTUVWXYZ'); $this->redis->zadd($this->cachekey,$num,json_encode(['name'=>$str]));// 由大到小排序$dataOne = $this->redis->ZREVRANGE($this->cachekey, 0, -1, true);// 由小到大排序$dataTow = $this->redis->ZRANGE($this->cachekey, 0, -1, true);
}echo "<pre>";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;
}
以上就是怎么用PHP+Redis实现排行榜的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号