Unread message counting system and method based on read message recording and caching technology optimization
By only recording read messages and using Redis cache to update unread message counts in real time, the long response time and data redundancy caused by full table scanning in traditional systems is solved, and efficient unread message counting and low storage space occupation are achieved.
Patent Information
- Application Number
- CN202411993510.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-31
- Publication Date
- 2025-05-16
AI Technical Summary
Traditional unread message counting systems lead to long response times through full table scanning, and disk space is wasted because all messages (both read and unread) are recorded.
Only read messages are recorded and the Redis cache is used to update each user's unread message count in real time to avoid full table scanning.
It significantly reduces data storage requirements, improves query efficiency, reduces storage space usage, and reduces the maintenance cost of message lists.
Smart Images

Figure CN120017626A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of information processing, and in particular to an unread message counting system and method based on read message recording and cache technology optimization. Background Art
[0002] In instant messaging software and other messaging services, accurately and quickly counting the number of users' unread messages is one of the key factors in user experience.
[0003] The traditional implementation usually creates a database record for each message and determines the number of unread messages for a specific user by scanning the entire table. This method is not only inefficient, but also takes up a lot of storage space. The specific problems are as follows:
[0004] Full table scan: When querying unread messages for a specific user, the entire database needs to be traversed, resulting in a long response time.
[0005] Data redundancy: All messages (both read and unread) are recorded, resulting in a waste of disk space.
[0006] Therefore, we need to propose an unread message counting system and method based on read message record and cache technology optimization, which can significantly improve data processing efficiency and reduce the required storage space. Summary of the invention
[0007] The purpose of the present invention is to provide an unread message counting system and method based on read message recording and caching technology optimization, which only records the read messages and greatly reduces the data storage requirements compared with the traditional method. By using Redis cache to directly obtain the unread message count, the full table scan is avoided, and the query efficiency is greatly improved to solve the problems raised in the above background technology.
[0008] To achieve the above object, the present invention provides the following technical solution: an unread message counting system based on read message recording and caching technology optimization, comprising:
[0009] Message details table: only saves the relevant information of the read messages;
[0010] Redis cache: used to update the unread message count of each user in real time; each user has an independent key-value pair, where the key is the user ID and the value is the current number of unread messages;
[0011] The fields in the message details table include:
[0012] ID: unique identifier of the message;
[0013] User-ID: The user ID that receives the message;
[0014] message_content: message content;
[0015] read_time: the time when the message was marked as read;
[0016] The key in the Redis cache is used to uniquely identify a user's unread message count, and the value in the Redis cache is updated in real time as new messages arrive and are read.
[0017] Preferably, the type of ID in the field is an integer or a string, which is used to distinguish different message records; the type of User-ID in the field is an integer or a string, which is used to identify the recipient of the message; the type of message_content in the field is text, link or picture; the type of read_time in the field is a timestamp, which is used to analyze the user's message reading habits or optimize the message push strategy.
[0018] Preferably, each user's key is unique, ensuring that each user's unread message count is stored independently; when a new message arrives, the user's unread message count increases, and when a user reads a message, the user's unread message count decreases.
[0019] The present invention also provides an unread message counting method based on read message record and cache technology optimization. Based on the above-described unread message counting system based on read message record and cache technology optimization, the method comprises the following steps:
[0020] S1. New message arrival: When a new message is sent to a user, the user's unread message counter in the Redis cache increases by the corresponding value;
[0021] S2, message reading: After reading a new message, the user marks the new message as read and adds the new message details to the message detail table; at the same time, the corresponding unread message count in the Redis cache is reduced;
[0022] S3. Query the number of unread messages: Calculate the number of unread messages by using the read message count value obtained from the Redis cache and the number of read messages queried in the message detail table.
[0023] Preferably, in step S1, the process of new message arrival is as follows:
[0024] S11. When a new message is sent to a user, first determine the user ID of the receiving message;
[0025] S12. Find the unread message counter corresponding to the user in the Redis cache, and increase the value of the unread message counter by 1;
[0026] S13. The new message is stored in the message storage system. Since the new message has not been read yet, the message list is not updated immediately.
[0027] Preferably, in step S2, the process of message reading is as follows:
[0028] S21. After the user reads the new message, the system first marks the new message as read;
[0029] S22, inserting the details of the new message into the message detail table and recording the reading time;
[0030] S23. Find the unread message counter corresponding to the user in the Redis cache, and subtract 1 from the value of the unread message counter.
[0031] Preferably, in step S3, the process of querying the number of unread messages is as follows:
[0032] S31, when querying the total number of unread messages of a specific user, first obtain the current unread message count value of the user from the Redis cache;
[0033] S32, then query the number of read messages of the user from the message details table according to the user ID;
[0034] S33. Calculate the difference between the unread message count value in the Redis cache and the number of read messages, which is the final number of unread messages.
[0035] Preferably, when obtaining the unread message count value from the Redis cache, the system constructs a Redis key according to the user ID, and then uses the Redis cache GET command to obtain the value of the Redis key, which is the current unread message count value of the user in the Redis cache.
[0036] Preferably, when querying the number of read messages from the message detail table, the system queries all read message records of the user from the message detail table according to the user ID, and then calculates the number of read messages using an aggregate function of an SQL query.
[0037] Preferably, in step S33, if the unread message value in the Redis cache is empty, the unread messages are regarded as 0, and if the number of read messages queried from the message detail table is 0, the unread message count value in the Redis cache is directly returned.
[0038] Compared with the prior art, the present invention has the following beneficial effects:
[0039] 1. The present invention only records the read messages, which greatly saves storage space and greatly reduces the data storage requirements compared with the traditional method. It is suitable for application scenarios with a large amount of messages but a relatively low reading rate;
[0040] 2. The Redis cache of the present invention provides high-speed read and write operations, and can complete data reading and writing in microseconds, and is used for real-time updating and querying of unread message counts. By using the Redis cache to directly obtain the unread message counts, full table scanning is avoided, and query efficiency is greatly improved.
[0041] 3. The present invention only involves read messages, and the maintenance cost of the message detail table is relatively low. There is no need to frequently clean up unread messages or process expired message records. BRIEF DESCRIPTION OF THE DRAWINGS
[0042] Figure 1 A flowchart of the unread message counting method of the present invention;
[0043] Figure 2 A flowchart of a traditional unread message counting method;
[0044] Figure 3 It is a flowchart of the unread message counting method of the present invention;
[0045] Figure 4 It is a flowchart of the new message arrival of the present invention;
[0046] Figure 5 A flowchart of message reading of the present invention;
[0047] Figure 6 This is a flowchart of querying the number of unread messages of the present invention. DETAILED DESCRIPTION
[0048] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0049] The present invention provides a technical solution: an unread message counting system based on read message recording and cache technology optimization, comprising:
[0050] Message detail table: recorded as t_ms_detail table, which only stores the relevant information of the read messages;
[0051] The fields in the message details table include:
[0052] ID: unique identifier of the message;
[0053] User-ID: the user ID receiving the message;
[0054] message_content: message content;
[0055] read_time: the time when the message was marked as read;
[0056] Sender ID: identifies the user or system that sent the message;
[0057] Message type: such as text message, picture message, voice message, etc., used to distinguish the type of message;
[0058] Message priority: used to identify the urgency or importance of the message;
[0059] Message-related data: such as the session ID to which the message belongs, the order of the messages in the session, etc.
[0060] The ID type in the field is an integer or a string, which is used to distinguish different message records; the User-ID type in the field is an integer or a string, which is used to identify the recipient of the message; the message_content type in the field is text, link, or picture; the read_time type in the field is a timestamp, which is used to analyze the user's message reading habits or optimize the message push strategy.
[0061] Since only read messages are stored, the data volume of the message detail table will be significantly reduced compared to storing all messages, which is a huge advantage for systems with a large amount of messages and a low reading rate.
[0062] Due to the small amount of data, querying the read messages is usually faster; since only the read messages are involved, the maintenance cost of the table is relatively low. For example, there is no need to frequently clean up unread messages or process expired message records.
[0063] Redis cache: used to update the unread message count of each user in real time; each user has an independent key-value pair, where the key is the user ID and the value is the current number of unread messages;
[0064] Redis cache is an open source, memory-based data storage system, often used as a cache, database, and message middleware. It supports multiple data structures. In the scenario of real-time updating of user unread message counts, its simple and efficient key-value data structure is used.
[0065] Compared with traditional disk-based databases, Redis's memory storage feature makes its reading and writing speeds extremely fast, and it can complete data update and query operations in a very short time. On a large social platform, there may be tens of thousands of messages sent per second. Using Redis can ensure that the unread message count of each user can be updated in near real time, allowing users to see the latest number of unread messages without obvious delays.
[0066] The key in the Redis cache is used to uniquely identify a user's unread message count, and the value in the Redis cache is updated in real time as new messages arrive and are read.
[0067] The key is usually a string consisting of the "user:" prefix and the user ID, such as user:123;
[0068] The key is unique per user, ensuring that each user's unread message count is stored independently; when a new message arrives, the user's unread message count increases, and when a user reads a message, the user's unread message count decreases.
[0069] See also Figure 1 , Figure 3-6 The present invention also provides an unread message counting method based on read message record and cache technology optimization. Based on the above-described unread message counting system based on read message record and cache technology optimization, the method comprises the following steps:
[0070] S1. New message arrival: When a new message is sent to a user, the user's unread message counter in the Redis cache increases by the corresponding value;
[0071] In step S1, the process of new message arrival is as follows:
[0072] S11. When a new message is sent to a user, first determine the user ID of the receiving message;
[0073] S12. Find the unread message counter corresponding to the user in the Redis cache, and increase the value of the unread message counter by 1;
[0074] S13. The new message is stored in the message storage system. Since the new message has not been read yet, the message list is not updated immediately.
[0075] When a new message needs to be sent to a user, the system first needs to determine the recipient of the message, that is, the user ID. Once the user ID receiving the message is determined, the system will search for the unread message counter corresponding to the user in Redis, and then increase the value of this counter by 1 (if only one message is sent at a time). If multiple messages are sent at a time, the corresponding value needs to be increased.
[0076] After updating the unread message counter in Redis, the system needs to store the new message in the message storage system. The message storage system can be a database (such as MySQL, PostgreSQL, etc.) or a message queue (such as RabbitMQ, Kafka, etc.).
[0077] S2, message reading: After reading a new message, the user marks the new message as read and adds the new message details to the message detail table; at the same time, the corresponding unread message count in the Redis cache is reduced;
[0078] In step S2, the message reading process is as follows:
[0079] S21. After the user reads the new message, the system first marks the new message as read;
[0080] This step involves updating the message status in a message storage system (such as a database or a message queue) to indicate that the message has been read by the user.
[0081] S22, inserting the details of the new message into the message detail table and recording the reading time;
[0082] After marking a message as read, the system needs to insert the details of the message (including user ID, message content, reading time, etc.) into the message detail table (t_ms_detail) for subsequent query and classification.
[0083] The insert operation should contain all the necessary fields, such as the unique identifier of the message (id), the ID of the user who received the message (user_id), the message content (message_content), and the time when the message was marked as read (read_time).
[0084] S23. Find the unread message counter corresponding to the user in the Redis cache, and subtract 1 from the value of the unread message counter.
[0085] If you read multiple messages at once, you need to subtract the corresponding values.
[0086] S3. Query the number of unread messages: Calculate the number of unread messages by using the read message count value obtained from the Redis cache and the number of read messages queried in the message detail table.
[0087] In step S3, the process of querying the number of unread messages is as follows:
[0088] S31, when querying the total number of unread messages of a specific user, first obtain the current unread message count value of the user from the Redis cache;
[0089] In step S31, when obtaining the unread message count value from the Redis cache, the system constructs a Redis key according to the user ID, and then uses the Redis cache GET command to obtain the value of the Redis key, which is the current unread message count value of the user in the Redis cache.
[0090] S32, then query the number of read messages of the user from the message details table according to the user ID;
[0091] In step S32, when querying the number of read messages from the message detail table, the system queries all read message records of the user from the message detail table according to the user ID, and then calculates the number of read messages using the aggregate function of the SQL query.
[0092] S33. Calculate the difference between the unread message count value in the Redis cache and the number of read messages, which is the final number of unread messages.
[0093] In step S33, if the unread message value in the Redis cache is empty, the unread messages are regarded as 0. If the number of read messages queried from the message detail table is 0, the unread message count value in the Redis cache is directly returned.
[0094] Optionally, after calculating the number of unread messages, the final number of unread messages is returned, and the system returns the unread message count value in Redis as the final number of unread messages to the user.
[0095] Although embodiments of the present invention have been shown and described, it will be appreciated by those skilled in the art that various changes, modifications, substitutions and variations may be made to the embodiments without departing from the principles and spirit of the present invention, and that the scope of the present invention is defined by the appended claims and their equivalents.
Claims
1. An unread message counting system based on read message record and cache technology optimization, characterized in that: include: Message details table: only saves the relevant information of the read messages; Redis cache: used to update the unread message count of each user in real time; Each user has a separate key-value pair, where the key is the user ID and the value is the number of currently unread messages; The fields in the message details table include: ID: unique identifier of the message; User-ID: The user ID that receives the message; message_content: message content; read_time: the time when the message was marked as read; The key in the Redis cache is used to uniquely identify a user's unread message count, and the value in the Redis cache is updated in real time as new messages arrive and are read.
2. The unread message counting system based on read message recording and cache technology optimization according to claim 1 is characterized in that: The ID type in the field is an integer or a string, which is used to distinguish different message records; the User-ID type in the field is an integer or a string, which is used to identify the recipient of the message; the message_content type in the field is text, link, or picture; the read_time type in the field is a timestamp, which is used to analyze the user's message reading habits or optimize the message push strategy.
3. The unread message counting system based on read message recording and cache technology optimization according to claim 1 is characterized in that: The key is unique per user, ensuring that each user's unread message count is stored independently; when a new message arrives, the user's unread message count increases, and when a user reads a message, the user's unread message count decreases.
4. A method for counting unread messages based on read message records and optimized cache technology, a system for counting unread messages based on read message records and optimized cache technology according to any one of claims 1 to 3, characterized in that: The following steps are involved: S1. New message arrival: When a new message is sent to a user, the user's unread message counter in the Redis cache increases by the corresponding value; S2, message reading: After reading a new message, the user marks the new message as read and adds the new message details to the message detail table; at the same time, the corresponding unread message count in the Redis cache is reduced; S3. Query the number of unread messages: Calculate the number of unread messages by using the read message count value obtained from the Redis cache and the number of read messages queried in the message detail table.
5. The unread message counting method based on read message record and cache technology optimization according to claim 1 is characterized in that: In step S1, the process of new message arrival is as follows: S11. When a new message is sent to a user, first determine the user ID of the receiving message; S12. Find the unread message counter corresponding to the user in the Redis cache, and increase the value of the unread message counter by 1; S13. The new message is stored in the message storage system. Since the new message has not been read yet, the message list is not updated immediately.
6. The unread message counting method based on read message record and cache technology optimization according to claim 1 is characterized in that: In step S2, the message reading process is as follows: S21. After the user reads the new message, the system first marks the new message as read; S22, inserting the details of the new message into the message detail table and recording the reading time; S23. Find the unread message counter corresponding to the user in the Redis cache, and subtract 1 from the value of the unread message counter.
7. The unread message counting method based on read message record and cache technology optimization according to claim 1 is characterized in that: In step S3, the process of querying the number of unread messages is as follows: S31, when querying the total number of unread messages of a specific user, first obtain the current unread message count value of the user from the Redis cache; S32, then query the number of read messages of the user from the message details table according to the user ID; S33. Calculate the difference between the unread message count value in the Redis cache and the number of read messages, which is the final number of unread messages.
8. The unread message counting method based on read message record and cache technology optimization according to claim 7 is characterized in that: When obtaining the unread message count value from the Redis cache, the system will construct a Redis key based on the user ID, and then use the Redis cache GET command to obtain the value of the Redis key. This value is the current unread message count value of the user in the Redis cache.
9. The unread message counting method based on read message record and cache technology optimization according to claim 7 is characterized in that: When querying the number of read messages from the message detail table, the system queries all read message records of the user from the message detail table based on the user ID, and then uses the aggregate function of the SQL query to calculate the number of read messages.
10. The unread message counting method based on read message record and cache technology optimization according to claim 7, characterized in that: In step S33, if the unread message value in the Redis cache is empty, the unread messages are regarded as 0. If the number of read messages queried from the message detail table is 0, the unread message count value in the Redis cache is directly returned.