CN116521745A - Caching method and device - Google Patents

Caching method and device Download PDF

Info

Publication number
CN116521745A
CN116521745A CN202310807434.1A CN202310807434A CN116521745A CN 116521745 A CN116521745 A CN 116521745A CN 202310807434 A CN202310807434 A CN 202310807434A CN 116521745 A CN116521745 A CN 116521745A
Authority
CN
China
Prior art keywords
request
graphql
client
key value
function
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN202310807434.1A
Other languages
Chinese (zh)
Inventor
余尚坤
朱文雷
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Beijing Chaitin Tech Co ltd
Original Assignee
Beijing Chaitin Tech Co ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Beijing Chaitin Tech Co ltd filed Critical Beijing Chaitin Tech Co ltd
Priority to CN202310807434.1A priority Critical patent/CN116521745A/en
Publication of CN116521745A publication Critical patent/CN116521745A/en
Pending legal-status Critical Current

Links

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor
    • G06F16/20Information retrieval; Database structures therefor; File system structures therefor of structured data, e.g. relational data
    • G06F16/24Querying
    • G06F16/245Query processing
    • G06F16/2455Query execution
    • G06F16/24552Database cache management
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor
    • G06F16/90Details of database functions independent of the retrieved data types
    • G06F16/95Retrieval from the web
    • G06F16/957Browsing optimisation, e.g. caching or content distillation
    • G06F16/9574Browsing optimisation, e.g. caching or content distillation of access to content, e.g. by caching
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor
    • G06F16/90Details of database functions independent of the retrieved data types
    • G06F16/95Retrieval from the web
    • G06F16/958Organisation or management of web site content, e.g. publishing, maintaining pages or automatic linking
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/54Interprogram communication
    • G06F9/547Remote procedure calls [RPC]; Web services
    • YGENERAL TAGGING OF NEW TECHNOLOGICAL DEVELOPMENTS; GENERAL TAGGING OF CROSS-SECTIONAL TECHNOLOGIES SPANNING OVER SEVERAL SECTIONS OF THE IPC; TECHNICAL SUBJECTS COVERED BY FORMER USPC CROSS-REFERENCE ART COLLECTIONS [XRACs] AND DIGESTS
    • Y02TECHNOLOGIES OR APPLICATIONS FOR MITIGATION OR ADAPTATION AGAINST CLIMATE CHANGE
    • Y02DCLIMATE CHANGE MITIGATION TECHNOLOGIES IN INFORMATION AND COMMUNICATION TECHNOLOGIES [ICT], I.E. INFORMATION AND COMMUNICATION TECHNOLOGIES AIMING AT THE REDUCTION OF THEIR OWN ENERGY USE
    • Y02D30/00Reducing energy consumption in communication networks
    • Y02D30/50Reducing energy consumption in communication networks in wire-line communication networks, e.g. low power modes or reduced link rate

