像微博这些一个热门帖子就有其他的消息量,所以使用redis来进行数据的浏览和操作是很好的选择
public void addComment(Comment comment){
Long commentId = commentMappper.insert(comment);
String listKey = "weibo:"+comment.getWeiboId+"comments";
String hashKey = "weibo:"+comment.getWeiboId+":comment:content";
jedis.lpush(listKey,commentId.toString());
jedis.hset(hashKey,commentId.toString,comment.getConent());
//只要最新的5000条数据存在redis中
List ids = jedis.lrange(listKey,5000,-1);
for(String id : ids){
jedis.hdel(hashKey,id);
}
jedis.ltrim(listKey,0,5000);
}
public List queryComments(Long weiboId,QueryObject qo){
String listKey = "weibo:"+weiboId+"comments";
String hashKey = "weibo:"+weiboId+":comment:content";
Long start = (qo.getCurrentPage - 1) * qo.getPageSize;
Long end = start + qo.getPageSize - 1; // qo.getCurrentSize()*qo.getPageSize()-1;
List ids = jedis.lrange(listkey,start,end);
List result = new ArrayList<>();
if(ids.size
public void delete(Long weiboId,Long commentId){
String listKey = "weibo:"+weiboId+"comments";
tring hashKey = "weibo:"+weiboId+":comment:content";
jedis.lrem(listKey,commentId,1);
jedis.hdel(hashKey,commentId);
commentMapper.delete(Long commentId);
}
当一个需求是获取最近的浏览功能的时候,其实这个数据模型就应该马上出来,起码是有序对的存储,所以使用redis的list和zset都可以实现,使用list的时候只需要简单地保存就可以了,但是需要注意如果相近的两天浏览了相同商品,那么这个商品的浏览记录应该是最近的,也就是我们要删除之前的。同时,我们还需要设置(因为redis的过期删除是删除整个key,所以这里的有效时间是整个浏览历史的有效时间),而且浏览记录需要设置一个数量
public void addMemeberRecentGoods(Long memberId,Long templateId){
String key = "MemeberRecentGoods:"+memberId;
jedis.lrem(key,1,templateId.toString());
jedis.lpush(key,templated);
jedis.ltrim(key,0,59);
jedis.expire(key,60*60*24*30);
}
public Map queryMemberRecentGoods(Long memberId, QueryObject qo){
String key = "MemeberRecentGoods:"+memberId;
Long start = (qo.getCurrentPage - 1) * qo.getPageSize;
Long end = start + qo.getPageSize - 1; // qo.getCurrentSize()*qo.getPageSize()-1;
//获取总共条数,算出有多少页用于前台显示
Long totalCount = jedis.llen(key);
Integer totalPage = totalCount/pageSize == 0 ? totalCount/pageSize : totalCount/pageSize + 1;
List ids = jedis.lrange(key,start,end);
//查询出商品的信息(可以保存在数据库,也可以保存在redis)
List goods = recentGoodsService.get(ids);
Map map = new HashMap<>();
map.put("goods",goods);
map.put("totalCount",totalCount);
return map;
}
很多应用都有排行榜的功能,排行榜有两个特点,其一是按积分排名,其二就是需要有时间约束,排行榜是有时效性的,如果排行榜没有时效,那始终都是老用户在榜首,这对新用户并不公平,使用redis可以很高效地实现这个功能
伪代码:
public void addScore(String nowDate, Long userId, Integer score){
String key = "rank:"+nowDate;
jedis.zincrby(key,score,userId);
}
选出前10名展示出来
伪代码:
public List getDailyRankings(String nowDate){
String key = "rank:"+nowDate;
return jedis.zrevreange(key,0,9);
}
利用zset的并集实现一周的日排行相加,从而选出周排行
伪代码:
public List getWeeklyRankings(String[] keys){
String weeklyRankings = "weeklyRankings";
jedis.zunionstore(weeklyRankings,2,keys[0],keys[1],keys[2],keys[3],keys[4],keys[5],keys[6],"weights",1,1,1,1,1,1,1);
return jedis.zrevrange(weeklyRankings,0,9);
}
文章点击量的排行,其实主要是利用了redis的单线程自加来实现的
HSET user:00001 “article:000001” “文章”
HSET user:00002 “article:000001” “文章”
ZADD article:score 0 user:00001:artical:0000001
ZADD article:score 0 user:00002:artical:0000001
ZINCRBY article:score 1 user:00001:artical:0000001
ZINCRBY article:score 1 user:00002:artical:0000001
ZREVRANGEWITHSCORES article:score 0 -1
存储模型设计
因为有上亿的用户量,所以可以将每个用户按照每个位来存入,每一天都有一个bitmap,每个bitmap的每一位对应着一个用户的当天的登录状态值
数据id设计
每天的bitmap的key设置成当天日期,比如login_20181206,而用户的id就和位数对应,比如王五的id为8,则bitset login_20181206 8 1
bitop and login_last_3_day login_20181206 login_20181205 login_20181204
(其实就是取三天的二进制位进行与运算)
bitop or login_last_7_day login_20181206 login_20181205 login_20181204 login_20181203 login_20181204 login_20181205 login_20181206
bitop not nerver_login_last_7_day login_last_7_day
使用bitmap存储所有用户的当天的登录情况,在java代码中循环30次取得每一天某一个用户的状态,并统计
使用bitmap存储某一个用户的这个月的登录情况,1号是第0位,2号是第1位,以此类推,只需要8个字节就能解决某一个用户一个月的登录情况,取出来的时候只需要使用bitcount来查询即可
第一次进来肯定还没有支持或反对. =====>结果为0,说明没有点赞.接着判断是否反对
getbit answer:166:user:oppose 6 =====>结果为0,说明没有反对.
第二次进来,已经点赞了
getbit answer:166:user:goods 6 =====>结果为1,说明对这个答案是点赞了,返回前台,显示支持效果。(已经知道点赞就没有去查询是否点反对了)
第三次进来,此时点击了反对
getbit answer:166:user:goods 6 =====>结果为0,说明没有点赞.接着判断是否反对
getbit answer:166:user:oppose 6 =====>结果为1,说明点了反对,返回前台,显示反对的效果.