使用Redis实现关注好友的功能
现在很多社交都有关注或者添加粉丝的功能, 类似于这样的功能我们如果采用数据库做的话只是单纯得到用户的一些粉丝或者关注列表的话是很简单也很容易实现, 但是如果我想要查出两个甚至多个用户共同关注了哪些人或者想要查询两个或者多个用户的共同粉丝的话就会很麻烦, 效率也不会很高. 但是如果你用redis去做的话就会相当的简单而且效率很高. 原因是redis自己本身带有专门针对于这种集合的交集,并集, 差集的一些操作。
设计思路如下:
总体思路我们采用redis里面的zset完成整个功能, 原因是zset有排序(我们要按照关注时间的倒序排列), 去重(我们不能多次关注同一用户)功能. 一个用户我们存贮两个集合, 一个是保存用户关注的人 另一个是保存关注用户的人.
用到的命令是:
1、 zadd 添加成员:命令格式:zadd key score member [[score][member] …]
2、zrem 移除某个成员:命令格式:zrem key member [member …]
3、 zcard 统计集合内的成员数:命令格式:zcard key
4、 zrange 查询集合内的成员:命令格式:ZRANGE key start stop [WITHSCORES]
描述:返回指定区间的成员。其中成员位置按 score 值递增(从小到大)来排序。 WITHSCORES选项是用来让成员和它的score值一并返回.
5、 zrevrange跟zrange作用相反
6、zrank获取成员的排名:命令格式:zrank key member
描述:返回有序集key中成员member的排名。成员按 score 值递增(从小到大)顺序排列。排名以0开始,也就是说score 值最小的为0。返回值:返回成员排名,member不存在返回nil.
7、 zinterstore 取两个集合的交集:命令格式:ZINTERSTORE destination numkeys key [key …][WEIGHTS weight [weight …]] [AGGREGATE SUM|MIN|MAX]
描述:计算给定的一个或多个有序集的交集。其中给定 key 的数量必须以 numkeys 参数指定,并将该交集(结果集)储存到 destination 。默认情况下,结果集中某个成员的 score 值是所有给定集下该成员 score 值之 和 。
返回值:保存到 destination 的结果集成员数。
下面我用Java写了一个简单的例子 maven构建
第一步: 添加Redis客户端
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
第二步: 封装一个简单的redis工具类
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
public final class RedisUtil {
//Redis服务器IP
private static String ADDR = "localhost";
//Redis的端口号
private static int PORT = 6379;
//访问密码
private static String AUTH = "admin";
//控制一个pool最多有多少个状态为idle(空闲的)的jedis实例,默认值也是8。
private static int MAX_IDLE = 200;
private static int TIMEOUT = 10000;
//在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;
private static boolean TEST_ON_BORROW = true;
private static JedisPool jedisPool = null; static {
try {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxIdle(MAX_IDLE);
config.setTestOnBorrow(TEST_ON_BORROW);
jedisPool = new JedisPool(config, ADDR, PORT, TIMEOUT, AUTH);
} catch (Exception e) {
e.printStackTrace();
}
} public synchronized static Jedis getJedis() {
try {
if (jedisPool != null) {
Jedis resource = jedisPool.getResource();
return resource;
} else {
return null;
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
} @SuppressWarnings("deprecation")
public static void returnResource(final Jedis jedis) {
if (jedis != null) {
jedisPool.returnResource(jedis);
}
}
}
第三步: 封装简单的Follow类
import java.util.HashSet;
import java.util.Set; import redis.clients.jedis.Jedis; import com.indulgesmart.base.util.RedisUtil;
public class FollowUtil { private static final String FOLLOWING = "FOLLOWING_";
private static final String FANS = "FANS_";
private static final String COMMON_KEY = "COMMON_FOLLOWING"; // 关注或者取消关注
public static int addOrRelease(String userId, String followingId) {
if (userId == null || followingId == null) {
return -1;
}
int isFollow = 0; // 0 = 取消关注 1 = 关注
Jedis jedis = RedisUtil.getJedis();
String followingKey = FOLLOWING + userId;
String fansKey = FANS + followingId;
if (jedis.zrank(followingKey, followingId) == null) { // 说明userId没有关注过followingId
jedis.zadd(followingKey, System.currentTimeMillis(), followingId);
jedis.zadd(fansKey, System.currentTimeMillis(), userId);
isFollow = 1;
} else { // 取消关注
jedis.zrem(followingKey, followingId);
jedis.zrem(fansKey, fansKey);
}
return isFollow;
} // 验证两个用户之间的关系
// 0=没关系 1=自己 2=userId关注了otherUserId 3= otherUserId是userId的粉丝 4=互相关注
public int checkRelations (String userId, String otherUserId) { if (userId == null || otherUserId == null) {
return 0;
} if (userId.equals(otherUserId)) {
return 1;
}
Jedis jedis = RedisUtil.getJedis();
String followingKey = FOLLOWING + userId;
int relation = 0;
if (jedis.zrank(followingKey, otherUserId) != null) { // userId是否关注otherUserId
relation = 2;
}
String fansKey = FANS + userId;
if (jedis.zrank(fansKey, userId) != null) {// userId粉丝列表中是否有otherUserId
relation = 3;
}
if ((jedis.zrank(followingKey, otherUserId) != null)
&& jedis.zrank(fansKey, userId) != null) {
relation = 4;
}
return relation;
} // 获取用户所有关注的人的id
public static Set<String> findFollwings(String userId) {
return findSet(FOLLOWING + userId);
} // 获取用户所有的粉丝
public static Set<String> findFans(String userId) {
return findSet(FANS + userId);
} // 获取两个共同关注的人
public static Set<String> findCommonFollowing(String userId, String otherUserId) {
if (userId == null || otherUserId == null) {
return new HashSet<>();
}
Jedis jedis = RedisUtil.getJedis();
String commonKey = COMMON_KEY + userId + "_" + otherUserId;
// 取交集
jedis.zinterstore(commonKey + userId + "_" + otherUserId, FOLLOWING + userId, FOLLOWING + otherUserId);
Set<String> result = jedis.zrange(commonKey, 0, -1);
jedis.del(commonKey);
return result;
} // 根据key获取set
private static Set<String> findSet(String key) {
if (key == null) {
return new HashSet<>();
}
Jedis jedis = RedisUtil.getJedis();
Set<String> result = jedis.zrevrange(key, 0, -1); // 按照score从大到小排序
return result;
}
}
使用Redis实现关注好友的功能的更多相关文章
- php + redis 实现关注功能
产品价值 1: 关注功能 2: 功能分析之"关注"功能 3: 平平无奇的「关注」功能,背后有4点重大价值 应用场景 在做PC或者APP端时,掺杂点社交概念就有关注和粉丝功能; 数据 ...
- 简单实现Redis缓存中的排序功能
1.在实现缓存排序功能之前,必须先明白这一功能的合理性.不妨思考一下,既然可以在数据库中排序,为什么还要把排序功能放在缓存中实现呢?这里简单总结了两个原因:首先,排序会增加数据库的负载,难以支撑高并发 ...
- 【springboot】【redis】springboot+redis实现发布订阅功能,实现redis的消息队列的功能
springboot+redis实现发布订阅功能,实现redis的消息队列的功能 参考:https://www.cnblogs.com/cx987514451/p/9529611.html 思考一个问 ...
- redis(四)--简单实现Redis缓存中的排序功能
在实现缓存排序功能之前,必须先明白这一功能的合理性.不妨思考一下,既然可以在数据库中排序,为什么还要把排序功能放在缓存中实现呢?这里简单总结了两个原因:首先,排序会增加数据库的负载,难以支撑高并发的应 ...
- redis 实现发布订阅的功能
redis 除了作为缓存的功能外还可以用作消息中间件的功能,这片博客主要是介绍一下 redis 整合spring 实现消息的发布和订阅功能: 1:redis依赖,依赖两个包,redis 包, spri ...
- Redis位图实现用户签到功能
场景需求 适用场景如签到送积分.签到领取奖励等,大致需求如下: 签到1天送1积分,连续签到2天送2积分,3天送3积分,3天以上均送3积分等. 如果连续签到中断,则重置计数,每月初重置计数. 当月签到满 ...
- 基于Redis位图实现用户签到功能
场景需求 适用场景如签到送积分.签到领取奖励等,大致需求如下: 签到1天送1积分,连续签到2天送2积分,3天送3积分,3天以上均送3积分等. 如果连续签到中断,则重置计数,每月初重置计数. 当月签到满 ...
- python开发-实现redis中的发布订阅功能
Python3学习(二十七):python实现Redis的订阅与发布(sub-pub机制) 先介绍一下redis的pub/sub功能: Pub/Sub功能(means Publish, Subscri ...
- swift 实现QQ好友列表功能
最近项目中有类似QQ好友列表功能,整理了一下,话不多说,直接上代码 import UIKit class QQFriend: NSObject { var name: String? var intr ...
随机推荐
- BI数据可视化工具怎么选?用这款就够了!
任何一项产品的选择都需要谨慎而全面,BI数据可视化工具的选择就更不用说了.作为企业的IT部门,如果没有良好的BI工具支持,IT部门将会十分容易陷入困境.那么面对多元化的BI工具市场,IT部门该如何选择 ...
- 案例三:shell统计ip访问情况并分析访问日志
题目要求 有日志 1.log,部分内容如下: 112.111.12.248 – [25/Sep/2013:16:08:31 +0800]formula-x.haotui.com"/secco ...
- 一个简单的性能计数器:CodeTimer
public static class CodeTimer { public static void Initialize() { Process.GetCurrentProcess().Priori ...
- java创建线程的四种方法
第一种: 通过继承Thread类创建线程 第二种: 通过实现Runnable接口创建线程 这两种早已烂记于心,这里就不作过多的介绍, 主要介绍其源码 Thread类 implements Runna ...
- WIN10:隐藏记事本等任务栏项目的最近访问
右键任务栏 点开任务栏设置 关闭最近打开项的显示按钮
- c/c++ 内存泄漏分析
Valgrind: https://zhuanlan.zhihu.com/p/111556601 valgrind输出结果分析 valgrind输出结果会报告5种内存泄露,"definite ...
- python+pytest接口自动化(4)-requests发送get请求
python中用于请求http接口的有自带的urllib和第三方库requests,但 urllib 写法稍微有点繁琐,所以在进行接口自动化测试过程中,一般使用更为简洁且功能强大的 requests ...
- MySQL — DML语言
DML 全称 Data Manipulation Language.数据操作语言,用来对数据库表中的数据进行增删改. 1.添加数据 插入一条数据 给指定字段插入数据:insert into 表名 (字 ...
- 矩池云上使用nvidia-smi命令教程
简介 nvidia-smi全称是NVIDIA System Management Interface ,它是一个基于NVIDIA Management Library(NVML)构建的命令行实用工具, ...
- 矩池云上安装ikatago及远程链接教程
https://github.com/kinfkong/ikatago-resources/tree/master/dockerfiles 从作者的库中可以看到,该程序支持cuda9.2.cuda10 ...