<?php
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$redis->select(2);//選擇數據庫1
$key = "hot";
//insert($redis, $key);
//get_rank_list($redis, $key);
get_rank($redis, $key, 'key29');
/**
* 插入數據
* @param $redis
* @param $key
*/
function insert($redis, $key)
{
$id = rand(1, 20);
$redis->zIncrBy($key, 1, 'key' . $id);
echo 'done';
}
/**
* 獲取 top N
* @param $redis
* @param $key
*/
function get_rank_list($redis, $key)
{
$top = 10; //獲取前10個
$list = $redis->zRevRange($key, 0, $top - 1, true);
print_r($list);
}
/**
* 獲取某個key的排名
* @param $redis
* @param $key
* @param $id
*/
function get_rank($redis, $key, $id)
{
echo $redis->zRank($key, $id);
}