微信开发系列之七 - 使用Redis存储微信聊天记录

本文涉及的产品
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
简介: 微信开发系列之七 - 使用Redis存储微信聊天记录

In the second blog Wechat development series 2 – development Q&A service using nodejs of this series, we have developed a kind of Q&A service in Wechat which leverages a free Tuning Restful service so that you could chat with this service:image.pngimage.pngn this blog, I will show the steps how to store all of those conversation logs via Redis, so that you can review the conversations within Wechat app or delete them if necessary.


Implemented feature

Two new sub menus “Review” and “Delete” are developed under menu “Conversation”:image.pngimage.pngOnce Review menu is pressed, the conversation log will be read out from Redis and displayed in Wechat app:

image.pngimage.pngDelete menu will trigger the deletion of the conversation log belonging to current user who has pressed it. Once conversation is deleted, corresponding notification will be sent out if user has pressed the Review menu again.image.pngimage.pngImplementation detail

(1) Since as before my WeChat server runs on Heroku, which supports Redis as well.

Redis is an open source (BSD licensed), in-memory data structure store, used as a database, cache and message broker. It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs and geospatial indexes with radius queries.

In this blog I will use Redis to store conversation logs.


It is very easy in Heroku to enable an application with Redis support, just select your application from drop down list and press Continue:image.pnghe default plan “Hobby Dev” is completely for free and enough for this prototype development.

image.pngOnce done, in you application dashboard you can see the Redis as addon:image.pngand the actual url for this Redis instance is automatically set in application variable which could be used in your programimage.pngou don’t need any additional configuration on Redis, which is now ready for development.

(2) follow the steps in blog Wechat development series 5 – embedded your UI5 application to Wechat app to create menu and two submenu.


It is enough to create menu via postman. You’d better first remove the old menu done in previous blog and recreate a new menu from scratch:image.pnghe source code of HTTP post payload for the new menu creation:image.png3) Now every time when Tuning service receives a query from end user and is ready to send the answer of this query, add logic to record this conversation detail.

Just add one line for logging in tuning.js:image.pngThe conversationLogService is implemented in conversationLogService.js, which simply delegates the logging call to Redis service wrapper module.image.pngRedis service wrapper module is implemented in file redisClient.js.


This wrapper module is built on top of open source Redis module for nodejs, whose source code could be found from github: https://github.com/NodeRedis/node_redis


image.png

image.pngimage.png4) implement event handling logic when menu “review” and “delete” are pressed.


Corresponding API provided by Redis are called to read log records from Redis or just clear the list where the log records are stored.image.png


相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore     ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库 ECS 实例和一台目标数据库 RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
相关文章
|
存储 NoSQL JavaScript
微信开发系列之七 - 使用Redis存储微信聊天记录
微信开发系列之七 - 使用Redis存储微信聊天记录
278 0
微信开发系列之七 - 使用Redis存储微信聊天记录
|
存储 NoSQL JavaScript
微信开发系列之七 - 使用Redis存储微信聊天记录
微信开发系列之七 - 使用Redis存储微信聊天记录
123 0
微信开发系列之七 - 使用Redis存储微信聊天记录
|
1月前
|
存储 NoSQL 算法
09- Redis分片集群中数据是怎么存储和读取的 ?
Redis分片集群使用哈希槽分区算法,包含16384个槽(0-16383)。数据存储时,通过CRC16算法对key计算并模16383,确定槽位,进而分配至对应节点。读取时,根据槽位找到相应节点直接操作。
65 12
|
1月前
|
NoSQL Linux Redis
06- 你们使用Redis是单点还是集群 ? 哪种集群 ?
**Redis配置:** 使用哨兵集群,结构为1主2从,加上3个哨兵节点,总计分布在3台Linux服务器上,提供高可用性。
359 0
|
1月前
|
负载均衡 监控 NoSQL
Redis的集群方案有哪些?
Redis集群包括主从复制(基础,手动故障恢复)、哨兵模式(自动高可用)和Redis Cluster(官方分布式解决方案,自动分片和容错)。此外,还有如Codis、Redisson和Twemproxy等第三方工具用于代理和负载均衡。选择方案需考虑应用场景、数据规模和并发需求。
289 2
|
1天前
|
NoSQL 算法 Java
深入浅出Redis(八):Redis的集群模式
深入浅出Redis(八):Redis的集群模式
|
7天前
|
NoSQL Redis
透视Redis集群:心跳检测如何维护高可用性
Redis心跳检测保障集群可靠性,通过PING命令检测主从连接状态,预防数据丢失。当连接异常时,自动触发主从切换。此外,心跳检测辅助实现`min-slaves-to-write`和`min-slaves-max-lag`策略,避免不安全写操作。还有重传机制,确保命令无丢失,维持数据一致性。合理配置心跳检测,能有效防止数据问题,提升Redis集群的高可用性。关注“软件求生”获取更多Redis知识!
107 10
透视Redis集群:心跳检测如何维护高可用性
|
9天前
|
监控 NoSQL 算法
Redis集群模式:高可用性与性能的完美结合!
小米探讨Redis集群模式,通过一致性哈希分散负载,主从节点确保高可用性。节点间健康检测、主备切换、数据复制与同步、分区策略和Majority选举机制保证服务可靠性。适合高可用性及性能需求场景,哨兵模式则适用于简单需求。一起学习技术的乐趣!关注小米微信公众号“软件求生”获取更多内容。
41 11
Redis集群模式:高可用性与性能的完美结合!
|
10天前
|
监控 NoSQL Redis
|
15天前
|
NoSQL Redis Docker
使用Docker搭建Redis主从集群
使用Docker搭建Redis主从集群
32 1