zoukankan      html  css  js  c++  java
  • PHP+Redis实现排行榜

    通过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;
        }

  • 相关阅读:
    Charles抓包使用教程
    学习规划
    log重复数据
    CNN实现推特文本分类
    conda env
    matplotlib画基础表
    决策树算法
    sklearn实现决策树
    分词与文本预处理工具
    matplotlib作图学习(1)
  • 原文地址:https://www.cnblogs.com/dawuge/p/14660731.html
Copyright © 2011-2022 走看看