日销量排行榜

1.热门的数据(商品属于热数据而不是排行榜)

2.每天定时更新(24小时才会更新一次)延迟

3.倒叙的排序 =》order by 的查询  10条limit 

1.查询redis的有序集合    有序集合有分数    销量  ,查询出销量排行前10的商品id

2.通过商品id去查询,redis中string类型存储的商品详细信息

为什么不用hash 而是用string?

hash类型 灵活性更好,经常需要进行用作更新的数据

string 不需要经常修改的数据

3.如果没有查询到排行前10的商品,就需要到mysql中查询

4.mysql查询出来后,将商品id按照销量作为有序集合的分数写入redis

5.商品的详细信息通过id为key写入redis

6.返回结果

select id,name sold_count from lmrs_products order by sold_count desc limit 10;

加索引

alter table lmrs_products add index idx_sold_count_name(sold_count,'name')

有group by 以及order by 的sql语句,要加索引

redis管道

客户端向服务端发送一个查询请求,并监听socket返回

通常是以阻塞模式,等待服务端响应

服务端处理命令,并将结果返回给客户端

通过pipeline方式当有大批量的操作时候,我们可以节省很多原来浪费在网络延时的时间

Redis::pipeline(function ($redis) use($productSoldCountData){
$redis->del("lmrs::index::product::queue");
foreach ($productSoldCountData as $item){
$redis->zadd("lmrs::index::product::queue",$item['sold_count'],$item['id']);
$redis->set("lmrs::index::product::".$item["id"],serialize($item),"EX",86400);
$redis->expires("lmrs::index::product::queue",86400);
}
});
原文地址:https://www.cnblogs.com/gaosf/p/14952454.html