Redis实现微博后台业务逻辑系列(四)

本文涉及的产品
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
简介:

保存微博用户之间的关系:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import  redis
 
class  RelatoinShip( object ):
     """使用无序集合键保存用户关系"""
     def  __init__( self , client):
         self .client  =  client
         
     def  follow( self , fans, target):
         """关注某人"""
         # 将用户添加到目标用户粉丝集合中
         target_fans_set  =  "weibo::user::"  +  str (target)  +  "::fans"
         self .client.sadd(target_fans_set, fans)
         # 将目标用户id添加到用户自身的关注集合中
         self_following_set  =  "weibo::user::"  +  str (fans)  +  "::following"
         self .client.sadd(self_following_set, target)
         
     def  is_following( self , fans, target):
         """检查fans用户是否关注了target用户"""
         self_following_set  =  "weibo::user::"  +  str (fans)  +  "::following"
         return  self .client.sismember(self_following_set, target)
         
     def  is_following_each_other( self , user_a, user_b):
         """检查两个用户是否已互相关注"""
         return  self .is_following(user_a, user_b)  and  self .is_following(user_b, user_a)
         
     def  get_all_following( self , user):
         """返回用户关注的所有人"""
         self_following_set  =  "weibo::user::"  +  str (user)  +  "::following"
         return  self .client.smembers(self_following_set)
         
     def  get_all_fans( self , user):
         """返回用户的所有粉丝"""
         target_fans_set  =  "weibo::user::"  +  str (user)  +  "::fans"
         if  not  self .client.exists(target_fans_set):
             return  False
         else :
             return  self .client.smembers(target_fans_set)
         
     def  common_following( self , user_a, user_b):
         """返回两个用户共同关注的人"""
         user_a_following  =  "weibo::user::"  +  str (user_a)  +  "::following"
         user_b_following  =  "weibo::user::"  +  str (user_b)  +  "::following"
         return  self .client.sinter(user_a_following, user_b_following)
 
 
if  __name__  = =  "__main__" :
     redis_client  =  redis.StrictRedis()
     relation  =  RelatoinShip(redis_client)
     relation.follow( 10086 12345 )
     relation.follow( 10010 12345 )
     print (relation.is_following( 10086 12345 ))
     print (relation.get_all_following( 10086 ))
     print (relation.get_all_fans( 12345 ))
     print (relation.common_following( 10086 10010 ))

    关注集合,用于存储用户关注的人的ID,“weibo::user::<id>::following”

    粉丝集合,用于存储用户的粉丝的ID,“weibo::user::<id>::fans”

 

   我们在微博中看到喜欢的人可以点击关注,当然我们自己也可以被人关注,我们可以看到自己所有的粉丝,也可以看到自己关注的所有人,有显示是否已互相关注,微博也会显示自己和好友共同关注的人。

    在这个类中我们使用无序集合来保存用户的关系,集合的特性是去重,绝对没有重复值。无序集合这种数据结构特别适合保存不能有重复值,并且不在乎存储顺序的数据。就像我们在朋友圈点赞时,先点赞的人不一定排在前面,后点赞的人不一定排在后面显示,他们的显示顺序是无序的,但又是唯一的。后面我们在实现点赞(投票)功能时,也使用的无序集合这种数据结构来存储。

    现在可以思考一下如果要取消关注该怎么办呢!?

本文转自戴柏阳的博客博客51CTO博客,原文链接http://blog.51cto.com/daibaiyang119/1962665如需转载请自行联系原作者


daibaiyang119

相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
相关文章
|
2月前
|
NoSQL Java Redis
springboot搭建后台框架 (二)整合Redis
springboot搭建后台框架 (二)整合Redis
41 0
|
5月前
|
NoSQL Linux Redis
linux下后台启动redis
linux下后台启动redis
|
5月前
|
缓存 NoSQL 中间件
太卷了!京东、微博最新「Redis缓存高手心法手册」竟被开源了
众所周知,分布式架构被广泛应用于企业级应用开发中,以满足高并发、高可用、高性能、高扩展性等要求。
|
8月前
|
存储 监控 Java
基于SpringBoot+Redis+Vue的后台管理系统开源项目,附源码地址
eladmin 是一款基于 Spring Boot 2.1.0 、 Jpa、 Spring Security、redis、Vue 的前后端分离的后台管理系统,项目采用分模块开发方式, 权限控制采用 RBAC,支持数据字典与数据权限管理,支持一键生成前后端代码,支持动态路由。
|
9月前
|
NoSQL Redis 数据库
Redis 常见面试问题 Redis持久化aof后台重写和写时复制
今天有个群里网友问,redis面试被问到aof 后台重写 写时复制,我尝试回答一下
326 3
|
10月前
|
缓存 NoSQL 前端开发
【Go】基于 Gin 从0到1搭建 Web 管理后台系统后端服务(三)路由、自定义校验器和 Redis
【Go】基于 Gin 从0到1搭建 Web 管理后台系统后端服务(三)路由、自定义校验器和 Redis
|
12月前
|
NoSQL JavaScript 小程序
Redis实现微博好友功能微服务(关注,取关,共同关注)
Redis实现微博好友功能微服务(关注,取关,共同关注)
|
NoSQL Redis
Redis学习4:List数据类型、拓展操作、实现日志等
注意点:对存储空间的顺序进行分析!
Redis学习4:List数据类型、拓展操作、实现日志等
|
存储 NoSQL Redis
Redis学习3:hash类型操作、拓展操作、实现购物等
首先可以理解成一个redis里面有一个小的redis。同时要注意引入了一个field的名字。
Redis学习3:hash类型操作、拓展操作、实现购物等

热门文章

最新文章