A method for dynamic caching of instant messaging based on roaming data and local database
By using a dynamic caching method for instant messaging, and leveraging differentiated incremental synchronization and gap repair of roaming summaries and local databases, the problems of high traffic consumption and low caching efficiency in instant messaging under weak network conditions are solved, thereby improving user experience and storage management efficiency.
Patent Information
- Application Number
- CN202611068422.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-07-17
- Publication Date
- 2026-11-10
AI Technical Summary
Existing instant messaging technologies suffer from high traffic consumption, high database I/O pressure, low cache utilization efficiency, and storage space expansion in weak network environments, especially when users switch work areas or jump to historical messages.
We adopt a real-time dynamic caching method based on roaming data and local database. By predicting roaming summaries, differentiating incremental synchronization, targeted gap repair, and lightweight dynamic caching, we optimize message synchronization strategies, reduce traffic consumption and database pressure, and improve cache utilization and user experience.
It effectively reduces network traffic consumption, alleviates database I/O pressure, improves message loading speed and user experience, ensures the continuity of message browsing and efficient use of cache, and avoids unlimited expansion of storage space.
Smart Images

Figure CN123075008A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of instant messaging data processing technology, and in particular to an instant messaging dynamic caching method based on roaming data and a local database. Background Technology
[0002] With the advancement of digital office work, enterprise instant messaging has become a core tool for cross-scenario collaboration. Users often need to view historical messages and locate target content in environments with weak or even no network, such as during commuting or outdoor work. In order to balance cloud synchronization capabilities and local access efficiency, existing solutions generally adopt a basic architecture of storing all roaming messages on the server side and caching loaded content locally on the client side.
[0003] During cache synchronization, existing clients mostly use fixed-batch retrieval logic. When a user logs in for the first time, switches work areas, or enters an unfamiliar session, they directly request a preset number of historical messages from the server without pre-checking the synchronization progress of locally cached messages. In this case, even if most of the historical data has been retained locally, a large number of duplicate message requests will still be generated, increasing data consumption in mobile network environments. At the same time, writing a large number of messages to the local database will continuously occupy disk I / O resources, which can easily cause problems such as interface lag and device overheating, especially on low-end devices with limited performance. In historical message navigation scenarios, existing solutions default to directly fetching a fixed range of context content before and after the target message from the server. This approach neither prioritizes matching existing local cache ranges nor performs continuity checks on the loaded message sequence. However, this method not only leads to repeated requests for the same historical message, resulting in low utilization efficiency of the local cache, but also requires the client to store all accessed historical messages in full to ensure a smooth navigation experience. This causes the local database capacity to continuously expand over time, consuming excessive device storage space. To address this, we propose a real-time dynamic caching method based on roaming data and a local database. Summary of the Invention
[0004] To address the aforementioned problems in existing technologies, the present invention aims to provide a dynamic caching method for instant messaging based on roaming data and a local database. Through a full-link design that includes roaming summary pre-judgment, differentiated incremental synchronization, targeted gap repair, and lightweight dynamic caching, the method ensures message integrity while reducing traffic consumption and write pressure, and improves loading speed and user experience in weak network environments.
[0005] To solve the above problems, the technical solution adopted by the present invention is as follows: A dynamic caching method for instant messaging based on roaming data and a local database is applied to an instant messaging client. The server stores all roaming messages and only sends roaming summary data to the client. The steps of this method are as follows: Obtain roaming summary data issued by the server side. The roaming summary data includes the channel identifier of each message channel, the latest message summary, the incremental sequence number of messages in the channel assigned by the server side, the number of unread messages, and the channel joining status identifier. Read the maximum incremental sequence number of the message in the corresponding channel that has been cached in the local database, calculate the sequence difference with the latest incremental sequence number of the message in the channel on the server side, and then evaluate the local cache coverage status. A differentiated incremental retrieval strategy is generated based on the sequence difference, and the message data is written to the local database in batches after the incremental message synchronization is performed; In response to the anchor point location request of the target message, read the target message and the corresponding range of context messages from the local database, and verify the continuity of the message sequence within the loading range; Identify message gaps within the loading interval, match hierarchical replenishment rules according to the number of messages contained in the gap, fill in the missing messages, and write them to the local database. Once the target context is loaded, a background asynchronous warm-up task is started to dynamically expand the cache coverage around the target message and store message data in the high-frequency access range of the user into the local database.
[0006] Preferably, the local database uses a two-level table structure to store data, including a local message table and a local channel table; The local message table stores the complete content and attributes of a single message. Fields include channel ownership identifier, server-side message unique identifier, client-side local message identifier, message increment sequence number within the channel, message timestamp, message status, sending status, and work area ownership identifier. The local channel table stores the aggregated data required for displaying the session list. Fields include the last message content, last message time, last message type, number of unread messages, mention status, and channel joining identifier. The local message table establishes a joint index for the channel ownership identifier and the incremental sequence number of messages within the channel, and a unique index for the unique identifier of the service-side messages.
[0007] Preferably, when a client enters or switches a workspace, it first retrieves all basic channel data for the current workspace and writes it to the local channel table; then, it prioritizes extracting the latest message sequence number within the latest channel from the latest message summary of the joined channel, and extracts it from the channel attribute field if the summary has no valid sequence number. Query the maximum channel-incrementing sequence number of valid messages in the local message table, calculate the sequence difference, and take the smaller value between the sequence difference and the preset single-channel preheating quantity threshold as the actual retrieval scale. If a valid message sequence number within the service-side channel is not obtained, the service-side interface returns an error, or the number of recently cached messages locally is insufficient to meet the preset single-channel preheating threshold, the latest messages from the channel will be retrieved according to the preset single-channel preheating threshold.
[0008] Preferably, the retrieved messages are uniformly converted into a local message table storage structure and then batch-stored into the database. Duplicate messages are replaced and written using the unique index corresponding to the unique identifier of the service-side message. When the client initiates message sending, it first generates a local message identifier and writes the sending status into the local message table. After receiving the confirmation receipt from the server, it uses the local message identifier as the matching basis to write back the server's unique message identifier, the message increment sequence number in the corresponding channel, and the sending success status to the same local record. After a message is written, the fields related to the last message for the corresponding channel in the local channel table are refreshed synchronously.
[0009] Preferably, when the client recovers the network after going offline, the offline roaming data directory on the server side is retrieved in pages. If there is no more offline channel data, the pagination stops and all offline channel data is summarized. Iterate through the latest message summary and the incremental message sequence number in each offline channel, compare them one by one with the maximum incremental message sequence number in the corresponding channel in the local message table, and calculate the sequence difference as the message gap. First, the incremental messages within the message gap are retrieved and stored. After the message sequence is completed, the number of unread messages, the last message time, the last message content, the last message type and the mention status of the local channel table are calibrated based on the roaming summary data, and the channel statistics are written back asynchronously.
[0010] Preferably, after receiving an anchor point location request carrying a channel identifier and a target message identifier, the target message is first retrieved from the local message table. The triggering scenarios for the anchor point location request include search result jump, notification click, mention message jump, reference message viewing, and channel historical message location. When the target message exists in the local message table, read the message increment sequence number in the corresponding channel, and query the preceding context message with a sequence number less than the target value and the subsequent context message with a sequence number greater than the target value respectively. The query scale before and after the target message is matched with the preset context loading threshold. After all messages are sorted in ascending order by their channel serial number, the target message's index is used as the anchor point to return to the interface display layer, simultaneously triggering a message sequence continuity check within the loading interval.
[0011] Preferably, when the target message is not stored in the local message table, the server-side single message query interface is first called to obtain the target message and write it into the local message table, and then the context messages of the corresponding scale before and after the target message are retrieved and stored in the database. After all messages are written, a context list is generated by sorting the messages in ascending order of their sequence number within the channel. The target message's index is used as the anchor point to return to the interface display layer, and the message sequence continuity check of the loading interval is performed synchronously.
[0012] Preferably, when verifying the continuity of the message sequence in the loading interval, first generate the expected continuous sequence number set within the range of message increment sequence numbers in the starting and ending channels of the loading interval, and perform a difference operation with the local actual set of message increment sequence numbers in the channel to obtain all missing sequence numbers; Consecutive adjacent missing sequence numbers are merged into independent message gap intervals. Multiple short message gap intervals with an interval distance less than the preset interval merging threshold are merged into the same interval to be filled.
[0013] Preferably, when performing message gap filling, the anchor point is matched and pulled according to the gap direction. The message gap interval in the preceding direction takes the message after the end sequence number of the interval as the anchor point and calls the descending historical query interface to pull the message. The message gap in the subsequent direction is filled by calling the ascending historical query interface with the previous message as the anchor point of the interval's starting sequence number. When a single message gap contains only one missing message, a single message is retrieved. When the number of missing messages does not exceed the preset batch retrieval threshold, a full retrieval is performed. When the number of missing messages exceeds the preset batch retrieval threshold, retrieval is performed page by page according to a fixed page size. The unique identifier of the service-side message of the last message returned on the previous page is used as the request anchor point for the next page. All messages retrieved are written to the local message table.
[0014] Preferably, after the target context completes the interface display, an asynchronous background warm-up task is started without blocking user interaction. Starting from the two ends of the current loading interval, the cache continuity is checked in both forward and backward directions within a preset range, and message gaps are filled when they exist. When a user scrolls through messages, the local message table is read first, and a server request is only initiated when there is a message gap in the target browsing direction. The local database only retains message data from joined channels, recent conversations, unread channels, and surrounding areas linked by anchor points. Historical messages that have not been accessed by users are not pre-fetched or stored locally. When logging in from multiple terminals or switching work areas, the unread status of the local channel table and the display status of the channel list are aligned with the server-side data based on the roaming summary data.
[0015] Compared with the prior art, the beneficial effects of the present invention are as follows: When a client enters or switches workspaces, it first retrieves lightweight roaming summary data from the server. It then compares the incremental message sequence numbers within the channel on the local machine with those on the server to determine the cache coverage progress, performing message synchronization only for the missing incremental parts locally. This method avoids triggering a fixed batch of full fetches each time and effectively compresses unnecessary network requests, adapting to traffic control requirements in mobile network environments. Simultaneously, the incremental writing method avoids large-scale message dumping into the database, alleviating disk I / O pressure and reducing the probability of interface lag and device overheating. Secondly, when processing historical message jump requests in scenarios such as search results and notification clicks, it prioritizes reading locally cached message content, performing targeted fetching only for missing sequence intervals. The cache range gradually expands following the user's actual access behavior. This reduces repeated fetching of the same message segment and improves the utilization efficiency of the local cache. Furthermore, the client does not need to pre-store all historical messages, keeping the local database storage space usage within a controllable range and preventing unlimited expansion over time.
[0016] Furthermore, during offline recovery, gaps in the message sequence are filled first, and then the statistical display data of the session list is updated. When users enter a session, there will be no time sequence break problem where the last message is displayed and the middle content is missing, ensuring the continuity of message browsing. Among them, hierarchical patching rules are matched for message gaps of different sizes: single gaps are repaired directly, batch gaps are filled at once, and large-scale gaps are processed in pages. This balances the efficiency of sequence repair and the stability of interface requests, and avoids gap repair operations affecting the interactive response of the current interface. Attached Figure Description
[0017] Figure 1 This is a diagram showing the overall structure of the dynamic caching system of the present invention; Figure 2 This is a flowchart of the dynamic preheating process based on the difference between the roaming summary and the local roomSeq in this invention; Figure 3 This is a flowchart of the gap filling and statistical write-back process during offline message synchronization in this invention; Figure 4 This is a flowchart of the target message context loading and bidirectional extension preheating process in this invention; Figure 5 This is a flowchart of the hierarchical filling process based on roomSeq gap intervals in this invention. Detailed Implementation
[0018] The present invention will be further described below with reference to specific embodiments.
[0019] This application provides a dynamic caching method for instant messaging based on roaming data and a local database. This method ensures message integrity while reducing traffic consumption and write pressure, and improves loading speed and user experience in weak network environments. It is primarily applied to enterprise-level instant messaging clients with multiple workspaces. Existing technologies generally employ a basic architecture combining server-side storage of all roaming messages and client-side local caching of loaded content. Server-side full storage ensures users can synchronize all historical messages across terminals, while client-side local caching improves the loading speed of frequently used messages. While this architecture meets basic synchronization requirements, it commonly suffers from problems such as coarse synchronization logic, high traffic consumption, message sequence gaps during offline recovery, repeated historical data retrieval, and unlimited cache expansion. Therefore, we provide a new solution, the details of which are as follows: Based on the existing architecture described above, refer to the appendix Figure 1 The overall structure of the dynamic caching system shown is as follows: on the server side, an independent roaming message storage cluster is deployed to retain the complete content of all historical messages. Instead of directly sending the complete chat content to the client, only lightweight aggregated roaming summary data is sent to avoid the bandwidth consumption and performance pressure caused by full message synchronization. The client-side architecture consists of eight modules that work collaboratively: roaming summary acquisition, local state assessment, dynamic fetching decision-making, context loading, gap detection and repair, offline message completion, statistical write-back, and local cache scheduling. All data is stored in a local database and ultimately output to the client's message display layer. The overall architecture uses the message incrementing sequence number allocated by the server within the channel as the sole core timing benchmark, corresponding to roomSeq in the attached diagram. This sequence number is a globally incrementing integer sequence maintained independently by the server for each channel, incrementing by 1 for each new message. Compared to the timestamp benchmark commonly used in existing technologies, this avoids sorting errors caused by clock asynchrony across multiple devices. All synchronization progress judgments, message sorting, range queries, and gap detection throughout the entire process are completed based on the same sequence number benchmark, avoiding sequence chaos caused by multiple timing standards. It is adaptable to various usage scenarios such as weak networks, offline, and multiple terminals. The client-side uses a widely compatible SQLite database for caching and storage. The overall architecture balances synchronization efficiency, storage usage, and interaction smoothness.
[0020] Local database dual-layer storage architecture: Existing technologies generally use a single message table to store all message data. This design is simple in structure and low in implementation cost, and can meet basic message storage needs. However, when there are many channels, the session list rendering needs to aggregate data such as the last message and unread messages in real time, which takes a long time to query and can easily cause list scrolling to lag. To address this issue, we optimize it to a dual-layer table structure design, which respectively carries message details and session aggregate data. The association and matching of the two tables are realized through the channel ownership identifier. All table structures and index designs are optimized around the query and write requirements of the entire process.
[0021] The local message table stores the complete attributes of a single message, corresponding to the chat table in the attached diagram. Core fields include the channel ownership identifier (fk_room_id), the server-side message unique identifier (fk_msg_id), the client-side local message identifier (local_id), the channel-internal message incrementing sequence number (room_seq), the message timestamp (timestamp), the message status (msg_status), the sending status (send_status), and the workspace ownership identifier (workspace_id). The functions of each field are as follows: the channel ownership identifier corresponds one-to-one with the server-side channel ID, serving as a unified filtering condition for queries across all channel dimensions; the server-side message unique identifier is a globally unique ID assigned by the server, universal across terminals, and is the core basis for cross-terminal message matching and deduplication; the client-side local message identifier is a temporary unique identifier generated when sending a message, effective only locally, used for record matching during the sending status write-back process, and is not a time-series benchmark field; the channel-internal message incrementing sequence number is the server-side globally incrementing sequence mentioned earlier, and is the core benchmark for the entire process of sorting, querying, and judging; the workspace ownership identifier is used to mark the workspace to which the message belongs, supporting data isolation in multi-workspace scenarios.
[0022] For high-frequency range query scenarios throughout the entire process, two types of dedicated indexes are set up in the local message table, as follows: One type is a combined index for channel affiliation identifier and message incrementing sequence number within the channel. Since all sessions in the entire process have four high-frequency operations, namely loading recent messages, scrolling up to load history, searching and jumping to locate context, and gap detection to query range messages, they all need to be filtered by channel first and then retrieved by sequence number range. Frequent full table scans can cause interface lag. This combined index can reduce the time consumption of such high-frequency range queries by an order of magnitude, adapting to the performance constraints of mobile devices. Another type is a unique index that uniquely identifies the message on the service side. Relying on SQLite's native conflict replacement mechanism, it automatically overwrites the original record when writing duplicate messages, realizing message deduplication from the database layer. This eliminates the need for the business layer to perform additional duplicate verification logic, further improving write efficiency.
[0023] Another local channel table stores the aggregated data required for displaying the session list. Corresponding to the channel table in the attached figure, the core fields include the last message content, last message time, last message type, number of unread messages, mention status, channel joining identifier, and work area affiliation identifier. All fields are aggregated from the message data, and the session list can be directly read from this table when rendering, without having to perform aggregation calculations from the message table each time.
[0024] General message writing and state transition mechanism: All message writing follows a unified general rule, employing a mechanism combining batch writing and underlying deduplication. This rule is applicable to all scenarios involving message writing, such as full-channel synchronization, offline synchronization, gap filling, and anchor loading. Figure 1 The core execution logic of the statistical write-back module.
[0025] All messages retrieved from the server are uniformly converted into a local message table storage structure and then batch-written using database transactions. This means that the write operations of multiple messages are merged into a single transaction and committed. At the same time, the atomicity of transactions ensures that no half-written data will occur in the event of an exception during the write process, thus avoiding database file corruption. During the write process, duplicate messages are automatically handled using the unique index of the unique identifier of the message on the server side. After completion, the relevant fields of the last message of the corresponding channel in the local channel table are refreshed synchronously to ensure that the content displayed in the session list is consistent with the latest cache, without the need to trigger additional aggregation queries.
[0026] For scenarios where the client actively sends messages, a write-ahead and write-back state transition mechanism is adopted: When a message is sent, it has not yet received confirmation from the server and does not yet have a unique identifier for the server-side message or a formal in-channel message increment sequence number. Therefore, a local message identifier is generated on the client first as the matching basis, and messages marked as being sent are written to the local message table, ensuring that users can immediately see the content they sent on the interface, achieving an optimistic interactive experience. After the server returns a confirmation receipt, the corresponding record is matched using the client's local message identifier, and the formal message identifier and in-channel message increment sequence number assigned by the server are written back. The sending status is then updated to sent successfully. Only one message record is retained throughout the process to avoid the problem of duplicate display on the interface caused by duplicate entries of "sending" and "sent successfully" records in the database. In this process, the client's local message identifier is only a temporary matching basis. After message synchronization is completed, the in-channel message increment sequence number is still used as the unique timing benchmark to ensure that subsequent gap repair, search query, and other operations can accurately locate the corresponding message using the server-side sequence number.
[0027] Full-channel incremental synchronization process: When a client starts up for the first time or switches between different work areas, it needs to complete the cache synchronization of all channels. Existing technologies mostly use fixed batch fetch logic, which fetches a fixed number of the most recent messages from each channel when entering the work area, regardless of how much content is cached locally. This logic is simple to implement, but it generates a large number of duplicate requests, consumes a lot of data on mobile networks, and can also cause a large number of messages to be written at once, increasing disk I / O pressure and causing low-end devices to overheat and the interface to lag. The execution flow for this scenario can be found in the appendix. Figure 2 The dynamic preheating process shown is based on the difference between the roaming summary and the local sequence number. For this scenario, a lightweight cache state prediction + differentiated incremental synchronization logic is adopted to pull only the minimum necessary data that is missing locally.
[0028] The client first calls the workspace channel interface to retrieve all basic channel data for the current workspace, writing fields such as channel identifier, workspace identifier, channel type, join status, and unread message count into the local channel table. This completes the basic rendering of the session list, allowing users to quickly see a channel overview without waiting for full message synchronization, thus optimizing the perceived speed of startup and switching scenarios. For each joined channel, the client sets up a two-level fallback logic to extract the latest incremental sequence number of messages within the channel from the server side. It prioritizes extraction from the latest message digest returned by the interface. If the digest field is missing, has an abnormal format, or has no valid sequence number, it extracts from the channel's top-level attribute fields as a fallback, ensuring that each channel can obtain a reliable synchronization benchmark and that synchronization will not fail due to abnormalities in individual fields.
[0029] After extracting the sequence number, the client queries the local message table for the maximum incremental sequence number of valid messages in the corresponding channel. The difference between the latest sequence number on the server and the maximum local sequence number is calculated. A difference of 0 indicates that the local cache is fully synchronized with the server and no additional fetching is needed. A difference greater than 0 indicates varying degrees of lag on the local cache. The client presets a single-channel preheating threshold as the upper limit for a single fetch, with a default value of 20 messages. The actual number of messages fetched is the smaller of the sequence difference and the preheating threshold. When the lag is small, only the missing messages are filled in. For example, if the latest sequence number on the server is 105 and the maximum local sequence number is 100, the difference is 5, and only 5 missing messages need to be fetched. When the lag is large, only messages from the most recent window are fetched. For example, if the server sequence number has reached 3000 and the local sequence number has only reached 1200, the difference is 1800 messages, and only the most recent 20 messages are fetched to ensure the first screen display. This avoids synchronizing all historical data at once, reducing traffic consumption and database write pressure during startup.
[0030] If the server-side interface returns a timeout or an exception that prevents the retrieval of a valid sequence number, or if the number of recently cached messages locally is insufficient to meet the preheating threshold for a single channel, the client will fetch the latest messages from the channel according to the threshold as a fallback mechanism to prevent the channel from having no content to display and to ensure a basic browsing experience. The fetch operation calls the server-side descending historical message interface. Channel-type rooms call the / msg / channelRoomPageDescMsgId interface, and ordinary rooms call the / msg / roomPageDescMsgId interface. Starting from the position of the latest message on the server, the corresponding number of messages are retrieved backward to match the fetching requirements of the most recent messages. After all messages are written to the local message table, the corresponding fields in the local channel table are refreshed synchronously. After the full channel synchronization process is completed, the background asynchronously triggers the preheating detection of the range of the most recent messages for each channel to further expand the cache coverage.
[0031] Offline recovery dedicated synchronization process: When the client reconnects to the network after a network outage, the offline recovery dedicated synchronization process is triggered. Existing technologies mostly adopt the logic of first updating the last message in the session list and then synchronizing messages one by one. This logic can quickly update the session list display, but it is prone to the problem of a time sequence gap where the session list has already displayed the latest message, but when you click into the session, hundreds of messages are missing. This results in a broken sequence when the user scrolls forward, leading to a very poor user experience. The execution logic for this scenario can be found in the appendix. Figure 3 The offline message gap filling and statistics write-back process shown adopts the execution order of filling messages first and then updating the list to solve the time sequence gap problem.
[0032] The client first fetches the offline roaming data directory from the server in pages, returning a fixed number of offline channels per page. Page numbers start from 1 and increment sequentially until the server returns no more data. Page fetching avoids excessive load on a single request when there are too many channels, and adapts to transmission stability in weak network environments. After summarizing the summary information of all offline channels, they are processed one by one. During the traversal, the latest incremental message sequence number within each channel is extracted and compared with the maximum sequence number of the corresponding channel in the local message table to calculate the message gap. Incremental message fetching is performed first and the local database is completed. After the message sequence is completely filled, the roaming data is then retrieved. The summary calibrates the number of unread messages, the fields related to the last message, and the mentioned status in the local channel table. All statistical updates are executed asynchronously in a thread without blocking the main interface. For example, if 50 new messages are added to a channel while the user is offline, existing technology will first update the summary of the 50th message to the channel table. When the user clicks to enter the conversation, they can only see the 50th message, with messages 1-49 missing. However, this invention will first complete the local message table with messages 1-50, and then update the last message in the channel table. When the user enters the conversation, they can see 50 consecutive messages without any sequence breakage.
[0033] Single-session anchor context loading process: When a user triggers an anchor location request through entry points such as search results, notification clicks, mentioned messages, referenced messages, or channel history, it is necessary to quickly locate the target message and load the context. Existing technology defaults to directly fetching a fixed number of items before and after the target message from the server, making repeated requests regardless of whether the corresponding range is cached locally. This logic is simple to implement, but the cache utilization is extremely low, generating a large amount of redundant traffic. The complete process of anchor location and context loading can be found in the appendix. Figure 4 The target message context loading and bidirectional expansion preheating process shown covers the entire chain of local hit, local miss, gap repair, and asynchronous preheating. For this scenario, the logic of local priority and on-demand replenishment is adopted, prioritizing the reuse of local cache and only initiating network requests for missing content.
[0034] All anchor point location requests carry two core parameters: the target channel identifier and the target message identifier. After receiving the request, the client first retrieves the target message from the local message table. If the target message exists in the local message table, it reads the message's ascending sequence number within the corresponding channel and queries the preceding content with a sequence number less than the target value and the subsequent content with a sequence number greater than the target value. The query scale matches the preset context loading threshold, with a default of 20 entries for each side, conforming to the user's usual context viewing habits when browsing target messages. All results are merged with the target message and sorted in ascending order by the message's ascending sequence number within the channel. The index of the target message in the list is recorded as the location anchor point to return to the interface, allowing direct scrolling to the corresponding position for display.
[0035] If the target message is not stored locally, the client follows a target-first, context-later loading order: first, it calls the server-side single message query interface / msg / getById to retrieve the target content and write it locally, allowing the user to see the core target message as quickly as possible. Then, it calls the descending and ascending history interfaces to retrieve context messages of the corresponding size before and after the target and completes the storage. After all messages are written, they are also sorted in ascending order to generate a context list, and the target message index is returned as the positioning anchor point. Regardless of whether the context is found locally or loaded from the network, the message sequence continuity check within the interval will be automatically triggered after loading is completed to ensure the continuity of browsing.
[0036] Message sequence continuity verification and tiered patching mechanism: Continuity verification and gap repair after context loading are the core steps to ensure message sequence integrity. The execution logic for gap detection and tiered patching can be found in the appendix. Figure 5 The illustrated hierarchical patching process based on the sequence number gap range sets up complete gap detection and hierarchical patching logic to accurately repair missing content while controlling interface request pressure.
[0037] The gap identification rules for continuity verification are as follows: Extract the message increment sequence number within the minimum and maximum channels of the currently loaded list, generate the expected continuous sequence number set within this interval, which is all the sequence numbers that should theoretically exist, and perform a difference operation with the actual sequence number set on the local machine to obtain all missing sequence numbers; then merge consecutive adjacent missing sequence numbers into independent message gap intervals. Multiple gaps with an interval distance less than the preset interval merging threshold (default 5 sequence number intervals) will be merged into the same interval to be filled, reducing the number of interface requests and reducing the interface pressure on the server.
[0038] The gap filling operation matches different anchor point rules and interface types according to the gap direction to ensure that the filled content can be seamlessly connected with the existing sequence. For gaps in the preceding direction, that is, the historical message direction, the descending historical message interface is called to fill the gap forward, with the known message after the end sequence number of the interval as the anchor point. The reason for using the descending interface is that the descending interface returns data from the anchor point to the earlier historical message direction, which just matches the filling requirement of the preceding gap. For gaps in the subsequent direction, that is, the new message direction, the ascending historical message interface / msg / roomPageAscMsgId is called to fill the gap backward, with the known message before the start sequence number of the interval as the anchor point. The ascending interface returns data from the anchor point to the updated message direction, matching the filling requirement of the subsequent gap.
[0039] A three-tiered patching strategy is employed to address gaps of varying sizes, balancing repair efficiency with system load: For single missing entries, patching is performed directly, minimizing resource consumption and maximizing repair speed; for missing entries within a preset batch patching threshold (default 100), a full patch is performed, repairing the gap with a single request for maximum efficiency; for missing entries exceeding the batch patching threshold, patching is performed page-by-page at a fixed page size (default 100 entries per page). For previous gaps, the unique identifier of the earliest message returned from the previous page serves as the request anchor for the next page; for subsequent gaps, the unique identifier of the latest message returned from the previous page serves as the request anchor for the next page. The process is repeated until all gaps are repaired, avoiding API timeouts or UI lag caused by excessive data volume in a single request. If a network error causes a request failure during the patching process, the client employs an exponential backoff retry strategy: a 1-second interval for the first retry, a 2-second interval for the second, a 4-second interval for the third, and a maximum of 3 retries. This avoids frequent retries that exacerbate network congestion while ensuring the gaps are eventually repaired. After all patched content is written to the local message table, the corresponding fields in the local channel table are simultaneously refreshed, and the current message list is automatically refreshed, providing a seamless browsing experience for the user. Once all gaps are repaired, an asynchronous preheating task is triggered within the current context, corresponding to the attached... Figure 4 The bidirectional extended preheating stage in the process.
[0040] Lightweight dynamic cache scheduling management: It adopts a lightweight dynamic cache management mode, which only retains frequently accessed content, and combines background asynchronous preheating to balance smoothness and storage usage.
[0041] The asynchronous preheating task runs in an independent sub-thread, without blocking the user's current interaction. It is triggered in three scenarios: after full-channel synchronization, after offline synchronization, and after anchor point loading gap repair. The preheating starts from the two boundaries of the current loading interval and expands the cache continuity detection in two directions forward and backward, with a default expansion of 20 entries in each direction. If gaps exist, the hierarchical replenishment rules are reused in advance to complete the replenishment. The content that the user may browse later is pre-stored locally. This process does not block the user from viewing the current message; it only caches the part that the user may continue to scroll up or down locally in advance, making the user experience smoother when they continue to scroll. When the user scrolls up and down to browse messages, data is read from the local message table first. A server request is only initiated when there is a gap in the sequence of the target browsing direction, which greatly improves the browsing smoothness in weak network environments and reduces the redundant overhead of repeated requests.
[0042] The local cache adopts a lightweight management mode. The local database is more inclined to be a data reserve of recent messages, rather than forcibly requiring the storage of complete message history. Only message data in four high-frequency access ranges are retained: recent messages of joined channels, historical content of recently accessed sessions, unread messages of unread channels, and the range of messages around which the user has navigated. Historical messages that have never been accessed by the user are not pre-fetched or stored locally, thus avoiding the database capacity from expanding indefinitely over time.
[0043] When logging in from multiple terminals or switching work areas, the client directly calibrates the unread status and session list information of the local channel table through roaming summaries. It can achieve consistency of status across multiple terminals without fetching the full amount of messages. The synchronization speed is fast and the traffic consumption is low, ensuring that the session list and unread badge display are consistent across different devices. For multi-work area scenarios, all message queries and writes carry the work area ownership identifier for conditional filtering, strictly achieving data isolation between different work areas, avoiding cross-work area data interference, and adapting to the usage needs of enterprises with multiple organizational structures.
[0044] Finally, it should be noted that the above embodiments are merely examples for clearly illustrating the present invention and are not intended to limit the implementation. Those skilled in the art will recognize that other variations or modifications can be made based on the above description. It is neither necessary nor possible to exhaustively list all possible implementations. However, obvious variations or modifications derived therefrom are still within the scope of protection of this invention.
Claims
1. A method for dynamic caching of real-time communication based on roaming data and a local database, characterized in that, This method, applied to instant messaging clients, involves the server storing all roaming messages and only sending roaming summary data to the client. The steps are as follows: Obtain roaming summary data issued by the server side. The roaming summary data includes the channel identifier of each message channel, the latest message summary, the incremental sequence number of messages in the channel assigned by the server side, the number of unread messages, and the channel joining status identifier. Read the maximum incremental sequence number of the message in the corresponding channel that has been cached in the local database, calculate the sequence difference with the latest incremental sequence number of the message in the channel on the server side, and then evaluate the local cache coverage status. A differentiated incremental retrieval strategy is generated based on the sequence difference, and the message data is written to the local database in batches after the incremental message synchronization is performed; In response to the anchor point location request of the target message, read the target message and the corresponding range of context messages from the local database, and verify the continuity of the message sequence within the loading range; Identify message gaps within the loading interval, match hierarchical replenishment rules according to the number of messages contained in the gap, fill in the missing messages, and write them to the local database. Once the target context is loaded, a background asynchronous warm-up task is started to dynamically expand the cache coverage around the target message and store message data in the high-frequency access range of the user into the local database.
2. The instant messaging dynamic caching method based on roaming data and a local database according to claim 1, characterized in that, The local database uses a two-level table structure to store data, including a local message table and a local channel table; The local message table stores the complete content and attributes of a single message. Fields include channel ownership identifier, server-side message unique identifier, client-side local message identifier, message increment sequence number within the channel, message timestamp, message status, sending status, and work area ownership identifier. The local channel table stores the aggregated data required for displaying the session list. Fields include the last message content, last message time, last message type, number of unread messages, mention status, and channel joining identifier. The local message table establishes a joint index for the channel ownership identifier and the incremental sequence number of messages within the channel, and a unique index for the unique identifier of the service-side messages.
3. The instant messaging dynamic caching method based on roaming data and a local database according to claim 2, characterized in that, When a client enters or switches a workspace, it first retrieves all the basic channel data of the current workspace and writes it to the local channel table; then it prioritizes extracting the latest message sequence number in the latest channel from the latest message summary of the joined channel. If the summary has no valid sequence number, it extracts it from the channel attribute field. Query the maximum channel-incrementing sequence number of valid messages in the local message table, calculate the sequence difference, and take the smaller value between the sequence difference and the preset single-channel preheating quantity threshold as the actual retrieval scale. If a valid message sequence number within the service-side channel is not obtained, the service-side interface returns an error, or the number of recently cached messages locally is insufficient to meet the preset single-channel preheating threshold, the latest messages from the channel will be retrieved according to the preset single-channel preheating threshold.
4. The instant messaging dynamic caching method based on roaming data and local database according to claim 3, characterized in that, The retrieved messages are uniformly converted into the storage structure of a local message table and then stored in batches. Duplicate messages are replaced and written using the unique index corresponding to the unique identifier of the message on the service side. When the client initiates message sending, it first generates a local message identifier and writes the sending status into the local message table. After receiving the confirmation receipt from the server, it uses the local message identifier as the matching basis to write back the server's unique message identifier, the message increment sequence number in the corresponding channel, and the sending success status to the same local record. After a message is written, the fields related to the last message for the corresponding channel in the local channel table are refreshed synchronously.
5. The instant messaging dynamic caching method based on roaming data and a local database according to claim 2, characterized in that, When the client resumes network access after going offline, it retrieves the offline roaming data directory from the server in pages. If there is no more offline channel data, the pagination stops and all offline channel data is aggregated. Iterate through the latest message summary and the incremental message sequence number in each offline channel, compare them one by one with the maximum incremental message sequence number in the corresponding channel in the local message table, and calculate the sequence difference as the message gap. First, the incremental messages within the message gap are retrieved and stored. After the message sequence is completed, the number of unread messages, the last message time, the last message content, the last message type and the mention status of the local channel table are calibrated based on the roaming summary data, and the channel statistics are written back asynchronously.
6. The instant messaging dynamic caching method based on roaming data and local database according to claim 2, characterized in that, Upon receiving an anchor point location request carrying a channel identifier and a target message identifier, the target message is first retrieved from the local message table. The trigger scenarios for the anchor point location request include search result redirection, notification click, mention message redirection, viewing of referenced messages, and channel historical message location. When the target message exists in the local message table, read the message increment sequence number in the corresponding channel, and query the preceding context message with a sequence number less than the target value and the subsequent context message with a sequence number greater than the target value respectively. The query scale before and after the target message is matched with the preset context loading threshold. After all messages are sorted in ascending order by their channel serial number, the target message's index is used as the anchor point to return to the interface display layer, simultaneously triggering a message sequence continuity check within the loading interval.
7. The instant messaging dynamic caching method based on roaming data and a local database according to claim 6, characterized in that, If the target message is not stored in the local message table, first call the single message query interface on the service side to obtain the target message and write it into the local message table, and then pull the context messages of the corresponding scale before and after the target message and put them into the database. After all messages are written, a context list is generated by sorting the messages in ascending order of their sequence number within the channel. The target message's index is used as the anchor point to return to the interface display layer, and the message sequence continuity check of the loading interval is performed synchronously.
8. The instant messaging dynamic caching method based on roaming data and a local database according to claim 1, characterized in that, When verifying the continuity of the message sequence in the loading interval, first generate the expected continuous sequence number set within the range of message increment sequence numbers in the starting and ending channels of the loading interval, and perform a difference operation with the actual local set of message increment sequence numbers in the channel to obtain all missing sequence numbers. Consecutive adjacent missing sequence numbers are merged into independent message gap intervals. Multiple short message gap intervals with an interval distance less than the preset interval merging threshold are merged into the same interval to be filled.
9. The instant messaging dynamic caching method based on roaming data and a local database according to claim 8, characterized in that, When performing message gap filling, the anchor point is matched and pulled according to the gap direction. The message gap interval in the preceding direction takes the message after the end sequence number of the interval as the anchor point and calls the descending historical query interface to pull the message. The message gap in the subsequent direction is filled by calling the ascending historical query interface with the previous message as the anchor point of the interval's starting sequence number. When a single message gap contains only one missing message, a single message is retrieved. When the number of missing messages does not exceed the preset batch retrieval threshold, a full retrieval is performed. When the number of missing messages exceeds the preset batch retrieval threshold, retrieval is performed page by page according to a fixed page size. The unique identifier of the service-side message of the last message returned on the previous page is used as the request anchor point for the next page. All messages retrieved are written to the local message table.
10. The instant messaging dynamic caching method based on roaming data and local database according to claim 2, characterized in that, After the target context completes the interface display, a background asynchronous warm-up task is started without blocking user interaction. Starting from the two ends of the current loading interval, the cache continuity is checked in both forward and backward directions within a preset range, and message gaps are filled in if any exist. When a user scrolls through messages, the local message table is read first, and a server request is only initiated when there is a message gap in the target browsing direction. The local database only retains message data from joined channels, recent conversations, unread channels, and surrounding areas linked by anchor points. Historical messages that have not been accessed by users are not pre-fetched or stored locally. When logging in from multiple terminals or switching work areas, the unread status of the local channel table and the display status of the channel list are aligned with the server-side data based on the roaming summary data.
