实战:第二十一章:实现微博微信关注模型

本文涉及的产品
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
简介: 实战:第二十一章:实现微博微信关注模型

业务实现层:

    @Override
    public ResultData<RelationshipUsersAndFansDto> getRelationshipUsersAndFans(String userId, String followId) {
        List<B8UserUserinfoEntity> myself = userinfoMapper.getRelationshipUsersAndFans(userId);
        List<String> myselfIds = myself.stream().map(B8UserUserinfoEntity::getFollowId).collect(Collectors.toList());
        for (String myselfId : myselfIds) {
            //我关注的人的id集合
            redisService.redisTemplate.opsForSet().add("myselfFollowUsers",myselfId);
        }
        //进入大V主页他关注的人的id集合
        List<B8UserUserinfoEntity> bigV = userinfoMapper.getRelationshipUsersAndFans(followId);
        List<String> bigvIds = bigV.stream().map(B8UserUserinfoEntity::getFollowId).collect(Collectors.toList());
        for (String bigvId : bigvIds) {
            //把我们二个关注的人丢到集合中
            redisService.redisTemplate.opsForSet().add("bigvFollowUsers",bigvId);
        }
        //获取我们共同关注的人id集合
        Set intersectSet = redisService.redisTemplate.opsForSet().intersect("myselfFollowUsers", "bigvFollowUsers");
        List<String> intersectList = new ArrayList<>(intersectSet);
        //共同关注的数量
        int intersectSize = intersectList.size();
        //我关注的人也关注这个大V,但是大V没有关注他,取差集
        Set differenceSet = redisService.redisTemplate.opsForSet().difference("myselfFollowUsers", "bigvFollowUsers");
        List<String> differenceList = new ArrayList<>(differenceSet);
        //差集的数量
        int differenceSize = differenceList.size();
        //封装
        RelationshipUsersAndFansDto relationshipUsersAndFansDto = new RelationshipUsersAndFansDto();
        if(!CollectionUtils.isEmpty(intersectList)){
            //获取共同关注的用户信息
            List<B8UserUserinfoEntity> myselfList = userinfoMapper.getUserByIds(intersectList);
            relationshipUsersAndFansDto.setMyself(myselfList);
        }
        if(!CollectionUtils.isEmpty(differenceList)){
            //获取我关注的人也关注他的用户信息
            List<B8UserUserinfoEntity> bigvList = userinfoMapper.getUserByIds(differenceList);
            relationshipUsersAndFansDto.setBigV(bigvList);
        }
        relationshipUsersAndFansDto.setIntersectSize(intersectSize);
        relationshipUsersAndFansDto.setDifferenceSize(differenceSize);
        return ResultData.success(relationshipUsersAndFansDto);
    }


redisService:

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
 * spring redis 工具类
 **/
