基于redis的最新文章推荐

本文介绍了一种使用Redis缓存来优化文章推荐系统的方法,通过将文章内容和用户推荐列表存储于Redis的hash和string结构中,实现了快速查询和更新,显著提高了系统的响应速度。

1.目的

背景:文章量6000条左右,用户量170w多

在考虑用户频繁查询数据库这一块吗,我们在数据库与java程序之间介入了redis缓存

2.知识储备

1.redis:hash结构

2.redis:string结构

3.实现

1.我们把最新文章前1000条数分别存入hash,与string结构中

//从db查询出来的文章列表
List articleList = new ArrayList();

//获取文章id列表
List<Integer> ids = articleList .stream().map(z->{return  z.getInt("id");}).collect(Collectors.toList());

//文章放入缓存中
jedis.hmset("articleList", articleList);
//文章id放入缓存中
jedis.set("articleIdList", ids);

2.查询最新文章实现

//获取用户推荐过文章列表
List<Integer> hasRecommendList = jedis.get("hasRecommedArticle"+userId);

//获取推荐文章id列表
List<Integer> recommendList = jedis.get("articleIdList");

//做推荐文章列表 与 用户推荐过文章列表差集
recommendList.removaAll(hasRecommendList);

//查询文章id,不用subList的原因:subList没有序列化
List selectIdList = new ArrayList();
for (int i = 0; i < 5; i++) {
  selectIdList.add(recommendList.get(i));
}    

//查询文章
List retrunList = jedis.hmget("articleList",selectIdList.toArray());

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DencyCheng

你的鼓励是我最大的支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值