Landscapes

  • Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Databases & Information Systems (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Data Mining & Analysis (AREA)
  • Software Systems (AREA)
  • Computational Linguistics (AREA)
  • Information Retrieval, Db Structures And Fs Structures Therefor (AREA)

Abstract

The embodiment of the application provides a caching method and device, and belongs to the technical field of communication. Judging whether the GraphQL request needs to be cached according to a request method and a request name of the GraphQL request, acquiring a Key Value of the GraphQL request when the GraphQL request needs to be cached, inquiring a database according to the Key Value, and returning a Value corresponding to the Key Value when the Key Value exists in the database. When the Key value does not exist in the database, the request function is used for processing, and the processing result of the request function is cached. If the same request is frequently initiated, the result obtained from the cache can obviously reduce the processing time and improve the response speed. Furthermore, the buffer memory is avoided on the function called by the interface, but the buffer memory is directly carried out in the interface, and the buffer memory speed of the buffer memory interface is high, so that the development efficiency is improved.

Description

Caching method and device
Technical Field
The embodiments of the present application belong to the field of communication technologies, and in particular relate to a caching method and device.
Background
In web applications, there are often operations that require computational statistical analysis of large amounts of data, which are not highly real-time.
Under the traditional Restful interface, since there is little strong type of verification, it is easy to cache the results of the computation on the functions called by the interface.
Under the condition of using the GraphQL interface, the serialization and the deserialization of the result and the input parameter are very complicated due to the existence of strong type check sum and a large number of custom types, so that the caching of the interface on the function called by the interface is very difficult, the caching speed of the cache interface is low, and the development efficiency is low.
Disclosure of Invention
The invention provides a caching method and a caching device, which aim to solve the technical problems that in the prior art, caching is difficult on functions called by interfaces, and the caching speed of the cache interfaces is low, so that development efficiency is low.
In a first aspect, the present invention provides a caching method, including:
receiving an HTTP request initiated by a client, wherein the HTTP request comprises a GraphQL request;
responding to the HTTP request, and analyzing the HTTP request to acquire user information of the client;
authenticating the client according to the user information;
when the client authentication passes, analyzing the GraphQL request to obtain a request method and a request name of the GraphQL request;
inquiring a cache table in the system according to the request method and the request name, and judging whether the GraphQL request needs to be cached;
when the GraphQL request does not need to be cached, processing is carried out through a request function;
when the GraphQL request needs to be cached, acquiring a Key value of the GraphQL request, and inquiring a database according to the Key value; when the Key Value exists in the database, returning a Value corresponding to the Key Value; when the Key value does not exist in the database, processing through a request function;
judging whether the GraphQL request has errors according to the processing result of the request function, if so, returning the processing result of the request function to the client, and caching the processing result of the request function; otherwise, returning an error prompt to the client.
In a second aspect, the present invention provides a buffering device, including:
the receiving module is used for receiving an HTTP request initiated by the client, wherein the HTTP request comprises a GraphQL request;
the response module is used for responding to the HTTP request and analyzing the HTTP request to acquire the user information of the client;
the authentication module is used for authenticating the client according to the user information;
the analysis module is used for analyzing the GraphQL request to acquire a request method and a request name of the GraphQL request when the client authentication passes;
the judging module is used for inquiring a cache table in the system according to the request method and the request name and judging whether the GraphQL request needs to be cached or not;
the processing module is used for processing through a request function when the GraphQL request does not need to be cached;
the query module is used for acquiring the Key value of the GraphQL request when the GraphQL request needs to be cached, and querying the database according to the Key value; when the Key Value exists in the database, returning a Value corresponding to the Key Value; when the Key value does not exist in the database, processing through a request function;
the caching module is used for judging whether the GraphQL request has errors according to the processing result of the request function, if so, returning the processing result of the request function to the client and caching the processing result of the request function; otherwise, returning an error prompt to the client.
Compared with the prior art, the invention has at least the following beneficial effects:
according to the method and the name of the request of the GraphQL request, whether the GraphQL request needs to be cached is judged, when the GraphQL request needs to be cached, the Key Value of the GraphQL request is obtained, a database is queried according to the Key Value, and when the Key Value exists in the database, the Value corresponding to the Key Value is returned. When the Key value does not exist in the database, the request function is used for processing, and the processing result of the request function is cached. If the same request is frequently initiated, the result obtained from the cache can obviously reduce the processing time and improve the response speed. Furthermore, the buffer memory is avoided on the function called by the interface, but the buffer memory is directly carried out in the interface, and the buffer memory speed of the buffer memory interface is high, so that the development efficiency is improved.
Drawings
FIG. 1 is a schematic flow chart of a caching method provided by the invention;
FIG. 2 is a schematic diagram of a buffering device according to the present invention;
the accompanying drawings, which are included to provide a further understanding of the application and are incorporated in and constitute a part of this application, illustrate embodiments of the application and together with the description serve to explain the application and do not constitute an undue limitation to the application. Some specific embodiments of the present application will be described in detail hereinafter by way of example and not by way of limitation with reference to the accompanying drawings.
Detailed Description
In order to enable those skilled in the art to better understand the present application, the following description will make clear and complete descriptions of the technical solutions in the embodiments of the present application with reference to the accompanying drawings in the embodiments of the present application. It will be apparent that the described embodiments are merely some, but not all, of the embodiments of the present application. All other embodiments, which can be made by one of ordinary skill in the art based on the embodiments herein without making any inventive effort, shall fall within the scope of the present application.
In a first aspect, referring to fig. 1, a flowchart of a caching method provided by an embodiment of the present invention is shown.
The invention provides a caching method, which comprises the following steps:
s101: and receiving an HTTP request initiated by the client.
The HTTP request comprises a GraphQL request.
The HTTP request is a request message sent by the client to the server, and is used for acquiring or transmitting data. The GraphQL request is a specific type of HTTP request, and is used to send GraphQL query, change or subscription operations. The advantage of GraphQL is that it provides a higher degree of flexibility and efficiency. The client can specify the required data precisely without relying on a fixed endpoint defined by the server. This reduces unnecessary data transmission and improves network efficiency. Meanwhile, graphQL also provides introspection capability so that clients can dynamically learn and adapt to the capabilities of the server.
S102: in response to the HTTP request, the HTTP request is parsed to obtain user information of the client.
Optionally, the user information of the client is obtained by parsing fields such as Cookie and JWT Token in header of the HTTP request.
Among them, cookie is a mechanism for transferring state information between a client and a server in the HTTP protocol. When a client sends an HTTP request, relevant user information may be stored in a Cookie and sent to a server along with the request. After receiving the request, the server can analyze the value in the Cookie to acquire the user information.
Among them, JWT Token is an open standard for transferring security information between clients and servers. JWTs are typically used as authentication and authorization mechanisms for conveying user information or access rights in a request. After authentication, the client typically receives a JWT Token containing the user information. The Token is issued by the server, which contains encrypted or signed user data. In subsequent requests, the client needs to send the JWT Token to the server as the beer Token field of the Authorization header of the request. The server may parse the JWT Token, verify its signature and validity, and extract the user information therein.
S103: and authenticating the client according to the user information.
The purpose of authenticating the client according to the user information is to confirm whether the client is a legal user. By authenticating the client and determining whether the client is a legal user, higher security and fine-grained access control can be provided, and user data and system security can be protected.
Specifically, the client may be authenticated by checking a user identification, verifying authority, verifying a signature, password verification, and two-factor verification.
In one possible implementation, S103 specifically includes:
and inquiring whether the authentication cache module in the system records user information.
Specifically, JWT content may be retrieved as a Key into the authentication cache module to confirm whether the authentication cache module has user information recorded.
When the authentication cache module records the user information, the client is determined to be a legal user, and the client passes authentication.
And when the authentication cache module does not record the user information, performing JWT authentication on the client.
JWT authentication, among other things, provides a simple, secure and extensible authentication mechanism that can be used to protect API endpoints, implement Single Sign On (SSO), and build authentication and authorization functions in a distributed system.
In one possible implementation manner, when the authentication cache module does not record the user information, JWT authentication is performed on the client, and specifically includes:
the JWT field storing the user information is segmented according to a preset symbol and divided into Header information, payload information and Verify Signature information.
Wherein the preset symbol may be "".
The Header information Header includes the type of the message (fixed jwt authentication type) and the encryption algorithm (fixed encryption algorithm) used, and both the information are fixed in the invention, so that in the actual application process, the first segment is not required to be decrypted, but is directly compared with a fixed value, and if the first segment is different, the first segment is identified as an illegal user. The Payload information Payload and the verification information Signature require subsequent decryption.
The Payload information Payload generally includes a user name and an authentication expiration time, and the system compares the user name with a system database, and if the user name does not exist or expires, the system identifies the user as an illegal user.
Wherein the Verify Signature information is generated by signing the header and payload with a private key for verifying the integrity and authenticity of the message. The signature is typically generated by a signature algorithm and private key specified in the header.
And decrypting the segmented Header information, payload information and Verify Signature information respectively.
Specifically, base64 decryption may be performed separately for each of the segmented JWT fields.
And sequentially checking the Header information, the Payload information and the Verify Signature information.
Under the condition that the authentication of the Header information Header and the Payload information Payload is successful, the verification information Verify Signature can be verified through an HMACSHA256 algorithm, and if the verification is successful, the verification is determined as a legal user.
The HMACSHA256 algorithm is a message authentication code algorithm based on a SHA-256 hash function and a secret key. It is used to verify the integrity and authenticity of the data to ensure that the data has not been tampered with during transmission. The present invention is not described in detail, but is well established prior art.
When the Header information, the Payload information and the Verify Signature information are checked successfully, determining that the client is a legal user, and recording the user information into an authentication cache module; otherwise, determining the client as an illegal user.
The user information is recorded in the authentication cache module, the cache expiration time is set, decryption is not needed when the same jwt information exists next time, the database is compared with the peer-to-peer step, the user information can be directly identified as a legal user, the verification process is not needed to be repeated, and the request processing efficiency is improved.
S104: when the client authentication passes, analyzing the GraphQL request to obtain a request method and a request name of the GraphQL request.
Specifically, the request method and the request name may be obtained by parsing a string form of the GraphQL request, converting it into a GraphQL syntax tree (AST), and then extracting the operation name and the type of the request from the AST. This can facilitate corresponding processing and logic decisions based on the different types of requests.
In one possible implementation, S104 specifically includes:
it is detected whether the start character of the current request is GraphQL.
It should be noted that the purpose of detecting whether the start character of the current request is GraphQL is to determine whether the received request is a GraphQL request, because in an HTTP request of an application program, there may be other types of requests (such as RESTful API requests). By detecting whether the start character is GraphQL, the type of request can be quickly determined for proper processing.
When the initial character of the current request is detected to be GraphQL, a GraphQLVaildator verifier is used for verifying the GraphQL request, and a request method and a request name of the GraphQL request are obtained.
GraphQL Validator is a tool or library for verifying the correctness of the grammar and structure of GraphQL queries.
In one possible implementation, the GraphQL request is verified by using a GraphQLVaildator verifier, which specifically includes:
performing lexical analysis on the GraphQL request, and decomposing the GraphQL request into a plurality of Token lexical units.
Wherein, each lexical unit may represent a syntax element of GraphQL, such as a field, a variable, an operation type, and the like. The purpose of lexical analysis is to convert an input string into a series of meaningful Token.
A plurality of Token lexical units are combined into a syntax tree.
The grammar tree represents the structure and the hierarchical relation of the GraphQL request, and the input GraphQL request is organized and nested according to grammar rules.
The syntax tree is converted into an abstract syntax tree AST.
The abstract grammar tree AST is a more abstract and simplified representation form, grammar details and redundant information are removed, and only key elements and structures of the GraphQL request are reserved. AST provides a more convenient, operable form to handle GraphQL requests.
The request method and the request name of the GraphQL request are extracted from the abstract syntax tree AST.
Specifically, by traversing an AST, a corresponding node or attribute may be found from which the request method (e.g., query, change, subscription) and request name (particular operation name) are obtained.
In the invention, the grammar accuracy of the request can be ensured by lexical analysis, grammar analysis, AST conversion and extraction of the request method and the request name, the structured request representation form is provided, the request is flexibly processed, and corresponding operation and logic judgment are carried out. By the aid of the method, accuracy of a request method and a request name for extracting the GraphQL request is improved.
S105: and inquiring a cache table in the system according to the request method and the request name, and judging whether the GraphQL request needs to be cached.
Specifically, whether the GraphQL request needs to be cached or not can be determined according to the expiration time of the current cache of the GraphQL request. If the current cache of the GraphQL request is expired, the current cache is needed to be cached, otherwise, the current cache is not needed to be cached.
In one possible implementation, S105 specifically includes:
and carrying out Hash processing on the request method and the request name to obtain a Hash processing result.
The hash processing is an algorithm for converting input data of an arbitrary length into output of a fixed length. By hashing the request method and the request name, a unique hash value can be generated as the identification.
And matching in a cache table according to the result of the Hash processing to obtain the expiration time of the current cache of the GraphQL request.
The cache table is a data structure for storing cached requests, and the hash processing result of each request and corresponding cache information, such as expiration time, are recorded therein. By matching the hash result with the record in the cache table, it can be determined whether there is already a cache and the expiration time of the cache is acquired.
When the expiration time of the current cache exceeds the preset time, determining that the GraphQL request needs to be cached, otherwise, determining that the GraphQL request does not need to be cached.
When the expiration time of the current cache exceeds the preset time, the cache is expired, and the request needs to be cached again. Otherwise, if the expiration time of the current cache is still within the preset time range, the cache is still valid, and re-caching is not needed.
According to the invention, the validity of the cache can be detected in time by judging whether the expiration time of the current cache exceeds the preset time. If the expiration time of the cache has exceeded the preset time, meaning that the cache has expired, the latest results need to be re-cached. Therefore, the use of outdated cache data can be avoided, and the accuracy and the instantaneity of the data are ensured. Further, all GraphQL requests do not need to be cached, and only when the expiration time of the caching exceeds the preset time, the caching is performed. Therefore, excessive cache can be avoided, storage space is saved, and unnecessary resource consumption is reduced. Only GraphQL requests which are frequently requested and have a longer validity period can be cached, so that the validity and the utilization rate of the cache are improved.
S106: when the GraphQL request does not need to be cached, the processing is performed through a request function.
Wherein, the request function refers to an actual function or method for processing the GraphQL request. And executing corresponding business logic according to the received GraphQL request and returning a result to the client.
S107: and when the GraphQL request needs to be cached, acquiring the Key value of the GraphQL request. Inquiring the database according to the Key Value, and returning a Value corresponding to the Key Value when the Key Value exists in the database. When the Key value does not exist in the database, the processing is performed through the request function.
Wherein the Key value is a value for uniquely identifying and indexing cache data in the cache system. In this step, the information such as the field, parameter, operation type, etc. of the request may be combined to form a unique identifier, i.e., key value. This ensures that different requests generate different Key values so that different cached data can be distinguished and indexed.
It should be noted that, if there is cache data corresponding to the Key Value in the database, the Value of the cache data may be directly fetched from the database and returned to the client. If the Key value does not exist in the database, indicating that the requested data has not been cached, processing by the request function is required. The request function executes corresponding business logic, acquires data from a database or other data sources, processes the request, and finally obtains a processing result. The processing results may be returned to the client and cached prior to return so that the next same request may retrieve data directly from the cache.
According to the invention, when the GraphQL request needs to be cached, a database or a cache system can be queried according to the key value, so that the acquisition and the utilization of the cached data are realized. Thus, unnecessary calculation and database query operations can be reduced, and the response speed and performance of the system can be improved. Meanwhile, when the cached data does not exist, the data returned to the client can be ensured to be up to date by processing through the request function, and the data is cached after the processing is finished so as to be used later.
In a possible implementation manner, the obtaining the Key value of the GraphQL request specifically includes:
and carrying out Hash processing on the HTTP request to obtain a first Hash result.
The first Hash processing is to perform Hash processing on the HTTP request, so as to map the whole request content into a Hash value with a fixed length. Thus, different requests can be ensured to generate different hash values, and uniqueness is ensured. By carrying out Hash processing on the HTTP request, the content of the request can be converted into a Hash value with short and fixed length, so that the subsequent processing and indexing are facilitated.
And splicing the user information with the first Hash result, and then performing second Hash processing to obtain a second Hash result, wherein the second Hash result is the Key value of the GraphQL request.
The second Hash processing is to splice the user information with the first Hash result and perform the Hash processing again. The purpose of this is to combine the user information with the requested content to generate the final Key value. By splicing the user information with the first Hash result and then carrying out second Hash processing, a unique Key value can be generated and used for identifying and indexing cache data corresponding to the GraphQL request in a cache system.
It should be noted that, the second Hash processing combines the user information and the first Hash result, and splicing the user information and the first Hash result is equivalent to associating the user information with the original request content. By again performing the Hash process, a new Hash value can be generated that combines the user information with the original requested content. When the Key value is used for inquiring next time, the same process can be used for checking the user information.
S108: judging whether the GraphQL request has errors according to the processing result of the request function, if so, returning the processing result of the request function to the client, and caching the processing result of the request function. Otherwise, returning an error prompt to the client.
Specifically, the request function is a function that processes and executes the GraphQL request, and may return processing results, such as query results, error information, and the like. By checking the processing result of the request function, it can be determined whether the GraphQL request is successfully executed or there is an error. If the request function returns an error result, judging that the GraphQL request has an error; if the request function returns an effective processing result, the GraphQL request can be judged to be successfully executed.
Further, when the processing result of the request function is determined to be a valid result, the result may be returned to the client as a response. At the same time, the result can be optionally cached. The purpose of the caching is to directly use the caching result when the same GraphQL request arrives next time, avoid repeatedly executing the request function, and improve the response speed and the system performance.
And if the processing result of the request function is determined to be an error result, a corresponding error prompt can be returned to the client. The error hint may include error codes, error messages, error details, etc. to indicate what error occurred to the client and how to handle the error.
In a possible implementation manner, judging whether the GraphQL request has an error according to a processing result of the request function specifically includes:
and searching the processing result of the request function, and determining that the GraphQL request has an error when an error field exists in the processing result of the request function.
It should be noted that the error field is generally used to identify an error condition in the execution of the request, and may include an error code, an error message, error details, and the like. Therefore, when the error field exists in the retrieved request function processing result, it can be determined that the GraphQL request has an error.
In a possible implementation manner, judging whether the GraphQL request has an error according to a processing result of the request function specifically includes:
adding annotation or function package to the request function, and determining that the GraphQL request has an error when the request function is overtime or fails to execute.
It should be noted that, by monitoring the execution time or the execution result of the request function, if the execution of the request function exceeds a preset time limit or the execution result indicates that the request fails, it may be determined that the GraphQL request has an error. Timeout and failure may be due to various reasons, such as network problems, dependent service failures, abnormal throws, etc. By the method, abnormal conditions in the request execution process can be timely found and processed, and the stability and reliability of the system are improved. And, the compatibility of the Restful request can be realized by using the method, and the method is applied to the cache of the Restful request.
Compared with the prior art, the invention has at least the following beneficial effects:
according to the method and the name of the request of the GraphQL request, whether the GraphQL request needs to be cached is judged, when the GraphQL request needs to be cached, the Key Value of the GraphQL request is obtained, a database is queried according to the Key Value, and when the Key Value exists in the database, the Value corresponding to the Key Value is returned. When the Key value does not exist in the database, the request function is used for processing, and the processing result of the request function is cached. If the same request is frequently initiated, the result obtained from the cache can obviously reduce the processing time and improve the response speed. Furthermore, the buffer memory is avoided on the function called by the interface, but the buffer memory is directly carried out in the interface, and the buffer memory speed of the buffer memory interface is high, so that the development efficiency is improved.
In a second aspect, referring to fig. 2, a schematic structural diagram of a buffering device according to an embodiment of the present invention is shown.
The present invention provides a caching apparatus 20, comprising:
the receiving module 201 is configured to receive an HTTP request initiated by a client, where the HTTP request includes a GraphQL request.
The response module 202 is configured to parse the HTTP request to obtain user information of the client in response to the HTTP request.
And the authentication module 203 is configured to authenticate the client according to the user information.
And the parsing module 204 is configured to parse the GraphQL request to obtain a request method and a request name of the GraphQL request when the client authentication passes.
The judging module 205 is configured to query a cache table in the system according to the request method and the request name, and judge whether the GraphQL request needs to be cached.
And the processing module 206 is configured to perform processing through a request function when the GraphQL request does not need to be cached.
And a query module 207, configured to obtain a Key value of the GraphQL request when the GraphQL request needs to be cached. Inquiring the database according to the Key Value, and returning a Value corresponding to the Key Value when the Key Value exists in the database. When the Key value does not exist in the database, the processing is performed through the request function.
And the caching module 208 is configured to determine whether the GraphQL request has an error according to the processing result of the request function, if so, return the processing result of the request function to the client, and cache the processing result of the request function. Otherwise, returning an error prompt to the client.
In one possible implementation, the authentication module 203 is specifically configured to:
and inquiring whether the authentication cache module in the system records user information.
When the authentication cache module records the user information, the client is determined to be a legal user, and the client passes authentication.
And when the authentication cache module does not record the user information, performing JWT authentication on the client.
In one possible implementation, the authentication module 203 is specifically configured to:
the JWT field storing the user information is segmented according to a preset symbol.
And decrypting the segmented JWT fields respectively.
Sequentially checking Header information, payload information and Verify Signature information;
when the Header information, the Payload information and the Verify Signature information are checked successfully, determining that the client is a legal user, and recording the user information into an authentication cache module; otherwise, determining the client as an illegal user.
In one possible implementation, the parsing module 204 is specifically configured to:
it is detected whether the start character of the current request is GraphQL.
When the initial character of the current request is detected to be GraphQL, a GraphQLVaildator verifier is used for verifying the GraphQL request, and a request method and a request name of the GraphQL request are obtained.
In one possible implementation, the parsing module 204 is specifically configured to:
performing lexical analysis on the GraphQL request, and decomposing the GraphQL request into a plurality of Token lexical units.
A plurality of Token lexical units are combined into a syntax tree.
The syntax tree is converted into an abstract syntax tree AST.
The request method and the request name of the GraphQL request are extracted from the abstract syntax tree AST.
In one possible implementation, the determining module 205 is specifically configured to:
and carrying out Hash processing on the request method and the request name to obtain a Hash processing result.
And matching in a cache table according to the result of the Hash processing to obtain the expiration time of the current cache of the GraphQL request.
When the expiration time of the current cache exceeds the preset time, determining that the GraphQL request needs to be cached, otherwise, determining that the GraphQL request does not need to be cached.
In one possible implementation, the query module 207 is specifically configured to:
and carrying out Hash processing on the HTTP request to obtain a first Hash result.
And splicing the user information with the first Hash result, and then performing second Hash processing to obtain a second Hash result, wherein the second Hash result is the Key value of the GraphQL request.
In one possible implementation, the buffering module 208 is specifically configured to:
and searching the processing result of the request function, and determining that the GraphQL request has an error when an error field exists in the processing result of the request function.
In one possible implementation, the buffering module 208 is specifically configured to:
adding annotation or function package to the request function, and determining that the GraphQL request has an error when the request function is overtime or fails to execute.
The buffer device 20 provided in the present invention can implement each process implemented in the above method embodiment, and in order to avoid repetition, details are not repeated here.
The virtual device provided by the invention can be a device, and can also be a component, an integrated circuit or a chip in a terminal.
Compared with the prior art, the invention has at least the following beneficial effects:
according to the method and the name of the request of the GraphQL request, whether the GraphQL request needs to be cached is judged, when the GraphQL request needs to be cached, the Key Value of the GraphQL request is obtained, a database is queried according to the Key Value, and when the Key Value exists in the database, the Value corresponding to the Key Value is returned. When the Key value does not exist in the database, the request function is used for processing, and the processing result of the request function is cached. If the same request is frequently initiated, the result obtained from the cache can obviously reduce the processing time and improve the response speed. Furthermore, the buffer memory is avoided on the function called by the interface, but the buffer memory is directly carried out in the interface, and the buffer memory speed of the buffer memory interface is high, so that the development efficiency is improved.
Finally, it should be noted that: the above embodiments are only for illustrating the technical solution of the present application, and are not limited thereto. Although the present application has been described in detail with reference to the foregoing embodiments, it should be understood by those of ordinary skill in the art that: the technical scheme described in the foregoing embodiments may be modified or some or all of the technical features may be replaced with equivalents. Such modifications and substitutions do not depart from the spirit of the corresponding technical solutions from the scope of the technical solutions of the embodiments of the present application.

Claims (10)

1. A caching method, comprising:
receiving an HTTP request initiated by a client, wherein the HTTP request comprises a GraphQL request;
responding to the HTTP request, and analyzing the HTTP request to acquire user information of the client;
authenticating the client according to the user information;
when the client authentication passes, analyzing the GraphQL request to obtain a request method and a request name of the GraphQL request;
inquiring a cache table in the system according to a request method and a request name, and judging whether the GraphQL request needs to be cached or not;
when the GraphQL request does not need to be cached, processing through a request function;
when the GraphQL request needs to be cached, acquiring a Key value of the GraphQL request, and inquiring a database according to the Key value; returning a Value corresponding to the Key Value when the Key Value exists in the database; when the Key value does not exist in the database, processing through a request function;
judging whether the GraphQL request has errors according to the processing result of the request function, if so, returning the processing result of the request function to the client and caching the processing result of the request function; otherwise, returning an error prompt to the client.
2. The caching method according to claim 1, wherein the authenticating the client according to the user information specifically includes:
inquiring whether an authentication cache module in the system records the user information or not;
when the authentication cache module records the user information, determining that the client is a legal user, and passing the authentication of the client;
and when the authentication cache module does not record the user information, performing JWT authentication on the client.
3. The caching method of claim 2, wherein the JWT authentication for the client specifically includes:
segmenting the JWT field storing the user information into a Header information, a Payload information and a Verify Signature information according to a preset symbol;
decrypting the Header information, payload information and Verify Signature information;
sequentially checking the decrypted Header information, payload information and Verify Signature information;
when the decrypted Header information, payload information and Verify Signature information are checked successfully, determining that the client is a legal user, and recording the user information into the authentication cache module; otherwise, determining the client as an illegal user.
4. The caching method according to claim 1, wherein the parsing the GraphQL request to obtain the request method and the request name of the GraphQL request specifically includes:
detecting whether the initial character of the current request is GraphQL;
when the initial character of the current request is detected to be GraphQL, a GraphQLVaildator verifier is used for verifying the GraphQL request, and a request method and a request name of the GraphQL request are obtained.
5. The caching method according to claim 4, wherein the verifying the GraphQL request using the GraphQLVaildator includes:
performing lexical analysis on the GraphQL request, and decomposing the GraphQL request into a plurality of Token lexical units;
combining a plurality of Token lexical units into a grammar tree;
converting the grammar tree into an abstract grammar tree AST;
and extracting a request method and a request name of the GraphQL request from the abstract syntax tree AST.
6. The caching method according to claim 1, wherein the determining whether the GraphQL request needs to be cached according to the request method and a cache table in the request name query system includes:
carrying out Hash processing on the request method and the request name to obtain a Hash processing result;
matching is carried out in the cache table according to the Hash processing result, and the expiration time of the current cache of the GraphQL request is obtained;
and when the expiration time of the current cache exceeds a preset time, determining that the GraphQL request needs to be cached, otherwise, determining that the GraphQL request does not need to be cached.
7. The caching method of claim 1, wherein the obtaining the Key value of the GraphQL request specifically includes:
carrying out Hash processing on the HTTP request to obtain a first Hash processing result;
and splicing the user information with the first Hash processing result, and then performing second Hash processing to obtain a second Hash processing result, wherein the second Hash processing result is the Key value of the GraphQL request.
8. The caching method according to claim 1, wherein the determining whether the GraphQL request has an error according to the processing result of the request function specifically includes:
and searching a processing result of the request function, and determining that the GraphQL request has an error when an error field exists in the processing result of the request function.
9. The caching method according to claim 1, wherein the determining whether the GraphQL request has an error according to the processing result of the request function specifically includes:
adding notes or function packages to the request function, and determining that the GraphQL request has errors when the request function is overtime or fails to execute.
10. A caching apparatus, comprising:
the receiving module is used for receiving an HTTP request initiated by the client, wherein the HTTP request comprises a GraphQL request;
the response module is used for responding to the HTTP request and analyzing the HTTP request to acquire the user information of the client;
the authentication module is used for authenticating the client according to the user information;
the analyzing module is used for analyzing the GraphQL request to acquire a request method and a request name of the GraphQL request when the client authentication passes;
the judging module is used for inquiring a cache table in the system according to the request method and the request name and judging whether the GraphQL request needs to be cached or not;
the processing module is used for processing through a request function when the GraphQL request does not need to be cached;
the query module is used for acquiring the Key value of the GraphQL request when the GraphQL request needs to be cached, and querying a database according to the Key value; returning a Value corresponding to the Key Value when the Key Value exists in the database; when the Key value does not exist in the database, processing through a request function;
the caching module is used for judging whether the GraphQL request has errors according to the processing result of the request function, if so, returning the processing result of the request function to the client and caching the processing result of the request function; otherwise, returning an error prompt to the client.
CN202310807434.1A 2023-07-04 2023-07-04 Caching method and device Pending CN116521745A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202310807434.1A CN116521745A (en) 2023-07-04 2023-07-04 Caching method and device

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202310807434.1A CN116521745A (en) 2023-07-04 2023-07-04 Caching method and device

Publications (1)

Publication Number Publication Date
CN116521745A true CN116521745A (en) 2023-08-01

Family

ID=87399799

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202310807434.1A Pending CN116521745A (en) 2023-07-04 2023-07-04 Caching method and device

Country Status (1)

Country Link
CN (1) CN116521745A (en)

Citations (6)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN111447220A (en) * 2020-03-26 2020-07-24 金蝶软件(中国)有限公司 Authentication information management method, server of application system and computer storage medium
CN114285835A (en) * 2021-12-30 2022-04-05 北京天融信网络安全技术有限公司 HTTP request data processing method and system
CN115396180A (en) * 2022-08-23 2022-11-25 中国民航信息网络股份有限公司 Micro service gateway unified authentication method, device, micro service gateway and storage medium
CN115757495A (en) * 2022-11-21 2023-03-07 中国平安财产保险股份有限公司 Cache data processing method and device, computer equipment and storage medium
CN116226200A (en) * 2023-03-07 2023-06-06 智慧星光(安徽)科技有限公司 BFF architecture-based service interface data caching method, device and equipment
CN116527341A (en) * 2023-04-19 2023-08-01 上海众种生态科技有限公司 Client-side calling rear-end interface authentication authorization security method

Patent Citations (6)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN111447220A (en) * 2020-03-26 2020-07-24 金蝶软件(中国)有限公司 Authentication information management method, server of application system and computer storage medium
CN114285835A (en) * 2021-12-30 2022-04-05 北京天融信网络安全技术有限公司 HTTP request data processing method and system
CN115396180A (en) * 2022-08-23 2022-11-25 中国民航信息网络股份有限公司 Micro service gateway unified authentication method, device, micro service gateway and storage medium
CN115757495A (en) * 2022-11-21 2023-03-07 中国平安财产保险股份有限公司 Cache data processing method and device, computer equipment and storage medium
CN116226200A (en) * 2023-03-07 2023-06-06 智慧星光(安徽)科技有限公司 BFF architecture-based service interface data caching method, device and equipment
CN116527341A (en) * 2023-04-19 2023-08-01 上海众种生态科技有限公司 Client-side calling rear-end interface authentication authorization security method

Similar Documents

Publication Publication Date Title
US11296865B1 (en) Cryptographic algorithm status transition
US8751815B2 (en) Creating and verifying globally unique device-specific identifiers
JP5658745B2 (en) HTTP-based authentication
US8693690B2 (en) Organizing an extensible table for storing cryptographic objects
US20180020008A1 (en) Secure asynchronous communications
US8869258B2 (en) Facilitating token request troubleshooting
CN112468520B (en) Data detection method, device and equipment and readable storage medium
CN110263579B (en) Data processing method, system and related equipment
CN110888838B (en) Request processing method, device, equipment and storage medium based on object storage
US20110029555A1 (en) Method, system and apparatus for content identification
US20070136794A1 (en) Request authentication token
US20070204156A1 (en) Systems and methods for providing access to network resources based upon temporary keys
CN1340940A (en) Method for dealing inserted-requested message of business in groups
CN110445615B (en) Network request security verification method, device, medium and electronic equipment
CN107306246A (en) Based on the data capture method for accessing key
CN113225351B (en) Request processing method and device, storage medium and electronic equipment
WO2022057002A1 (en) Abnormal request processing method and device
CN111367923A (en) Data processing method, data processing device, node equipment and storage medium
CN114157434A (en) Login verification method and device, electronic equipment and storage medium
WO2021078062A1 (en) Ssl certificate verification method, apparatus and device, and computer storage medium
CN111371811B (en) Resource calling method, resource calling device, client and service server
CN116521745A (en) Caching method and device
EP4203438A1 (en) Message pushing method, and electronic device and storage medium
CN112416875B (en) Log management method, device, computer equipment and storage medium
CN110750290B (en) Software version upgrading method and device based on DNS query

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
RJ01 Rejection of invention patent application after publication

Application publication date: 20230801