@SuppressWarnings(value = {"unchecked", "rawtypes"})
@Component
@Slf4j
public class RedisService {
    @Autowired
    public RedisTemplate redisTemplate;
    /**
     * 缓存基本的对象,Integer、String、实体类等
     *
     * @param key   缓存的键值
     * @param value 缓存的值
     */
    public <T> void setCacheObject(final String key, final T value) {
        redisTemplate.opsForValue().set(key, value);
    }
    /**
     * 缓存基本的对象,Integer、String、实体类等
     *
     * @param key      缓存的键值
     * @param value    缓存的值
     * @param timeout  时间
     * @param timeUnit 时间颗粒度
     */
    public <T> void setCacheObject(final String key, final T value, final Integer timeout, final TimeUnit timeUnit) {
        redisTemplate.opsForValue().set(key, value, timeout, timeUnit);
    }
    /**
     * 设置有效时间
     *
     * @param key     Redis键
     * @param timeout 超时时间
     * @return true=设置成功;false=设置失败
     */
    public boolean expire(final String key, final long timeout) {
        return expire(key, timeout, TimeUnit.SECONDS);
    }
    /**
     * 设置有效时间
     *
     * @param key     Redis键
     * @param timeout 超时时间
     * @param unit    时间单位
     * @return true=设置成功;false=设置失败
     */
    public boolean expire(final String key, final long timeout, final TimeUnit unit) {
        return redisTemplate.expire(key, timeout, unit);
    }
    /**
     * 获得缓存的基本对象。
     *
     * @param key 缓存键值
     * @return 缓存键值对应的数据
     */
    public <T> T getCacheObject(final String key) {
        ValueOperations<String, T> operation = redisTemplate.opsForValue();
        return operation.get(key);
    }
    /**
     * 删除单个对象
     *
     * @param key
     */
    public boolean deleteObject(final String key) {
        return redisTemplate.delete(key);
    }
    /**
     * 删除集合对象
     *
     * @param collection 多个对象
     * @return
     */
    public long deleteObject(final Collection collection) {
        return redisTemplate.delete(collection);
    }
    /**
     * 缓存List数据
     *
     * @param key      缓存的键值
     * @param dataList 待缓存的List数据
     * @return 缓存的对象
     */
    public <T> long setCacheList(final String key, final List<T> dataList) {
        Long count = redisTemplate.opsForList().rightPushAll(key, dataList);
        return count == null ? 0 : count;
    }
    /**
     * 获得缓存的list对象
     *
     * @param key 缓存的键值
     * @return 缓存键值对应的数据
     */
    public <T> List<T> getCacheList(final String key) {
        return redisTemplate.opsForList().range(key, 0, -1);
    }
    /**
     * 缓存Set
     *
     * @param key     缓存键值
     * @param dataSet 缓存的数据
     * @return 缓存数据的对象
     */
    public <T> long setCacheSet(final String key, final Set<T> dataSet) {
        Long count = redisTemplate.opsForSet().add(key, dataSet);
        return count == null ? 0 : count;
    }
    /**
     * 获得缓存的set
     *
     * @param key
     * @return
     */
    public <T> Set<T> getCacheSet(final String key) {
        return redisTemplate.opsForSet().members(key);
    }
    /**
     * 缓存Map
     *
     * @param key
     * @param dataMap
     */
    public <T> void setCacheMap(final String key, final Map<String, T> dataMap) {
        if (dataMap != null) {
            redisTemplate.opsForHash().putAll(key, dataMap);
        }
    }
    /**
     * 获得缓存的Map
     *
     * @param key
     * @return
     */
    public <T> Map<String, T> getCacheMap(final String key) {
        return redisTemplate.opsForHash().entries(key);
    }
    /**
     * 往Hash中存入数据
     *
     * @param key   Redis键
     * @param hKey  Hash键
     * @param value 值
     */
    public <T> void setCacheMapValue(final String key, final String hKey, final T value) {
        redisTemplate.opsForHash().put(key, hKey, value);
    }
    /**
     * 获取Hash中的数据
     *
     * @param key  Redis键
     * @param hKey Hash键
     * @return Hash中的对象
     */
    public <T> T getCacheMapValue(final String key, final String hKey) {
        HashOperations<String, String, T> opsForHash = redisTemplate.opsForHash();
        return opsForHash.get(key, hKey);
    }
    /**
     * 获取多个Hash中的数据
     *
     * @param key   Redis键
     * @param hKeys Hash键集合
     * @return Hash对象集合
     */
    public <T> List<T> getMultiCacheMapValue(final String key, final Collection<Object> hKeys) {
        return redisTemplate.opsForHash().multiGet(key, hKeys);
    }
    /**
     * 获得缓存的基本对象列表
     *
     * @param pattern 字符串前缀
     * @return 对象列表
     */
    public Collection<String> keys(final String pattern) {
        return redisTemplate.keys(pattern);
    }
    /**
     * 向一张hash表中放入数据,如果不存在将创建
     *
     * @param key   键
     * @param item  项
     * @param value 值
     * @return true 成功 false失败
     */
    public Boolean hset(String key, String item, Object value) {
        try {
            redisTemplate.opsForHash().put(key, item, value);
            return true;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return false;
        }
    }
    /**
     * HashGet
     *
     * @param key  键 不能为 null
     * @param item 项 不能为 null
     * @return 值
     */
    public Object hget(String key, String item) {
        return redisTemplate.opsForHash().get(key, item);
    }
    /**
     * 删除hash表中的值
     *
     * @param key  键 不能为 null
     * @param item 项 可以使多个不能为 null
     */
    public void hdel(String key, Object... item) {
        redisTemplate.opsForHash().delete(key, item);
    }
    /**
     * 判断 key是否存在
     *
     * @param key 键
     * @return true 存在 false不存在
     */
    public Boolean hasKey(String key) {
        try {
            return redisTemplate.hasKey(key);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return false;
        }
    }
}
相关实践学习
基于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月前
|
JSON 小程序 JavaScript
【微信小程序】-- 宿主环境 & 通信模型 & 运行机制介绍(五)
【微信小程序】-- 宿主环境 & 通信模型 & 运行机制介绍(五)
|
5月前
|
供应链 数据可视化 开发者
微信小游戏矩阵化运营模型(试读)
微信小游戏矩阵化运营模型(试读)
66 1
|
9月前
|
小程序 JavaScript Android开发
微信小程序-双线程渲染模型
小程序的运行环境分成渲染层和逻辑层: • WXML 模板和 WXSS 样式工作在渲染层,通过 WebView 进行渲染 • 小程序会为每一个界面都创建一个 WebView 来渲染这个页面 • JS 脚本工作在逻辑层,通过 JsCore 线程运行 JS 脚本 • 这两个线程的通信会经由微信客户端做中转
152 2
|
JSON 小程序 JavaScript
微信小程序开发实战(宿主环境与通信模型)
微信小程序开发实战(宿主环境与通信模型)
微信小程序开发实战(宿主环境与通信模型)
|
JSON 小程序 JavaScript
【微信小程序】宿主环境 通信模型 运行机制 组件
🍓小程序的宿主环境 - 宿主环境简介 1. 什么是宿主环境 2. 小程序的宿主环境 3. 小程序宿主环境包含的内容 🍍小程序的宿主环境 - 通信模型 1. 通信的主体 2. 小程序的通信模型 🥦小程序的宿主环境 - 运行机制 🍒小程序的宿主环境 - 组件 1. 小程序中组件的分类 2. 常用的视图容器类组件 3. view 组件的基本使用
|
Kubernetes 应用服务中间件 Docker
DockOne微信分享(一三零):探究PaaS网络模型设计
本文讲的是DockOne微信分享(一三零):探究PaaS网络模型设计【编者的话】PaaS的抽象逻辑使得其网络模型与IaaS有所不同,本次通过重点说明Kubernetes和Docker的网络,从而来探究PaaS的网络模型设计。
2424 0
|
21天前
|
小程序 前端开发 API
微信小程序全栈开发中的异常处理与日志记录
【4月更文挑战第12天】本文探讨了微信小程序全栈开发中的异常处理和日志记录,强调其对确保应用稳定性和用户体验的重要性。异常处理涵盖前端(网络、页面跳转、用户输入、逻辑异常)和后端(数据库、API、业务逻辑)方面;日志记录则关注关键操作和异常情况的追踪。实践中,前端可利用try-catch处理异常,后端借助日志框架记录异常,同时采用集中式日志管理工具提升分析效率。开发者应注意安全性、性能和团队协作,以优化异常处理与日志记录流程。
|
21天前
|
小程序 安全 数据安全/隐私保护
微信小程序全栈开发中的身份认证与授权机制
【4月更文挑战第12天】本文探讨了微信小程序全栈开发中的身份认证与授权机制。身份认证包括手机号验证、微信登录和第三方登录,而授权机制涉及角色权限控制、ACL和OAuth 2.0。实践中,开发者可利用微信登录获取用户信息,集成第三方登录,以及实施角色和ACL进行权限控制。注意点包括安全性、用户体验和合规性,以保障小程序的安全运行和良好体验。通过这些方法,开发者能有效掌握小程序全栈开发技术。
|
21天前
|
小程序 前端开发 JavaScript
微信小程序全栈开发中的PWA技术应用
【4月更文挑战第12天】本文探讨了微信小程序全栈开发中PWA技术的应用,PWA结合Web的开放性和原生应用的性能,提供离线访问、后台运行、桌面图标和原生体验。开发者可利用Service Worker实现离线访问,Worker处理后台运行,Web App Manifest添加桌面图标,CSS和JavaScript提升原生体验。实践中需注意兼容性、性能优化和用户体验。PWA技术能提升小程序的性能和用户体验,助力开发者打造优质小程序。
|
4天前
|
小程序 前端开发 JavaScript
轻松学会微信小程序开发(一)
轻松学会微信小程序开发(一)

热门文章

最新文章