Disclosure of Invention
In view of this, the embodiments of the present invention provide a method and a system for updating a client cache, which can manage and update a cache file at a client, without modifying a file name of a resource file, and without setting a short cache time for the resource file, so as to implement timely update of the cache, thereby ensuring that the resource file of a user request page acquired by a browser each time is the latest version.
To achieve the above object, according to one aspect of the embodiments of the present invention, a method for updating a client cache is provided.
A method for updating client side cache includes registering page of unregistered cache management module by browser according to page access request and installing cache management module, searching requested resource file from cache by said cache management module according to said page access request and executing said resource file to send update check request to server, verifying whether resource file has been updated by said server according to said version identification and returning verification result to said cache management module, judging by said cache management module according to said verification result, if said resource file has been updated, requesting up to date resource file to said server by updating said cache management module and updating resource file in said cache.
Optionally, the method further comprises setting a caching strategy of the update verification request to never cache, so that the update verification request is sent every time the resource file is executed.
Optionally, the method further comprises the step of directly requesting the server for obtaining the resource file if the resource file is not found in the cache by the cache management module.
Optionally, the step of updating the cache management module to request the latest resource file from the server comprises the step of triggering the browser to update the cache management module by changing the version identifier of the cache, so that the updated cache management module directly requests the latest resource file from the server.
Optionally, the method further comprises the step of setting a caching strategy of the request in the installation process of the caching management module to be never cached, so that the request in the installation process is directly sent to the server.
Optionally, the cache management module is a Service Worker for a Service Worker.
According to another aspect of an embodiment of the present invention, a system for updating a client cache is provided.
A system for updating client side cache comprises a browser, a cache management module and a server, wherein the browser is used for registering and installing the cache management module for a page of an unregistered cache management module according to a page access request, the cache management module is used for searching a requested resource file from a cache according to the page access request and executing the resource file to send an update verification request to the server, the update verification request comprises a version identification of the resource file, after receiving a verification result returned by the server, judgment is carried out according to the verification result, if the resource file is updated, the latest resource file is requested to the server through updating the cache management module, and the resource file in the cache is updated, and the server is used for verifying whether the resource file is updated according to the version identification and returning the verification result to the cache management module.
Optionally, the update verification request is sent each time the resource file is executed by setting a caching policy of the update verification request to never cache.
Optionally, if the cache management module does not find the resource file from the cache, the cache management module directly requests the server to acquire the resource file.
Optionally, the cache management module is further configured to trigger the browser to update the cache management module by changing the version identifier of the cache, so that the updated cache management module directly requests the latest resource file from the server.
Optionally, the request in the installation process is directly sent to the server by setting the caching strategy of the request in the installation process of the caching management module to be never cached.
Optionally, the cache management module is a Service Worker for a Service Worker.
According to yet another aspect of an embodiment of the present invention, an electronic device for updating a client cache is provided.
An electronic device for updating a client cache comprises one or more processors and a storage device, wherein the storage device is used for storing one or more programs, and when the one or more programs are executed by the one or more processors, the one or more processors are enabled to realize the method for updating the client cache.
According to yet another aspect of an embodiment of the present invention, a computer-readable medium is provided.
A computer readable medium having stored thereon a computer program which when executed by a processor implements a method of updating a client cache provided by an embodiment of the present invention.
The embodiment of the invention has the advantages that the cache management module is introduced to control and update the cache at the client, the version identification is recorded in the resource file to be cached, and when the resource file is executed each time, an update verification request is sent to inform the server of the version identification, the server compares the received version identification with the version identification of the latest resource file, judges whether the update exists and informs the cache management module, if the cache management module receives the update information, the latest resource file is requested to the server through the update cache management module, the management and update of the cache file at the client are realized, the file name of the resource file is not required to be modified, and the cache can be updated in time without setting a short cache time for the resource file, so that the resource file of a user request page acquired by a browser each time is ensured to be the latest version. In addition, the invention can also avoid the problem of untimely resource update caused by caching the Service workbench installation file by carrying out version control on the cache name.
Further effects of the above-described non-conventional alternatives are described below in connection with the embodiments.
Detailed Description
Exemplary embodiments of the present invention will now be described with reference to the accompanying drawings, in which various details of the embodiments of the present invention are included to facilitate understanding, and are to be considered merely exemplary. Accordingly, those of ordinary skill in the art will recognize that various changes and modifications of the embodiments described herein can be made without departing from the scope and spirit of the invention. Also, descriptions of well-known functions and constructions are omitted in the following description for clarity and conciseness.
In order to solve the problems in the prior art, the invention provides a method and a system for updating a cache file at a client, which can realize timely updating of the cache without modifying a file name or setting a short cache time for the file. Since browser cache generally refers to HTTP cache, for distinction, the cache in the browser of the client is referred to as client cache, simply referred to as "cache" in the present invention.
In describing embodiments of the present invention, the terminology that appears is defined as follows:
the static resource file is that a client sends a request to a Web server, the Web server obtains corresponding files from a file system and directly returns the corresponding files to the client, the client analyzes and renders the files to display, and the files are generally called static resources and are usually files generated by dynamic operation of a non-server such as JS (JavaScript for short), CSS (CASCADING STYLE SHEETS ), pictures and the like;
service Worker is a script that the browser runs in the background independently of the web page, and provides the capability that functions that do not need to interact with the web page are silently executed behind the web page. The method is a JavaScript working thread, is a programmable network agent, and a developer can control the processing mode of a network request sent by a page. The Service Worker first has a function of intercepting and processing network requests, including a programmable message cache management capability.
The invention is based on the principle that a cache management module Service Worker is introduced to control and update the cache at a client, a version identifier is recorded in a static resource file to be cached, an update verification request is sent to inform a server of the version identifier when the static resource file is executed each time, the server compares the received version identifier with the version identifier of the latest resource file, whether the update exists or not is judged, and the cache management module Service Worker is informed. If the cache management module Service workbench receives the update information, the update of the browser to the Service workbench is triggered by changing the cache version number of the cache management module Service workbench, and the updated cache management module Service workbench requests the latest resource file. According to the technical scheme of the invention, timely cache updating is realized, and the file name of the resource file is not required to be modified.
In order to realize the technical scheme of the invention, version identification can be added in the static resource files needing to be cached in advance, and when the static resource files are executed, an update verification request is sent to the server to verify whether the static resource files locally cached by the client are the latest version. Where the version identification is, for example, a version number or any data such as a timestamp that can identify the current version of the static resource file. In addition, the caching strategy of each resource can be preset, namely (1) the updating verification request for acquiring the file updating information is set to be directly sent to the server without caching, and (2) the static resource file is set to be cache priority, and if the result is not found in the cache, the static resource file is sent to the server again.
According to the technical scheme of the invention, the cache management module is, for example, a Service Worker of a Service work thread. When a user requests access to a page, the browser registers Service workbench for the page by calling a navigator. Serviceworkbench. Register () method. Specifically, the specific location of the browser Service workbench script can be notified by executing the code of the registered Service workbench deployed in the page.
After registering the Service workbench for the page, the browser finds the Service workbench script file from the specific position where the Service workbench script is placed, loads it into the browser and executes it to install the Service workbench. In the install callback event handling function, the Service Worker may request a resource (e.g., JS file, etc.) that needs to be cached, open the cache, set the cache name, set the cache policy, etc. When the cache name is set, the current version identifier (such as version number, timestamp, etc.) of the cache can be used as the cache name and stored by using a variable version. When the cache is changed, the cached change can be identified by modifying the version identification of the cache, so that the browser can timely find that the Service workbench is modified, update the Service workbench and reinstall the Service workbench. By carrying out version control on the cache name, the problem of untimely resource update caused by cached Service workbench installation files can be avoided.
Meanwhile, in order to avoid that static resource files of other page access requests sent by a user in the process of installing the Service workbench are loaded from a browser cache (namely an HTTP cache), a caching policy of the requests in the process of installing the Service workbench can be set to never cache, for example, a request parameter { cache: 'no-cache' } can be set to ensure that the requests must be verified to a server. For example, by setting new Request ('/script. Js', { cache: 'no-cache' }) so that the acquisition Request of the static resource file "/script. Js" is directly verified to the server.
Finally, after the Service workbench is installed, intercepting the network request through a fetch event of the Service workbench, and responding to each intercepted request according to a preset response rule of the request. The preset response rule for the request is that if the request is a page access request, the requested resource file is preferentially searched from the cache according to the page access request, if the request is the version identification of the resource file returned by the server, the request is judged according to the version identification, if the resource file is not updated (the version identification is the same), the cached resource file is sent to the browser, and if the resource file is updated (the version identification is different), the Service Worker is updated by modifying the cache name (or the version identification of the cached or the version identification of the resource file in the cached resource list) so as to request the server to acquire the latest resource file.
Fig. 1 is a schematic diagram of main steps of a method for updating a client cache according to an embodiment of the present invention. As shown in fig. 1, the method for updating a client cache according to the embodiment of the present invention mainly includes steps S101 to S104 as follows.
Step S101, a browser registers and installs a cache management module for a page of an unregistered cache management module according to a page access request;
Step S102, a cache management module searches a requested resource file from a cache according to a page access request, and executes the resource file to send an update checking request to a server, wherein the update checking request comprises a version identifier of the resource file;
step S103, the server verifies whether the resource file is updated according to the version identification, and returns a verification result to the cache management module;
step S104, the cache management module judges according to the verification result, if the resource file is updated, the cache management module is updated to request the latest resource file from the server, and the resource file in the cache is updated.
According to the embodiment of the invention, the cache policy of the update verification request is set to never cache, so that the update verification request is sent every time the resource file is executed. Therefore, the static resource file of the user request page acquired by the browser can be guaranteed to be the latest version each time.
According to one embodiment of the invention, if the cache management module does not find the resource file from the cache, the resource file is directly requested to be obtained from the server. If the resource file is not found in the cache, it means that the file is not cached or the cache is expired, so that the latest file needs to be requested from the server, so that the file contained in the page displayed by the browser is the latest version of the file each time.
According to another embodiment of the invention, the method for requesting the latest resource file from the server by updating the cache management module can comprise the step of triggering the browser to update the cache management module by changing the version identification of the cache, so that the updated cache management module directly requests the latest resource file from the server. By carrying out version control on the cache name, the problem of untimely resource update caused by cached Service workbench installation files can be avoided.
According to one embodiment of the invention, the request in the installation process is directly sent to the server by setting the caching strategy of the request in the installation process to never cache. Therefore, the problem that the page file displayed to the user is not necessarily the latest version caused by loading the resource file from the browser cache (namely HTTP cache) when the user sends other page access requests in the installation process of the cache management module can be avoided, and the accuracy of the page file displayed to the user whenever the user is ensured.
In the embodiment of the invention, the cache management module is a Service Worker of a Service work thread.
Fig. 2 is a schematic diagram of the main components of a system for updating a client cache according to an embodiment of the present invention. As shown in fig. 2, a system 200 for updating client caches according to an embodiment of the present invention mainly includes a browser 201, a cache management module 202, and a server 203. Wherein, the browser 201 and the cache management module 202 are located at a client, and the server 203 is located at a server.
The browser 201 is configured to register and install a cache management module for a page of the unregistered cache management module according to a page access request;
The cache management module 202 is configured to search a requested resource file from a cache according to a page access request, execute the resource file to send an update verification request to a server, where the update verification request includes a version identifier of the resource file, and determine according to a verification result returned by the server after receiving the verification result, and if the resource file is updated, request an updated resource file from the server by updating the cache management module, and update the resource file in the cache;
the server 203 is configured to verify whether the resource file is updated according to the version identifier, and return a verification result to the cache management module.
According to one embodiment of the invention, the update verification request is sent each time the resource file is executed by setting the cache policy of the update verification request to never cache.
According to another embodiment of the present invention, if the cache management module does not find the resource file from the cache, it directly requests the server to acquire the resource file.
According to yet another embodiment of the present invention, the cache management module 202 may also be configured to:
and triggering the browser to update the cache management module by changing the version identifier of the cache, so that the updated cache management module directly requests the latest resource file from the server.
According to another embodiment of the invention, the request in the installation process is directly sent to the server by setting the caching strategy of the request in the installation process to never cache.
In the embodiment of the invention, the cache management module is a Service Worker for a Service work thread.
Fig. 3 is a schematic flow chart of an implementation of a client cache update method according to an embodiment of the present invention. As shown in fig. 3, the method for updating the client cache according to the embodiment of the present invention mainly includes the following implementation steps:
step S1, a user requests to open a page in a browser;
and S2, the browser judges whether the page is registered with the Service workbench. If so, executing the step S6, otherwise executing the step S3;
Step S3, the browser registers the Service workbench for the page, and can obtain the storage position of the Service workbench script by executing the code of registering the Service workbench in the page;
s4, downloading and executing a Service workbench script file by the browser;
S5, installing a Service workbench, and requesting and caching a required static resource file from a server through installing a callback event processing function;
and S6, judging whether the static resource file requested by the user exists in the cache after the Service workbench is installed. If yes, executing the step S8, otherwise, executing the step S7;
Step S7, requesting the latest static resource file from the server, and then executing step S8;
s8, the Service workbench executes the corresponding static resource file and sends an update verification request;
s9, intercepting a verification result returned by the server by a fetch method of a Service workbench;
and step S10, the Service workbench judges whether the static resource file is updated according to the verification result. If not, executing step S11, otherwise executing step S12;
Step S11, returning the static resource file cached in the installation stage of the Service workbench to the browser;
and S12, changing the buffer version number of the Service workbench to trigger the browser to execute the step S5.
According to the technical scheme of the embodiment of the invention, the cache management module is introduced to control and update the cache at the client, the version identification is recorded in the resource file to be cached, and when the resource file is executed each time, an update verification request is sent to inform the server of the version identification, the server compares the received version identification with the version identification of the latest resource file, judges whether the update exists and informs the cache management module, if the cache management module receives the update information, the latest resource file is requested to the server through the update cache management module, the management and update of the cache file at the client are realized, the file name of the resource file is not required to be modified, and the cache can be updated in time without setting a short cache time for the resource file, so that the resource file of a user request page acquired by a browser each time is ensured to be the latest version. In addition, the invention can also avoid the problem of untimely resource update caused by caching the Service workbench installation file by carrying out version control on the cache name.
Fig. 4 illustrates an exemplary system architecture 400 of a method of updating a client cache or a system of updating a client cache to which embodiments of the present invention may be applied.
As shown in fig. 4, the system architecture 400 may include terminal devices 401, 402, 403, a network 404, and a server 405. The network 404 is used as a medium to provide communication links between the terminal devices 401, 402, 403 and the server 405. The network 404 may include various connection types, such as wired, wireless communication links, or fiber optic cables, among others.
A user may interact with the server 405 via the network 404 using the terminal devices 401, 402, 403 to receive or send messages or the like. Various communication client applications, such as shopping class applications, web browser applications, search class applications, instant messaging tools, mailbox clients, social platform software, etc. (by way of example only) may be installed on the terminal devices 401, 402, 403.
The terminal devices 401, 402, 403 may be various electronic devices having a display screen and supporting web browsing, including but not limited to smartphones, tablets, laptop and desktop computers, and the like.
The server 405 may be a server providing various services, such as a background management server (by way of example only) providing support for shopping-type websites browsed by users using the terminal devices 401, 402, 403. The background management server may analyze and process the received data such as the product information query request, and feedback the processing result (e.g., the target push information, the product information—only an example) to the terminal device.
It should be noted that, the method for updating the client cache provided in the embodiment of the present invention is generally executed by the server 405, and accordingly, the system for updating the client cache is generally set in the server 405.
It should be understood that the number of terminal devices, networks and servers in fig. 4 is merely illustrative. There may be any number of terminal devices, networks, and servers, as desired for implementation.
Referring now to FIG. 5, there is illustrated a schematic diagram of a computer system 500 suitable for use in implementing a terminal device or server in accordance with an embodiment of the present invention. The terminal device or server shown in fig. 5 is only an example, and should not impose any limitation on the functions and scope of use of the embodiments of the present invention.
As shown in fig. 5, the computer system 500 includes a Central Processing Unit (CPU) 501, which can perform various appropriate actions and processes according to a program stored in a Read Only Memory (ROM) 502 or a program loaded from a storage section 508 into a Random Access Memory (RAM) 503. In the RAM 503, various programs and data required for the operation of the system 500 are also stored. The CPU 501, ROM 502, and RAM 503 are connected to each other through a bus 504. An input/output (I/O) interface 505 is also connected to bus 504.
Connected to the I/O interface 505 are an input section 506 including a keyboard, a mouse, and the like, an output section 507 including a Cathode Ray Tube (CRT), a Liquid Crystal Display (LCD), and the like, a speaker, and the like, a storage section 508 including a hard disk, and the like, and a communication section 509 including a network interface card such as a LAN card, a modem, and the like. The communication section 509 performs communication processing via a network such as the internet. The drive 510 is also connected to the I/O interface 505 as needed. A removable medium 511 such as a magnetic disk, an optical disk, a magneto-optical disk, a semiconductor memory, or the like is mounted on the drive 510 as needed so that a computer program read therefrom is mounted into the storage section 508 as needed.
In particular, according to embodiments of the present disclosure, the processes described above with reference to flowcharts may be implemented as computer software programs. For example, embodiments of the present disclosure include a computer program product comprising a computer program embodied on a computer readable medium, the computer program comprising program code for performing the method shown in the flow chart. In such an embodiment, the computer program may be downloaded and installed from a network via the communication portion 509, and/or installed from the removable media 511. The above-described functions defined in the system of the present invention are performed when the computer program is executed by a Central Processing Unit (CPU) 501.
The computer readable medium shown in the present invention may be a computer readable signal medium or a computer readable storage medium, or any combination of the two. The computer readable storage medium can be, for example, but not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or a combination of any of the foregoing. More specific examples of a computer-readable storage medium may include, but are not limited to, an electrical connection having one or more wires, a portable computer diskette, a hard disk, a Random Access Memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disc read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing. In the context of this document, a computer readable storage medium may be any tangible medium that can contain, or store a program for use by or in connection with an instruction execution system, apparatus, or device. In the present invention, however, the computer-readable signal medium may include a data signal propagated in baseband or as part of a carrier wave, with the computer-readable program code embodied therein. Such a propagated data signal may take any of a variety of forms, including, but not limited to, electro-magnetic, optical, or any suitable combination of the foregoing. A computer readable signal medium may also be any computer readable medium that is not a computer readable storage medium and that can communicate, propagate, or transport a program for use by or in connection with an instruction execution system, apparatus, or device. Program code embodied on a computer readable medium may be transmitted using any appropriate medium, including but not limited to wireless, wireline, optical fiber cable, RF, etc., or any suitable combination of the foregoing.
The flowcharts and block diagrams in the figures illustrate the architecture, functionality, and operation of possible implementations of systems, methods and computer program products according to various embodiments of the present invention. In this regard, each block in the flowchart or block diagrams may represent a module, segment, or portion of code, which comprises one or more executable instructions for implementing the specified logical function(s). It should also be noted that, in some alternative implementations, the functions noted in the block may occur out of the order noted in the figures. For example, two blocks shown in succession may, in fact, be executed substantially concurrently, or the blocks may sometimes be executed in the reverse order, depending upon the functionality involved. It will also be noted that each block of the block diagrams or flowchart illustration, and combinations of blocks in the block diagrams or flowchart illustration, can be implemented by special purpose hardware-based systems which perform the specified functions or acts, or combinations of special purpose hardware and computer instructions.
The units or modules involved in the embodiments of the present invention may be implemented in software or in hardware.
As a further aspect, the invention also provides a computer readable medium which may be comprised in the device described in the above embodiments or may be present alone without being fitted into the device. The computer readable medium carries one or more programs, when the one or more programs are executed by the device, the device comprises a browser, a cache management module, a server and a cache management module, wherein the browser registers and installs the cache management module for a page of an unregistered cache management module according to a page access request, the cache management module searches a requested resource file from a cache according to the page access request and executes the resource file to send an update verification request to the server, the update verification request comprises a version identifier of the resource file, the server verifies whether the resource file is updated according to the version identifier and returns a verification result to the cache management module, the cache management module judges according to the verification result, and if the resource file is updated, the cache management module is updated to request the latest resource file from the server and update the resource file in the cache.
According to the technical scheme of the embodiment of the invention, the cache management module is introduced to control and update the cache at the client, the version identification is recorded in the resource file to be cached, and when the resource file is executed each time, an update verification request is sent to inform the server of the version identification, the server compares the received version identification with the version identification of the latest resource file, judges whether the update exists and informs the cache management module, if the cache management module receives the update information, the latest resource file is requested to the server through the update cache management module, the management and update of the cache file at the client are realized, the file name of the resource file is not required to be modified, and the cache can be updated in time without setting a short cache time for the resource file, so that the resource file of a user request page acquired by a browser each time is ensured to be the latest version. In addition, the invention can also avoid the problem of untimely resource update caused by caching the Service workbench installation file by carrying out version control on the cache name.
The above embodiments do not limit the scope of the present invention. It will be apparent to those skilled in the art that various modifications, combinations, sub-combinations and alternatives can occur depending upon design requirements and other factors. Any modifications, equivalent substitutions and improvements made within the spirit and principles of the present invention should be included in the scope of the present invention.