博客
关于我
Redis实现微博好友功能微服务(关注,取关,共同关注)
阅读量:796 次
发布时间:2023-03-22

本文共 3348 字,大约阅读时间需要 11 分钟。

Redis与MySQL结合实现好友功能:基于Spring Boot的高效解决方案

背景与引言

在现代社交场景中,好友功能已经成为必备的一部分。用户之间的好友关系通常包括关注、取关、共同关注等多种操作。这些功能如果单纯依靠数据库来实现,虽然简单,但在复杂场景下却难以高效处理。比如,如何快速查询两个用户的共同关注列表,就变得颇为棘手。

数据库与Redis的结合

我们采用MySQL和Redis的方式结合使用,这样既能存储结构化的数据,又能高效处理集合操作。MySQL主要存储用户的基本信息和关注状态,而Redis通过其built-in的集合操作来管理用户间的关注关系。

Redis集合操作的优势

Redis的Sets数据类型提供了完善的集合操作,包括添加成员、移除成员、查询成员、统计成员数、判断成员以及查询交集等功能。这些操作在处理好友关系时效率非常高。

Redis命令示例

  • SADD key member [member...]:用于添加成员到集合中,支持批量添加。
  • SREM key member [member...]:用于移除指定成员。
  • SCARD key:返回集合中成员的数量。
  • SISMEMBER key member:判断指定用户是否是集合的成员。
  • SMEMBERS key:返回集合中的所有成员。
  • SINTER key [key...]:求多个集合的交集。

业务逻辑实现

关注与取关

  • 添加关注:当用户选择关注某用户时,我们首先检查数据库是否存在已有记录。如果不存在,再通过MySQL插入新的记录,并将用户ID添加到Redis的关注集合中。
  • 取关:当用户选择取关,我们更新数据库中的记录状态,并从Redis中移除用户ID。
  • 重新关注:如果用户取消关注后重新关注同一个用户,我们再次将用户ID添加到Redis集合中。
  • 共同关注查询

    通过Redis的SINTER操作,我们可以快速得到两个用户的共同关注列表。具体步骤如下:

  • 获取当前登录用户的关注集合和被关注用户的关注集合。
  • 使用SINTER操作计算两者的交集。
  • 将交集结果转换为用户ID列表,调用用户服务获取详细信息。
  • Spring Boot配置

    Redis配置

    package com.zjq.seckill.config;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.redis.connection.RedisConnectionFactory;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.data.redis.serializer.StringRedisSerializer;
    @Configuration
    public class RedisTemplateConfiguration {
    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
    RedisTemplate template = new RedisTemplate();
    template.setConnectionFactory(redisConnectionFactory);
    template.setValueSerializer(new StringRedisSerializer());
    template.setKeySerializer(new StringRedisSerializer());
    return template;
    }
    }

    依赖管理

    org.springframework.boot
    spring-boot-starter-data-redis
    com.zjq
    commons
    1.0-SNAPSHOT

    测试与验证

    通过上述实现,我们可以轻松完成以下操作:

    • 用户A关注用户B
    • 用户B取关
    • 查询用户A和用户C的共同关注列表

    controller与service实现

    @RestController
    public class FollowController {
    @Resource
    private FollowService followService;
    @Resource
    private HttpServletRequest request;
    @PostMapping("/{followUserId}")
    public ResultInfo follow(@PathVariable Integer followUserId, @RequestParam int isFollowed, String access_token) {
    return followService.follow(followUserId, isFollowed, access_token, request.getServletPath());
    }
    }
    @Service
    public class FollowService {
    @Resource
    private FollowMapper followMapper;
    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private RestTemplate restTemplate;
    @Value("${service.name.ms-oauth-server}")
    private String oauthServerName;
    @Value("${service.name.ms-diners-server}")
    private String dinersServerName;
    public ResultInfo follow(Integer followUserId, int isFollowed, String access_token, String path) {
    // 业务逻辑详见代码
    return resultInfo;
    }
    private void addToRedisSet(Integer dinerId, Integer followUserId) {
    redisTemplate.opsForSet().add(RedisKeyConstant.following.getKey() + dinerId, followUserId);
    redisTemplate.opsForSet().add(RedisKeyConstant.followers.getKey() + followUserId, dinerId);
    }
    }

    总结

    通过Redis和MySQL的结合,我们成功实现了高效的好友关系管理。这种设计既保证了数据的高效操作,又保持了系统的可扩展性。未来,我们可以进一步优化查询性能,并引入更多的功能如用户通知和好友排名。

    转载地址:http://iiqfk.baihongyu.com/

    你可能感兴趣的文章
    Objective-C实现inversions倒置算法(附完整源码)
    查看>>
    Objective-C实现isalpha函数功能(附完整源码)
    查看>>
    Objective-C实现islower函数功能(附完整源码)
    查看>>
    Objective-C实现isPowerOfTwo算法(附完整源码)
    查看>>
    Objective-C实现ItemCF算法(附完整源码)
    查看>>
    Objective-C实现ItemCF算法(附完整源码)
    查看>>
    Objective-C实现iterating through submasks遍历子掩码算法(附完整源码)
    查看>>
    Objective-C实现jaccard similarity相似度无平方因子数算法(附完整源码)
    查看>>
    Objective-C实现Julia集算法(附完整源码)
    查看>>
    Objective-C实现k nearest neighbours k最近邻分类算法(附完整源码)
    查看>>
    Objective-C实现k-Means算法(附完整源码)
    查看>>
    Objective-C实现k-nearest算法(附完整源码)
    查看>>
    Objective-C实现knapsack背包问题算法(附完整源码)
    查看>>
    Objective-C实现knight tour骑士之旅算法(附完整源码)
    查看>>
    Objective-C实现KNN算法(附完整源码)
    查看>>
    Objective-C实现koch snowflake科赫雪花算法(附完整源码)
    查看>>
    Objective-C实现KPCA(附完整源码)
    查看>>
    Objective-C实现kth order statistick阶统计量算法(附完整源码)
    查看>>
    Objective-C实现LRU 缓存算法(附完整源码)
    查看>>
    Objective-C实现lstm prediction预测算法(附完整源码)
    查看>>