A data caching method and device based on local files in a WeChat applet
By creating a local JSON format database and index file within the WeChat Mini Program, the problem of limited data capacity in WeChat Mini Programs is solved, enabling large-capacity local caching and efficient data management, thereby improving page response speed and ease of operation.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- WUHAN HONGXIN TECH SERVICE CO LTD
- Filing Date
- 2023-09-15
- Publication Date
- 2026-07-03
AI Technical Summary
The existing storage caching method in WeChat Mini Programs has limited data capacity and cannot meet the needs of large-capacity data caching. Furthermore, cloud databases rely on network downloads and are not suitable for local caching.
It adopts a file management interface based on WeChat Mini Program to create local JSON format database files and index files, manages data through key-value pairs, realizes data addition, deletion, modification and query, and cleans up when the data exceeds the capacity. It also uses the file management FD interface for efficient local read and write.
It implements a lightweight JSON file database, supports large-capacity local data caching, improves page response speed, reduces network requests, enhances ease of operation, and improves performance.
Smart Images

Figure CN117171117B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of computer technology, and in particular to a method and apparatus for caching data based on local files in WeChat mini programs. Background Technology
[0002] WeChat Mini Programs are lightweight applications based on the WeChat platform. They can be used without downloading or installing, offering a convenient and easy-to-use experience. With the development of the internet, the application scenarios for Mini Programs are increasing, the number of Mini Programs being released is also growing, and their functions are becoming more complex. Data processing and user experience in Mini Programs have also received attention.
[0003] Generally, data is obtained in real-time through API requests. In some scenarios, to improve page rendering speed, we need to cache data in advance, such as image caching. If the image is static, we download it once, cache it, and then retrieve it from the cache. This makes image loading faster, saves bandwidth, and improves user experience. WeChat Mini Programs provide storage-related methods, which we can use for data caching. WeChat Mini Programs do not support methods for accessing the phone's local SQLite database. However, for cloud development, WeChat Mini Programs provide methods for accessing cloud databases.
[0004] Generally, simple data can be stored using storage, but the storage limit for a single WeChat user and a single mini-program is 10MB, and the maximum data length allowed for a single key is 1MB, so its use is also limited. Cloud databases are suitable for cloud development, and data stored in cloud databases still needs to be downloaded from the network for use, making them unsuitable for local caching. Summary of the Invention
[0005] This invention provides a method and apparatus for caching data based on local files in WeChat Mini Programs, which solves the problem of limited cached data capacity in the storage caching method provided by WeChat Mini Programs in the prior art.
[0006] In a first aspect, the present invention provides a data caching method based on local files in a WeChat Mini Program, comprising: creating a local JSON format database file based on the file management interface of the WeChat Mini Program; defining key values corresponding to the data to be cached in the form of key-value pairs to initialize the database file, and creating a JSON format database index file; wherein the database index file is used to record the data length and position information of the data corresponding to the key value in the database file; reading the corresponding first target data from the database file according to the target key value, and performing processing operations on the first target data to generate second target data; and storing the second target data in the database file.
[0007] According to the data caching method based on local files in WeChat Mini Programs provided by the present invention, after initializing the database file, the method further includes: creating a JSON-formatted database index file according to the key value corresponding to the data to be cached; wherein, the database index file records the data length of the data corresponding to the key value and the position information in the database file.
[0008] According to the data caching method based on local files in WeChat mini programs provided by the present invention, after saving the second target data to the database file, the method further includes: updating the database index file according to the data length and location information of the second target data.
[0009] According to the data caching method based on local files in WeChat Mini Programs provided by the present invention, the step of reading the corresponding first target data from the database file according to the target key value and processing the first target data to generate the second target data includes: after reading the first target data, the first target data is a JSON object; processing the first target data according to the processing method of the JSON object; the processing operation includes: adding, deleting, sorting, filtering and deleting data.
[0010] The data caching method based on local files in WeChat mini programs provided by the present invention further includes: extracting the location information of data corresponding to any key value according to a database index file; and reading the data corresponding to any key value according to the location information.
[0011] According to the data caching method based on local files in WeChat Mini Programs provided by the present invention, before storing the second target data in a database file, the method further includes: determining the data size of the second target data; if the data size of the second target data is greater than the remaining storage capacity of the database file, deleting a portion of the data of the second target data to generate third target data, such that the data size of the third target data is less than or equal to the remaining storage capacity of the database file; or, if the data size of the second target data is greater than the remaining storage capacity of the database file, deleting a portion of the cached data in the database file, such that the remaining capacity of the database file is greater than or equal to the data size of the second target data.
[0012] According to the data caching method based on local files in WeChat Mini Programs provided by the present invention, when the remaining capacity of the database file is less than a preset capacity threshold, the cached data in the database file is cleaned up; the file management interface is a file management FD interface.
[0013] Secondly, the present invention also provides a data caching device based on local files in a WeChat mini-program, comprising:
[0014] The first processing module is used to create a local JSON database file based on the WeChat Mini Program's file management interface;
[0015] The second processing module is used to define the key value corresponding to the data to be cached in the form of key-value pairs, so as to initialize the database file and create a database index file in JSON format; wherein, the database index file is used to record the data length of the data corresponding to the key value and the position information in the database file;
[0016] The third processing module is used to read the corresponding first target data from the database file according to the target key value, and to process the first target data to generate the second target data;
[0017] The fourth processing module is used to store the second target data into the database file.
[0018] Thirdly, the present invention provides an electronic device, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the program to implement the steps of the data caching method based on local files in any of the above-described WeChat mini-programs.
[0019] Fourthly, the present invention also provides a non-transitory computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the steps of the data caching method based on local files in the WeChat mini-program as described above.
[0020] The data caching method and apparatus based on local files in WeChat mini programs provided by this invention have the following advantages compared with the prior art:
[0021] This invention provides a method and apparatus for data caching based on local files in WeChat mini programs, which implements a lightweight JSON file database to meet basic data addition, deletion, modification and query functions, cache large amounts of local data, improve page response speed and reduce some network requests.
[0022] This invention provides a data caching method and apparatus based on local files in WeChat mini programs. The data exists in the form of files in the local user file directory. The file size in this directory can be set by the user (e.g., the maximum capacity is set to 100MB). Compared with the maximum capacity of 10MB in storage, this provides a significant improvement in the amount of data that can be stored.
[0023] This invention provides a method and apparatus for caching data based on local files in WeChat Mini Programs. The method reads file data into JSON objects, which can be directly manipulated in JavaScript. This allows for the use of native methods to add, delete, modify, and query data, thus increasing the convenience of operation.
[0024] This invention provides a data caching method and apparatus based on local files in WeChat Mini Programs. Based on the more efficient and atomic file management FD interface of WeChat Mini Programs, it performs local reading and writing according to the data index file, which improves the performance to a certain extent. It also detects the data capacity and cleans up part of the data if it exceeds the preset capacity. Attached Figure Description
[0025] To more clearly illustrate the technical solutions in this invention or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are some embodiments of this invention. For those skilled in the art, other drawings can be obtained from these drawings without creative effort.
[0026] Figure 1 This is a flowchart illustrating the data caching method based on local files in WeChat Mini Programs provided by this invention.
[0027] Figure 2 This is a schematic diagram of the structure of the data caching device based on local files in WeChat Mini Programs provided by the present invention;
[0028] Figure 3 This is a schematic diagram of the structure of the electronic device provided by the present invention. Detailed Implementation
[0029] To make the objectives, technical solutions, and advantages of this invention clearer, the technical solutions of this invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some, not all, of the embodiments of this invention. All other embodiments obtained by those skilled in the art based on the embodiments of this invention without creative effort are within the scope of protection of this invention.
[0030] It should be noted that, in the description of the embodiments of the present invention, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Without further limitations, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes said element. Those skilled in the art can understand the specific meaning of the above terms in the present invention according to the specific circumstances.
[0031] The terms "first," "second," etc., used in this application are used to distinguish similar objects and not to describe a specific order or sequence. It should be understood that such data can be interchanged where appropriate so that embodiments of this application can be implemented in orders other than those illustrated or described herein, and the objects distinguished by "first," "second," etc., are generally of the same class, without limiting the number of objects; for example, a first object can be one or more. Furthermore, "and / or" indicates at least one of the connected objects, and the character " / " generally indicates that the preceding and following objects have an "or" relationship.
[0032] The following is combined with Figures 1-3 This invention describes a method and apparatus for caching data based on local files in WeChat mini programs, as provided in embodiments of the present invention.
[0033] Figure 1 This is a flowchart illustrating the data caching method based on local files in WeChat Mini Programs provided by this invention, as shown below. Figure 1 As shown, including but not limited to the following steps:
[0034] Step 101: Create a local JSON database file based on the WeChat Mini Program's file management interface.
[0035] This invention uses the file management interface of WeChat Mini Program to create a local JSON file as a database file.
[0036] Specifically, in step 101, the WeChat Mini Program file management interface is used to create a local JSON file in the local user file directory based on the local user file directory wx.env.USER_DATA_PATH.
[0037] Specifically, in the cache data management utility class of the WeChat Mini Program, an initialization method is defined. In this method, wx.getFileSystemManager() is used to obtain the file manager, the path of the local file is defined as: ${wx.env.USER_DATA_PATH} / db.json, and the openSync method of the file manager is used to open and create it.
[0038] Step 102: Define the key values corresponding to the data to be cached in the form of key-value pairs to initialize the database file and create a database index file in JSON format.
[0039] The database index file is used to record the data length of the data corresponding to the key value and its position information in the database file.
[0040] This invention defines corresponding key values in the form of key-value pairs for the data to be cached as needed, and initializes a local JSON-formatted database file based on the key values.
[0041] Specifically, in the initialization method of the cached data management utility class in the WeChat Mini Program, the key corresponding to the data to be cached is defined. During initialization, the key value is an empty object or an empty array, and these data are combined into an initialized JSON object. The JSON.stringify method is called on this object to convert it into a string, and then the file manager's writeSync method can be called to write the initialized JSON object to a file for storage.
[0042] Based on the initialized JSON object, generate the length and position information corresponding to the key, and store this information in the data index file in the local user directory, such as: ${wx.env.USER_DATA_PATH} / dbDataIndex.json.
[0043] In WeChat Mini Programs, a cache data management utility class defines a default threshold for file size and records it using variables.
[0044] Step 103: Read the corresponding first target data from the database file according to the target key value, and process the first target data to generate the second target data.
[0045] Optionally, step 103 specifically includes: after reading the first target data, which is a JSON object; performing processing operations on the first target data according to the processing methods of the JSON object; the processing operations include: adding, deleting, sorting, filtering, and deleting data.
[0046] Specifically, in the cached data management utility class of the WeChat Mini Program, a read method is defined. Based on the passed target key value, the method finds the position and length of the first target data in the data index file, reads it using the readSync method of the file manager, converts the read ArrayBuffer data using the decode method of TextDecoder, and restores the converted data to a JSON object using JSON.parse.
[0047] Optionally, based on the methods of objects in JavaScript, properties can be added to or the values of the corresponding properties can be changed on the object data of the JSON object (the first target data) to generate the second target data.
[0048] Optionally, based on the array methods in JavaScript, operations such as adding, deleting, sorting, and filtering are performed on the array data of the JSON object ((the first target data)) to generate the second target data.
[0049] Step 104: Store the second target data in the database file.
[0050] In the cached data management utility class of WeChat Mini Program, a storage method is defined. Based on the length of the data after the change and the location information corresponding to the key, the writeSync function of the file manager is used to write the second target data to the database file.
[0051] Based on the above embodiments, as an optional embodiment, the data caching method based on local files in WeChat mini programs provided by the present invention further includes, after saving the second target data to the database file, updating the database index file according to the data length and location information of the second target data.
[0052] Based on the above embodiments, as an optional embodiment, the data caching method based on local files in WeChat mini programs provided by the present invention further includes: extracting the location information of data corresponding to any key value according to a database index file; and reading the data corresponding to any key value according to the location information.
[0053] Specifically, by reading a piece of data based on a key value, and extracting the location information of the data corresponding to the key value from the JSON-formatted database index file, the content of the local JSON-formatted database file can be read in a partial manner, which avoids reading all the contents of the local file before retrieving the data corresponding to the key value.
[0054] Based on the above embodiments, as an optional embodiment, the data caching method based on local files in WeChat Mini Programs provided by the present invention further includes, before storing the second target data in a database file, determining the data size of the second target data;
[0055] If the size of the second target data exceeds the remaining storage capacity of the database file, a portion of the second target data is deleted to generate third target data, such that the size of the third target data is less than or equal to the remaining storage capacity of the database file; or,
[0056] If the size of the second target data is greater than the remaining storage capacity of the database file, delete some cached data in the database file so that the remaining capacity of the database file is greater than or equal to the size of the second target data.
[0057] In the cached data management utility class of WeChat Mini Program, a file size check method is defined to add a mechanism for checking the remaining capacity of the database file. Before the data storage is completed, the remaining capacity needs to be checked so that adaptive data caching can be performed based on the remaining capacity and the specific size of the data to be cached.
[0058] It is understandable that the technical method provided by this invention improves the problem of data storage capacity in WeChat Mini Programs. Data can be easily added, deleted, modified, and queried through object and array methods. Data index files are used for local reading and storage of data, thereby improving file reading performance.
[0059] Based on the above embodiments, as an optional embodiment, the data caching method based on local files in WeChat mini programs provided by the present invention cleans up the cached data in the database file when the remaining capacity of the database file is less than a preset capacity threshold.
[0060] Optionally, the present invention may clean up (i.e. perform deletion) a portion of the cached data so that the remaining capacity is greater than or equal to a preset capacity threshold.
[0061] Optionally, the data caching method based on local files in the WeChat mini program provided by the present invention uses a file management FD interface.
[0062] The File Management FD (FD) interface is a more efficient and atomic file management interface.
[0063] Figure 2 This is a schematic diagram of the structure of the data caching device based on local files in WeChat Mini Programs provided by the present invention, as shown below. Figure 2 As shown, the device includes:
[0064] The first processing module 201 is used to create a local JSON format database file based on the file management interface of WeChat Mini Program;
[0065] The second processing module 202 is used to define the key value corresponding to the data to be cached in the form of key-value pairs, so as to initialize the database file and create a database index file in JSON format; wherein, the database index file is used to record the data length of the data corresponding to the key value and the position information in the database file;
[0066] The third processing module 203 is used to read the corresponding first target data from the database file according to the target key value, and to process the first target data to generate the second target data;
[0067] The fourth processing module 204 is used to store the second target data into the database file.
[0068] It should be noted that the data caching device based on local files in WeChat mini programs provided in this embodiment of the invention can execute the data caching method based on local files in WeChat mini programs described in any of the above embodiments during actual operation, which will not be elaborated in this embodiment.
[0069] In summary, the data caching method and apparatus based on local files in WeChat mini programs provided by this invention have the following advantages compared with the prior art:
[0070] This invention provides a method and apparatus for data caching based on local files in WeChat mini programs, which implements a lightweight JSON file database to meet basic data addition, deletion, modification and query functions, cache large amounts of local data, improve page response speed and reduce some network requests.
[0071] This invention provides a data caching method and apparatus based on local files in WeChat mini programs. The data exists in the form of files in the local user file directory. The file size in this directory can be set by the user (e.g., the maximum capacity is set to 100MB). Compared with the maximum capacity of 10MB in storage, this provides a significant improvement in the amount of data that can be stored.
[0072] This invention provides a method and apparatus for caching data based on local files in WeChat Mini Programs. The method reads file data into JSON objects, which can be directly manipulated in JavaScript. This allows for the use of native methods to add, delete, modify, and query data, thus increasing the convenience of operation.
[0073] This invention provides a data caching method and apparatus based on local files in WeChat Mini Programs. Based on the more efficient and atomic file management FD interface of WeChat Mini Programs, it performs local reading and writing according to the data index file, which improves the performance to a certain extent. It also detects the data capacity and cleans up part of the data if it exceeds the preset capacity.
[0074] Figure 3 This is a schematic diagram of the structure of the electronic device provided by the present invention, such as... Figure 3 As shown, the electronic device may include a processor 310, a communications interface 320, a memory 330, and a communication bus 340, wherein the processor 310, communications interface 320, and memory 330 communicate with each other via the communication bus 340. The processor 310 can call logical instructions in the memory 330 to execute a data caching method based on local files in a WeChat mini-program. This method includes: creating a local JSON-formatted database file based on the WeChat mini-program's file management interface; defining key values corresponding to the data to be cached in key-value pairs to initialize the database file and creating a JSON-formatted database index file; wherein the database index file is used to record the data length and position information of the data corresponding to the key value in the database file; reading the corresponding first target data from the database file according to the target key value, processing the first target data to generate second target data; and storing the second target data in the database file.
[0075] Furthermore, the logical instructions in the aforementioned memory 330 can be implemented as software functional units and, when sold or used as independent products, can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, or the part that contributes to the prior art, or a part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of the present invention. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.
[0076] On the other hand, the present invention also provides a computer program product, the computer program product including a computer program stored on a non-transitory computer-readable storage medium, the computer program including program instructions, when the program instructions are executed by a computer, the computer is able to execute the data caching method based on local files in the WeChat mini program provided in the above embodiments, the method including: creating a local JSON format database file based on the file management interface of the WeChat mini program; defining the key value corresponding to the data to be cached in the form of key-value pairs to initialize the database file, and creating a JSON format database index file; wherein, the database index file is used to record the data length and position information of the data corresponding to the key value in the database file; reading the corresponding first target data from the database file according to the target key value, and performing processing operations on the first target data to generate second target data; storing the second target data in the database file.
[0077] In another aspect, the present invention also provides a non-transitory computer-readable storage medium storing a computer program thereon. When executed by a processor, the computer program implements a data caching method based on local files in a WeChat mini-program provided in the above embodiments. The method includes: creating a local JSON-formatted database file based on the file management interface of the WeChat mini-program; defining key values corresponding to the data to be cached in the form of key-value pairs to initialize the database file, and creating a JSON-formatted database index file; wherein the database index file is used to record the data length and position information of the data corresponding to the key value in the database file; reading the corresponding first target data from the database file according to the target key value, and performing processing operations on the first target data to generate second target data; and storing the second target data in the database file.
[0078] The device embodiments described above are merely illustrative, and the units described as separate components may or may not be physically separate. Some or all of the modules can be selected to achieve the purpose of this embodiment according to actual needs. Those skilled in the art can understand and implement this without any creative effort.
[0079] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, and not to limit them; although the present invention has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some of the technical features; and these modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of the present invention.
Claims
1. A method for caching data based on local files in a WeChat mini-program, characterized in that, include: Create a local JSON database file based on the WeChat Mini Program's file management interface; Define the key value corresponding to the data to be cached in the form of key-value pairs to initialize the database file and create a database index file in JSON format; wherein, the database index file is used to record the data length of the data corresponding to the key value and the position information in the database file; The location information of the first target data is found in the database index file based on the target key value, and the corresponding first target data is read from the database file. The first target data is then processed to generate the second target data. The first target data is a JSON object. The second target data is stored in the database file; Before storing the second target data in the database file, the process also includes: Determine the data size of the second target data; If the size of the second target data exceeds the remaining storage capacity of the database file, a portion of the second target data is deleted to generate third target data, such that the size of the third target data is less than or equal to the remaining storage capacity of the database file; or, If the size of the second target data is greater than the remaining storage capacity of the database file, delete some cached data in the database file so that the remaining capacity of the database file is greater than or equal to the size of the second target data. After saving the second target data to the database file, the method further includes updating the database index file based on the data length and location information of the second target data.
2. The data caching method based on local files in WeChat Mini Programs according to claim 1, characterized in that, The step of reading the corresponding first target data from the database file according to the target key value, and processing the first target data to generate the second target data includes: After reading the first target data, the first target data is a JSON object; The first target data is processed according to the processing methods of the JSON object; The processing operations include: adding, deleting, sorting, filtering, and deleting data.
3. The data caching method based on local files in WeChat Mini Programs according to claim 1, characterized in that, Also includes: Based on the database index file, extract the location information of the data corresponding to any key value; Based on the location information, the data corresponding to any of the key values is read.
4. The data caching method based on local files in WeChat Mini Programs according to claim 1, characterized in that, Also includes: If the remaining capacity of the database file is less than a preset capacity threshold, the cached data in the database file is cleaned up.
5. The data caching method based on local files in WeChat Mini Programs according to claim 1, characterized in that, The file management interface is the File Management FD interface.
6. A data caching device based on local files in a WeChat mini-program, used to implement the data caching method based on local files in a WeChat mini-program as described in any one of claims 1 to 5, characterized in that, include: The first processing module is used to create a local JSON database file based on the WeChat Mini Program's file management interface; The second processing module is used to define the key value corresponding to the data to be cached in the form of key-value pairs, so as to initialize the database file and create a database index file in JSON format; wherein, the database index file is used to record the data length of the data corresponding to the key value and the position information in the database file; The third processing module is used to read the corresponding first target data from the database file according to the target key value, and to process the first target data to generate the second target data; The fourth processing module is used to store the second target data into the database file.
7. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that, When the processor executes the computer program, it implements the steps of the data caching method based on local files in the WeChat mini program as described in any one of claims 1 to 5.
8. A non-transitory computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by the processor, it implements the steps of the data caching method based on local files in the WeChat Mini Program as described in any one of claims 1 to 5.