CN110109983B - Method and device for operating Redis database - Google Patents

Method and device for operating Redis database Download PDF

Info

Publication number
CN110109983B
CN110109983B CN201711477196.3A CN201711477196A CN110109983B CN 110109983 B CN110109983 B CN 110109983B CN 201711477196 A CN201711477196 A CN 201711477196A CN 110109983 B CN110109983 B CN 110109983B
Authority
CN
China
Prior art keywords
metadata
redis database
interface
operation interface
stored
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.)
Active
Application number
CN201711477196.3A
Other languages
Chinese (zh)
Other versions
CN110109983A (en
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 Jingdong Century Trading Co Ltd
Beijing Jingdong Shangke Information Technology Co Ltd
Original Assignee
Beijing Jingdong Century Trading Co Ltd
Beijing Jingdong Shangke Information Technology 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 Jingdong Century Trading Co Ltd, Beijing Jingdong Shangke Information Technology Co Ltd filed Critical Beijing Jingdong Century Trading Co Ltd
Priority to CN201711477196.3A priority Critical patent/CN110109983B/en
Publication of CN110109983A publication Critical patent/CN110109983A/en
Application granted granted Critical
Publication of CN110109983B publication Critical patent/CN110109983B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Images

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/242Query formulation
    • G06F16/2433Query languages
    • 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/25Integrating or interfacing systems involving database management systems
    • 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/25Integrating or interfacing systems involving database management systems
    • G06F16/258Data format conversion from or to a database
    • 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/907Retrieval characterised by using metadata, e.g. metadata not derived from the content or metadata generated manually

Abstract

The invention discloses a method and a device for operating a Redis database, and relates to the technical field of computers. One embodiment of the method comprises: obtaining metadata of an object, the metadata comprising: the stored key value, the stored data structure and the data format of each field; mapping to an interface of a Redis database according to the called operation interface and the metadata, and calling the interface of the Redis database to realize the operation of the Redis database; the called operation interface is included in an operation interface set, and the operation interface in the operation interface set corresponds to an interface of the Redis database. Because the operation interface in the operation interface set corresponds to the Redis database, after the metadata of the object is obtained, the operation interface in the called operation interface set and the metadata can be mapped to the interface of the Redis database, and the Redis database can be operated more conveniently and efficiently.

Description

Method and device for operating Redis database
Technical Field
The invention relates to the technical field of computers, in particular to a method and a device for operating a Redis database.
Background
The Redis Database is a log-type and Key-Value Database which can be based on a memory and can also be persistent, the Redis Database is widely applied along with the development of the Internet, and the traditional RDBMS (Relational Database Management System) has a plurality of mature ORM (object Relational mapping) frameworks for supporting, but not a plurality of ORM frameworks for the Redis Database. In the prior art, Spring Data only serializes and deserializes objects for ORM operation of a Redis database, but cannot provide a rich Data storage structure by using Redis. The serialization of the object refers to a process of converting the object into a byte sequence, and the deserialization refers to a process of restoring the byte sequence into the object.
In the process of implementing the invention, the inventor finds that at least the following problems exist in the prior art: spring Data can not directly convert an object into a designated Data storage structure, and a simple structure of a Key Value Key-Value is used for storage, so that the simple storage structure cannot utilize the characteristics of various storage structures which can be supported by a Redis database; the use of Spring Data requires the establishment of a Spring Framework environment, the use of Spring Data depends on Spring Framework management, the maintenance complexity is increased, the time cost is increased, and developers also need to pay attention to the conversion of objects and a Redis database storage structure; the Spring Data serializes the object into a character string in an object serialization way, after the character string is written into a Redis database, if the written Data is modified or partially deleted, the whole object needs to be deserialized, the whole object is returned from the Redis database, and after the Data is modified or partially deleted, the whole object is serialized into a character string and written into the Redis database, and the operation cannot be completed by using some programs of the Redis database.
Disclosure of Invention
In view of this, embodiments of the present invention provide a method and an apparatus for operating a Redis database, where an operation interface in an operation interface set corresponds to a to-be-operated Redis database, and therefore after metadata of an object is obtained, the operation interface in the called operation interface set and the metadata can be mapped to an interface of the Redis database, so that the Redis database is operated more conveniently and efficiently. The operation instructions in the operation interface set are developed and stored according to the Redis database to be operated, and when the Redis database is operated, the operation instructions can be directly mapped to the interface of the Redis database through parameters, so that the operation on the Redis database is realized through the method, and developers do not need to care about bottom layer implementation details.
To achieve the above object, according to an aspect of an embodiment of the present invention, there is provided a method of operating a Redis database.
The method for operating the Redis database comprises the following steps: obtaining metadata of an object, the metadata comprising: the stored key value, the stored data structure and the data format of each field; mapping to an interface of a Redis database according to the called operation interface and the metadata, and calling the interface of the Redis database to realize the operation of the Redis database; the called operation interface is included in an operation interface set, and the operation interface in the operation interface set corresponds to an interface of the Redis database.
Optionally, the step of obtaining metadata of the object includes: judging whether the metadata of the object exists in a local cache or not; if yes, obtaining the metadata of the object from a local cache; otherwise, obtaining the metadata of the object from the annotation of the object through a reflection mechanism, and writing the metadata into a local cache.
Optionally, after obtaining the metadata of the object and before mapping to the interface of the Redis database, further comprising: and if the key value stored in the metadata does not exist, generating the stored key value according to the key value definition rule in the annotation of the object.
Optionally, the set of operation interfaces includes: adding operation interfaces, deleting operation interfaces, modifying operation interfaces and inquiring operation interfaces.
Optionally, after obtaining the metadata of the object and before mapping to the interface of the Redis database, further comprising: and under the condition that the called operation interface is an added operation interface, performing format conversion on the object according to the data structure stored in the metadata and the data format of each field.
Optionally, after mapping to an interface of a Redis database and calling the interface of the Redis database to implement an operation on the Redis database, the method further includes: and receiving a result returned by the Redis database, and displaying the returned result.
Optionally, the metadata further comprises a format of the display; after receiving the result returned by the Redis database and before displaying the returned result, the method further includes: and under the condition that the called operation interface is the query operation interface, performing format conversion on the returned result according to the displayed format.
To achieve the above object, according to another aspect of an embodiment of the present invention, there is provided an apparatus for operating a Redis database.
The device for operating the Redis database of the embodiment of the invention comprises the following components: the operation interface module is used for storing the operation interface set; the operation interfaces in the operation interface set correspond to the interfaces of the Redis database; the metadata acquisition module is used for acquiring metadata of the object; the metadata includes: the stored key value, the stored data structure and the data format of each field; the mapping module is used for mapping the operation interface to an interface of a Redis database according to the called operation interface and the metadata, and calling the interface of the Redis database to realize the operation of the Redis database; wherein the called operation interface is included in an operation interface set.
Optionally, the metadata obtaining module is further configured to determine whether metadata of the object exists in a local cache; if yes, obtaining the metadata of the object from a local cache; otherwise, obtaining the metadata of the object from the annotation of the object through a reflection mechanism, and writing the metadata into a local cache.
Optionally, the system further includes a key value generation module, configured to determine that a key value stored in the metadata does not exist, and generate the stored key value according to a key value definition rule in the annotation of the object.
Optionally, the operation interface set stored in the operation interface module includes: adding operation interfaces, deleting operation interfaces, modifying operation interfaces and inquiring operation interfaces.
Optionally, the system further includes a format conversion module, configured to, when the called operation interface is an additional operation interface, perform format conversion on the metadata according to a data structure stored in the metadata and a data format of each field.
Optionally, the system further comprises a returned result processing module, configured to receive a result returned by the Redis database, and display the returned result.
Optionally, the returned result processing module is further configured to, when the called operation interface is an inquiry operation interface, perform format conversion on the returned result according to a displayed format, where the displayed format is included in the metadata.
To achieve the above object, according to still another aspect of an embodiment of the present invention, there is provided an electronic apparatus.
The electronic device of the embodiment of the invention comprises: one or more processors; storage means for storing one or more programs which, when executed by the one or more processors, cause the one or more processors to implement any of the above methods of operating a Redis database.
To achieve the above object, according to still another aspect of an embodiment of the present invention, there is provided a computer readable medium having stored thereon a computer program, which when executed by a processor, implements any one of the above methods of operating a Redis database.
One embodiment of the above invention has the following advantages or benefits: the mapping between the Java object and the Redis storage can be realized by calling the operation interface in the operation interface set. And obtaining metadata of the object, wherein the metadata comprises: the stored key value, the stored data structure, and the data format of each field. Because the operation interface in the operation interface set corresponds to the Redis database, the called operation interface and the metadata are used as parameters and can be mapped to the interface of the Redis database, so that the Redis database can be operated more conveniently and efficiently. By operating the Redis database in the mode, the mapping between the object and the storage of the Redis database is realized, various stored data structures of the Redis database are flexibly supported, the data structures stored by the object are defined in an annotation mode, the performance is improved, and the readability and the maintainability are enhanced.
Further effects of the above-mentioned non-conventional alternatives will be described below in connection with the embodiments.
Drawings
The drawings are included to provide a better understanding of the invention and are not to be construed as unduly limiting the invention. Wherein:
FIG. 1 is a schematic diagram of a main flow of a method of operating a Redis database according to an embodiment of the invention;
FIG. 2 is a schematic diagram of operations to implement a write of data to a Redis database, according to an embodiment of the present invention;
FIG. 3 is a schematic diagram of operations for querying Redis database data, in accordance with embodiments of the present invention;
FIG. 4 is a schematic diagram of the main modules of an apparatus for operating a Redis database according to an embodiment of the present invention;
FIG. 5 is an exemplary system architecture diagram in which embodiments of the present invention may be employed;
fig. 6 is a schematic block diagram of a computer system suitable for use in implementing a terminal device or server of an embodiment of the invention.
Detailed Description
Exemplary embodiments of the present invention are described below with reference to the accompanying drawings, in which various details of embodiments of the invention are included to assist understanding, and which are to be considered as 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.
Fig. 1 is a schematic diagram of a main flow of a method for operating a Redis database according to an embodiment of the present invention, and as shown in fig. 1, the method for operating a Redis database according to the embodiment of the present invention mainly includes:
step S101: and acquiring metadata of the object, wherein the metadata at least comprises a stored key value, a stored data structure and a data format of each field. The stored key values, stored data structures, and data formats of the fields in the metadata may be part of parameters that map the interfaces of the Redis database. Specifically, whether metadata of an object exists in a local cache is judged; if yes, obtaining the metadata of the object from the local cache; otherwise, the metadata of the object is obtained from the annotation through a reflection mechanism and written into a local cache. The metadata is obtained from the local cache, so that the operation of obtaining the metadata of the object through a reflection mechanism when the same operation is carried out every time can be avoided, and certain performance is correspondingly improved. For the Java reflection mechanism, "program runtime, allows changing the program structure or variable type, this language is called dynamic language", from this point of view Perl, Python, Ruby is dynamic language, C + +, Java, C # is not dynamic language. However, Java has a very prominent dynamic correlation mechanism: reflection, as used on Java, refers to classes that can be loaded, ascertained, and used at runtime, which are not known at all during compilation. That is, a Java program can load a class whose name is known at runtime, learn its complete construct (but not the methods definition), and generate its object entities, or value its fields, or invoke its methods.
Step S102: mapping to an interface of a Redis database according to the called operation interface and the metadata, and calling the interface of the Redis database to realize the operation of the Redis database; the called operation interface is included in an operation interface set, and the operation interface in the operation interface set corresponds to the interface of the Redis database. Because the operation interfaces in the operation interface set are developed according to the Redis database to be operated, the called operation interfaces and the metadata of the object are used as parameters, the operation interfaces can be mapped to the interfaces of the Redis database, and the mapped interfaces are called to realize the operation of the Redis database. The interface of the Redis database is an API (application programming interface) of the Redis client, so that the Redis server can complete the operation corresponding to the operation to be executed. The Redis client is a Redis self-contained client which is self-contained with a plurality of functions. After receiving the request of the client, the Redis server executes the request sent by the processing client and returns a corresponding processing result to the client.
And after obtaining the metadata of the object and before mapping to the interface of the Redis database, the method further comprises the following steps: and on the premise that the key value stored in the metadata does not exist, generating the stored key value according to the key value definition rule in the annotation. The set of operation interfaces includes: adding operation interfaces, deleting operation interfaces, modifying operation interfaces or inquiring operation interfaces. The operation interface is added to write data into the Redis database, the operation interface is deleted to delete the data stored in the Redis database, the operation interface is modified to modify the data stored in the Redis database, and the operation interface is inquired to inquire the data stored in the Redis database. When the called operation interface is an added operation interface, that is, data is written into the Redis database, the format of the object can be converted into a data structure stored in the Redis database, such as a string (string), a hash table (hash), a list (list), a set (set), a sorted set (sorted set), and the like, and storage of various structures supported by the Redis database is fully utilized. Therefore, in the case that the called operation interface is an add operation interface, after the metadata of the object is obtained and before the interface is mapped to the Redis database, format conversion of the object is further included according to the data format of each field and the data structure stored in the metadata.
After the step S102, the Redis server completes the operation corresponding to the operation to be executed, and returns the result of the operation. And after receiving the result returned by the Redis database, displaying the returned result. If the called operation interface is an operation interface added, an operation interface deleted or an operation interface modified, the Redis server returns a result of whether the operation is successful, and after receiving the result, the result is sent to the caller, and the caller knows the operation result. The result returned by the Redis server is the data format stored by the Redis database, and the data structure recognizable by the caller is the object. Therefore, if the called operation interface is query operation, the format of the result returned by the Redis server can be converted between the results returned by the Redis server are sent to the caller. If the metadata also includes a display format, the returned result can be converted into a format according to the display format after the returned result is received. And after the returned result is converted into an object which can be checked by the caller, sending the converted object to the caller.
In this embodiment, the caller describes the object through the defined annotation specification, and then may obtain metadata of the object by parsing the object, where the metadata includes the stored key value, the stored data structure, and the data format of each field. And the metadata and the operation interface called by the caller are used as parameters and can be mapped to the interface of the Redis database, so that the operation on the Redis database is completed. The annotation specification is defined, and the operation instruction is stored, so that the operation of the Redis database is realized in the mode, and developers do not care about the details of the bottom layer implementation. For example, annotation specifications are defined that include: @ interface domain @ identification: defining the data format of each field of the object, such as: string, Date, Json, default data format is String type; the order of each field may also be defined. @ interface domainclasssantanation: defining data structures for object storage, such as: HashMap, Set, Queue, etc., the default data structure is the String structure; and simultaneously, a Key value during data object storage can be defined, and the default Key value is the packet name plus the class name of the data object. The developed operation interface set comprises:
boolean insert (Object dataModelobject); // write dataModelObject to Redis;
boolean insert (Object dataModelObject, borolean include OrExclude, String … fields); write a given field in dataModelObject to Redis;
DataModelObject query (Object DataModelObject); // query dataModelObject.
Fig. 2 is a schematic diagram of an operation of implementing data writing into a Redis database according to an embodiment of the present invention, and as shown in fig. 2, an operation process of implementing data writing into a Redis database according to an embodiment of the present invention includes:
step S201: the caller describes the object through the annotation specification and calls the increase operation result according to the business requirement. Before defining a Java Bean, we know the defined annotation specification and the provided operation interface API, for example, the defined annotation specification includes: @ interface domain @ identification: defining the data format of each field of the object, such as: string, Date, Json, default String type; the order of each field may also be defined. @ interface domainclasssantanation: defining data structures for object storage, such as: HashMap, Set, Queue, etc., default String structure; meanwhile, the Key value of the data object during storage can be defined, and the packet name and the class name of the default data object can be defined. Operational interfaces have been developed that include:
boolean insert (Object dataModelobject); // write dataModelObject to Redis;
boolean insert (Object dataModelObject, borolean include OrExclude, String … fields); write a given field in dataModelObject to Redis;
DataModelObject query (Object DataModelObject); // query dataModelObject.
In this embodiment, the operation interface in the called operation interface set is Boolean insert (Object dataModelObject).
With knowledge of the annotation specification defined and the APIs provided, the Java Bean is defined from the actual data. For example, to store a person's information, a defined Java Bean is as follows:
Figure BDA0001533072420000091
Figure BDA0001533072420000101
when creating objects, default annotations are used without describing them, so that the Redis database cannot be used to support the properties of multiple storage structures. Describing a stored data structure of Java beans mapped to a Redis database by annotation, and defaulting String type; mapping the annotation description data model object to a Key Value of a Key-Value structure in Redis, and adding a default package name and a class name; the data format in the Redis database is mapped by annotating fields in the descriptive data model object, the string format being the default.
Step S202: whether the metadata of the object exists in the local cache or not, if so, executing the step S203; otherwise, step S204 is executed.
Step S203: metadata of an object is obtained. The metadata may include stored key values, stored data structures, and data formats for the fields.
Step S204: metadata of the object is obtained from the annotation through a reflection mechanism and is cached into a local cache. If the local cache does not contain the metadata of the object, acquiring a data structure and a data format of each field stored in Redis of the DataModelObject through a Java reflection mechanism, and writing the acquired metadata into the local cache. In the embodiment, a data structure to be generated is acquired from the @ domainclasstanzation annotation of Java Bean through a reflection mechanism; and acquiring the data format to be generated of each field from the @ Domain name Annotation of Java Bean through a reflection mechanism.
Step S205: the object is format converted according to the stored data structure in the metadata and the data format of each field. According to the data structure stored by the object in the metadata and the data format of each field of the object, the object is converted into the data structure stored by the Redis database, such as a data structure of a character string (string), a hash table (hash), a list (list), a set (set), an ordered set (sorted set) and the like. The Redis database is a key-value store, where key is a string of characters and value may contain a variety of data structures listed above. Taking the example that an object is generated into a HashMap storage structure, the field type is in a basic type transformation format, and the field type is in a custom type transformation Json format: reading the name, field value and field type of each field of the Java Bean through a reflection mechanism; if the field type is a basic type, processing the field value as String as the value finally stored in the field; the field type is not a basic type, and a Json toolkit (such as Gson and the like) calling an open source is converted into a Json format character string to be used as a value finally stored in the field. Taking the name of each field as a Key Value of the HashMap, and taking the Value of the character string type of each field after processing as a Value of the HashMap; and returning the generated HashMap object, and converting the object into a data structure of the HashMap.
Step S206: and generating the stored key value according to the key value definition rule in the annotation. And if the key value defined in the acquired metadata does not exist, generating the stored key value according to the key value definition rule in the annotation. After the object is created, the key value can be customized, and after the metadata is acquired, the stored key value can be directly acquired without executing the step.
Step S207: and calling a programming interface of the Redis client according to the added operation API and the key value stored by the object. And taking the key value stored by the added operation API and the object as parameters, calling the Redis client API, storing the result after format conversion into a Redis database by the Redis server, and returning the written result. And after receiving a result of successful writing or unsuccessful writing returned by the Redis server, transmitting the result to a caller, and further finishing the operation of the Redis database.
Fig. 3 is a schematic diagram of an operation of querying a Redis database data according to an embodiment of the present invention, and as shown in fig. 3, the operation of querying the Redis database data according to an embodiment of the present invention includes:
step S301: the caller describes the object through the annotation specification and calls the adding operation interface according to the business requirement.
Step S302: whether the metadata of the object exists in the local cache or not, if so, executing the step S303; otherwise, step S304 is performed.
Step S303: metadata of an object is obtained.
Step S304: metadata of the object is obtained from the annotation through a reflection mechanism and is cached into a local cache.
Step S305: and calling a programming interface of the Redis client according to the query operation API and the key value stored by the object. And calling a programming interface of the Redis client by taking the key value stored by the query operation API and the object in the metadata as a parameter, and executing the query operation by the Redis server.
Step S306: and receiving data returned by the Redis server.
Step S307: the returned data is converted into an object according to the metadata. Because the Redis database is key-value storage, wherein value is a data structure in string, hash, list, set or sorted set, after data returned by the Redis server is acquired, the returned data is converted into an object according to metadata. And, the converted object is sent to the caller. For example, the data of the HashMap structure is converted into a Java Bean object, and the Java Bean object is sent to the caller.
A mapping relation is established between Java object conversion and database records, and the prior art scheme is to serialize objects into a character string in an object serialization way and store the character string according to a simple structure of K-V. This simple storage structure does not take advantage of the properties of the various stored data structures that Redis databases can support. The embodiment of the invention provides a set of Object Relational Mapping (ORM) framework aiming at the storage of a Redis database supporting various structures. The ORM is a technology for solving the phenomenon of mutual mismatch between an object-oriented database and a relational database. In brief, ORM is the automatic persistence of objects in a program into a relational database through the use of metadata that describes the mapping between the objects and the database. Mapping metadata is employed to describe the mapping of object relationships so that the ORM middleware can act as a bridge between the business logic tier and the database tier of any one application.
The method for operating the Redis database can realize the mapping of the Java object and the Redis storage by calling the operation interface in the operation interface set. And obtaining metadata of the object, wherein the metadata comprises: key values of object storage, data structures of object storage, and data formats of fields of object. Because the operation interface in the operation interface set corresponds to the Redis database, the called operation interface and the metadata are used as parameters and can be mapped to the interface of the Redis database, so that the Redis database can be operated more conveniently and efficiently. By operating the Redis database in the mode, the mapping between the object and the storage of the Redis database is realized, various stored data structures of the Redis database are flexibly supported, the data structures stored by the object are defined in an annotation mode, the performance is improved, and the readability and the maintainability are enhanced. The method of the embodiment of the invention is light-weight and does not depend on other components; the storage related information of the data can be seen through the annotation of the object, and the readability and maintainability are strong; meanwhile, abundant data storage structures such as HashMap, Set, List, Queue and the like are supported, so that the Redis database is more flexibly operated, the performance is improved, and the readability and the maintainability are enhanced. Where a component is a reusable set of code that is capable of performing a certain function and providing several interfaces out that use this function. The interior of the component is cooperatively used for realizing the designated function by a plurality of classes, and meanwhile, the component externally exposes one or more interfaces for external calling. Plug-ins, frames, etc. are all within the category of components.
Fig. 4 is a schematic diagram of main modules of an apparatus for operating a Redis database according to an embodiment of the present invention, and as shown in fig. 4, the modules of an apparatus 400 for operating a Redis database according to an embodiment of the present invention mainly include:
the module 401: the operation interface module is used for storing the operation interface set; and the operation interfaces in the operation interface set correspond to the interfaces of the Redis database to be operated.
A module 402: the metadata acquisition module is used for acquiring metadata of the object; the metadata includes: the stored key value, the stored data structure, and the data format of each field. The metadata acquisition module is also used for judging whether the metadata of the object exists in the local cache; if yes, obtaining the metadata of the object from the local cache; otherwise, the metadata of the object is obtained from the annotation of the object through a reflection mechanism and is written into a local cache.
A module 403: the mapping module is used for mapping the operation interface and the metadata to an interface of the Redis database according to the called operation interface and the metadata, and calling the interface of the Redis database to realize the operation of the Redis database; wherein the called operation interface is included in the operation interface set. The operation interface set stored in the operation interface module comprises: adding operation interfaces, deleting operation interfaces, modifying operation interfaces and inquiring operation interfaces. Because the operation interfaces in the operation interface set are developed according to the Redis database to be operated, the called operation interfaces and the metadata of the object are used as parameters and can be mapped to the interfaces of the Redis database, and the operation on the Redis database can be realized by calling the mapped interfaces.
The device for operating the Redis database of the embodiment of the invention also comprises a key value generating module, which is used for determining that the key value stored by the object in the metadata does not exist, and generating the key value stored by the object according to the key value definition rule in the annotation of the object.
The device for operating the Redis database of the embodiment of the invention also comprises a format conversion module and a returned result processing module, wherein the format conversion module is used for performing format conversion on the object according to the data structure stored by the object in the metadata and the data format of each field of the object under the condition that the called operation interface is an added operation interface. And the returned result processing module is used for receiving the result returned by the Redis database and displaying the returned result. And the returned result processing module is also used for carrying out format conversion on the returned result according to the displayed format under the condition that the called operation interface is the query operation interface, wherein the displayed format is included in the metadata of the object. When the device for operating the Redis database executes the operation of writing data into the Redis database, because the data format stored in the Redis database is string, hash, list, set, sorted set or the like, the object is converted into the data format stored in the Redis database according to the annotation. After the Redis server finishes the operation, the result of successful writing or failure of writing can be returned, and after the result returned by the Redis server is received, the result can be sent to the caller.
A mapping relation is established between Java object conversion and database records, and the prior art scheme is to serialize objects into a character string in an object serialization way and store the character string according to a simple structure of K-V. This simple storage structure does not take advantage of the properties of the various stored data structures that Redis databases can support. The embodiment of the invention provides a set of Object Relational Mapping (ORM) framework aiming at the storage of a Redis database supporting various structures. The ORM is a technology for solving the phenomenon of mutual mismatch between an object-oriented database and a relational database. In brief, ORM is the automatic persistence of objects in a program into a relational database through the use of metadata that describes the mapping between the objects and the database. Mapping metadata is employed to describe the mapping of object relationships so that the ORM middleware can act as a bridge between the business logic tier and the database tier of any one application.
Through the annotation description object provided by the device provided by the embodiment of the invention, the object can be analyzed to obtain the metadata of the object; the metadata describes the selected storage structure, storage Key and field serialization mode of the object; and calling a programming interface of the Redis client according to an operation interface called by a developer in the operation interface set of the device and the object introduced into the annotation description, so that the Redis server finishes the operation corresponding to the operation instruction to be executed. The operation interface set comprises an adding operation interface, a deleting operation interface, a modifying operation interface and an inquiring operation interface. Taking calling the additional operation interface as an example, the device converts the object into a specified storage format, and takes the called additional operation interface of the device and the key value described by the object as parameters, and calls a programming interface of the Redis client, so that the Redis server finishes the operation corresponding to the operation instruction to be executed.
The Redis database is operated by the device for operating the Redis database, so that the mapping between the object and the storage of the Redis database is realized, various stored data structures of the Redis database are flexibly supported, the data structures stored by the object are defined in an annotation mode, the performance is improved, and the readability and the maintainability are enhanced. The method of the embodiment of the invention is light-weight and does not depend on other components; the storage related information of the data can be seen through the annotation of the object, and the readability and maintainability are strong; meanwhile, abundant data storage structures such as HashMap, Set, List, Queue and the like are supported, so that the Redis database is more flexibly operated, the performance is improved, and the readability and the maintainability are enhanced. Where a component is a reusable set of code that is capable of performing a certain function and providing several interfaces out that use this function. The interior of the component is cooperatively used for realizing the designated function by a plurality of classes, and meanwhile, the component externally exposes one or more interfaces for external calling. Plug-ins, frames, etc. are all within the category of components.
The embodiment of the invention provides electronic equipment. The electronic device of the embodiment of the invention comprises: one or more processors; storage means for storing one or more programs which, when executed by the one or more processors, cause the one or more processors to implement any of the above methods of operating a Redis database.
An embodiment of the present invention provides a computer-readable medium, on which a computer program is stored, where the computer program, when executed by a processor, implements any of the above methods for operating a Redis database.
Fig. 5 illustrates an exemplary system architecture 500 of a method of operating a Redis database or an apparatus for operating a Redis database to which embodiments of the present invention may be applied.
As shown in fig. 5, the system architecture 500 may include terminal devices 501, 502, 503, a network 504, and a server 505. The network 504 serves to provide a medium for communication links between the terminal devices 501, 502, 503 and the server 505. Network 504 may include various connection types, such as wired, wireless communication links, or fiber optic cables, to name a few.
The user may use the terminal devices 501, 502, 503 to interact with a server 505 over a network 504 to receive or send messages or the like. The terminal devices 501, 502, 503 may have installed thereon various communication client applications, such as shopping-like applications, web browser applications, search-like applications, instant messaging tools, mailbox clients, social platform software, etc. (by way of example only).
The terminal devices 501, 502, 503 may be various electronic devices having a display screen and supporting web browsing, including but not limited to smart phones, tablet computers, laptop portable computers, desktop computers, and the like.
The server 505 may be a server providing various services, such as a background management server (for example only) providing support for shopping websites browsed by users using the terminal devices 501, 502, 503. The background management server can analyze and process the received data such as the product information inquiry request and feed back the processing result to the terminal equipment.
It should be noted that the method for operating the Redis database provided by the embodiment of the present invention is generally executed by the server 505, and accordingly, a device for operating the Redis database is generally disposed in the server 505.
It should be understood that the number of terminal devices, networks, and servers in fig. 5 is merely illustrative. There may be any number of terminal devices, networks, and servers, as desired for implementation.
Referring now to FIG. 6, a block diagram of a computer system 600 suitable for use with a terminal device implementing an embodiment of the invention is shown. The terminal device shown in fig. 6 is only an example, and should not bring any limitation to the functions and the scope of use of the embodiments of the present invention.
As shown in fig. 6, the computer system 600 includes a Central Processing Unit (CPU)601 that can perform various appropriate actions and processes according to a program stored in a Read Only Memory (ROM)602 or a program loaded from a storage section 608 into a Random Access Memory (RAM) 603. In the RAM 603, various programs and data necessary for the operation of the system 600 are also stored. The CPU 601, ROM 602, and RAM 603 are connected to each other via a bus 604. An input/output (I/O) interface 605 is also connected to bus 604.
The following components are connected to the I/O interface 605: an input portion 606 including a keyboard, a mouse, and the like; an output portion 607 including a display such as a Cathode Ray Tube (CRT), a Liquid Crystal Display (LCD), and the like, and a speaker; a storage section 608 including a hard disk and the like; and a communication section 609 including a network interface card such as a LAN card, a modem, or the like. The communication section 609 performs communication processing via a network such as the internet. The driver 610 is also connected to the I/O interface 605 as needed. A removable medium 611 such as a magnetic disk, an optical disk, a magneto-optical disk, a semiconductor memory, or the like is mounted on the drive 610 as necessary, so that a computer program read out therefrom is mounted in the storage section 608 as necessary.
In particular, according to the embodiments of the present disclosure, the processes described above with reference to the 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 illustrated in the flow chart. In such an embodiment, the computer program may be downloaded and installed from a network through the communication section 609, and/or installed from the removable medium 611. The computer program performs the above-described functions defined in the system of the present invention when executed by the Central Processing Unit (CPU) 601.
It should be noted that the computer readable medium shown in the present invention can be a computer readable signal medium or a computer readable storage medium or any combination of the two. A computer readable storage medium may be, for example, but not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination of the foregoing. More specific examples of the 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 present invention, 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, a computer readable signal medium may include a propagated data signal with computer readable program code embodied therein, for example, in baseband or as part of a carrier wave. Such a propagated data signal may take many forms, including, but not limited to, electro-magnetic, optical, or any suitable combination thereof. 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, wire, fiber optic cable, RF, etc., or any suitable combination of the foregoing.
The flowchart 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 modules described in the embodiments of the present invention may be implemented by software or hardware. The described modules may also be provided in a processor, which may be described as: a processor comprises an operation interface module, a metadata acquisition module and a mapping module. Where the names of these modules do not in some cases constitute a limitation on the module itself, for example, an operation interface module may also be described as a "module storing a set of operation interfaces".
As another aspect, the present invention also provides a computer-readable medium that may be contained in the apparatus described in the above embodiments; or may be separate and not incorporated into the device. The computer readable medium carries one or more programs which, when executed by a device, cause the device to comprise: obtaining metadata of an object, the metadata including: the stored key value, the stored data structure and the data format of each field; mapping to an interface of a Redis database according to the called operation interface and the metadata, and calling the interface of the Redis database to realize the operation of the Redis database; the called operation interface is included in the operation interface set, and the operation interface in the operation interface set corresponds to an interface of the Redis database.
According to the technical scheme of the embodiment of the invention, the mapping between the Java object and the Redis storage can be realized by calling the operation interface in the operation interface set. And acquiring metadata of the object, wherein the metadata at least comprises a key value stored by the object, a data structure stored by the object and a data format of each field of the object. Because the operation interface in the operation interface set corresponds to the Redis database to be operated, the called operation interface and the metadata are used as parameters and can be mapped to the interface of the Redis database, so that the Redis database can be operated more conveniently and efficiently. By operating the Redis database in the mode, the mapping between the object and the storage of the Redis database is realized, various stored data structures of the Redis database are flexibly supported, the data structures stored by the object are defined in an annotation mode, the performance is improved, and the readability and the maintainability are enhanced. The method of the embodiment of the invention is light-weight and does not depend on other components; the storage related information of the data can be seen through the annotation of the object, and the readability and maintainability are strong; meanwhile, abundant data storage structures such as HashMap, Set, List, Queue and the like are supported, so that the Redis database is more flexibly operated, the performance is improved, and the readability and the maintainability are enhanced.
The above-described embodiments should not be construed as limiting the scope of the invention. Those skilled in the art will appreciate that various modifications, combinations, sub-combinations, and substitutions can occur, depending on design requirements and other factors. Any modification, equivalent replacement, and improvement made within the spirit and principle of the present invention should be included in the protection scope of the present invention.

Claims (14)

1. A method of operating a Redis database, comprising:
obtaining metadata of an object, the metadata comprising: the stored key value, the stored data structure and the data format of each field;
mapping to an interface of a Redis database according to the called operation interface and the metadata, and calling the interface of the Redis database to realize the operation of the Redis database; the called operation interface is included in an operation interface set, and the operation interface in the operation interface set corresponds to an interface of the Redis database;
the step of obtaining metadata of an object comprises:
judging whether the metadata of the object exists in a local cache or not;
if yes, obtaining the metadata of the object from a local cache; otherwise, obtaining the metadata of the object from the annotation of the object through a reflection mechanism, and writing the metadata into a local cache.
2. The method of claim 1, after obtaining metadata of the object and before mapping to an interface of a Redis database, further comprising:
and if the key value stored in the metadata does not exist, generating the stored key value according to the key value definition rule in the annotation of the object.
3. The method of claim 1, wherein the set of operational interfaces comprises: adding operation interfaces, deleting operation interfaces, modifying operation interfaces or inquiring operation interfaces.
4. The method of claim 3, after obtaining metadata of the object and before mapping to the interface of the Redis database, further comprising:
and under the condition that the called operation interface is an added operation interface, performing format conversion on the object according to the data structure stored in the metadata and the data format of each field.
5. The method of claim 3, after mapping to an interface of a Redis database and invoking the interface of the Redis database to implement the operation on the Redis database, further comprising:
and receiving a result returned by the Redis database, and displaying the returned result.
6. The method of claim 5, wherein the metadata further comprises a format of the display; then
After receiving the result returned by the Redis database and before displaying the returned result, the method further includes: and under the condition that the called operation interface is the query operation interface, performing format conversion on the returned result according to the displayed format.
7. An apparatus for operating a Redis database, comprising:
the operation interface module is used for storing the operation interface set; the operation interfaces in the operation interface set correspond to interfaces of a Redis database;
the metadata acquisition module is used for acquiring metadata of the object; the metadata includes: the stored key value, the stored data structure and the data format of each field;
the mapping module is used for mapping the operation interface to an interface of a Redis database according to the called operation interface and the metadata, and calling the interface of the Redis database to realize the operation of the Redis database; wherein the called operation interface is included in an operation interface set;
the metadata acquisition module is further used for judging whether the metadata of the object exists in the local cache; if yes, obtaining the metadata of the object from a local cache; otherwise, obtaining the metadata of the object from the annotation of the object through a reflection mechanism, and writing the metadata into a local cache.
8. The apparatus of claim 7, further comprising a key value generation module configured to generate a stored key value according to a key value definition rule in the annotation of the object if it is determined that the key value stored in the metadata does not exist.
9. The apparatus of claim 7, wherein the set of operation interfaces stored in the operation interface module comprises: adding operation interfaces, deleting operation interfaces, modifying operation interfaces and inquiring operation interfaces.
10. The apparatus of claim 9, further comprising a format conversion module, configured to, if the called operation interface is an add operation interface, perform format conversion on the object according to a data format of each field and a data structure stored in the metadata.
11. The apparatus of claim 7, further comprising a returned result processing module, configured to receive a result returned by the Redis database, and display the returned result.
12. The apparatus of claim 11, wherein the returned result processing module is further configured to perform format conversion on the returned result according to a displayed format, where the called operation interface is a query operation interface, and the displayed format is included in the metadata.
13. An electronic device, comprising:
one or more processors;
a storage device for storing one or more programs,
when executed by the one or more processors, cause the one or more processors to implement the method of any one of claims 1-6.
14. A computer-readable medium, on which a computer program is stored, which, when being executed by a processor, carries out the method according to any one of claims 1-6.
CN201711477196.3A 2017-12-29 2017-12-29 Method and device for operating Redis database Active CN110109983B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN201711477196.3A CN110109983B (en) 2017-12-29 2017-12-29 Method and device for operating Redis database

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN201711477196.3A CN110109983B (en) 2017-12-29 2017-12-29 Method and device for operating Redis database

Publications (2)

Publication Number Publication Date
CN110109983A CN110109983A (en) 2019-08-09
CN110109983B true CN110109983B (en) 2021-11-09

Family

ID=67482992

Family Applications (1)

Application Number Title Priority Date Filing Date
CN201711477196.3A Active CN110109983B (en) 2017-12-29 2017-12-29 Method and device for operating Redis database

Country Status (1)

Country Link
CN (1) CN110109983B (en)

Families Citing this family (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN110727727B (en) * 2019-10-15 2023-01-10 深圳前海微众银行股份有限公司 Statistical method and device for database
CN112052263A (en) * 2020-07-13 2020-12-08 浙江大搜车软件技术有限公司 Method, system, computer device and readable storage medium for requesting instruction processing
CN113672771A (en) * 2021-09-06 2021-11-19 京东科技控股股份有限公司 Data entry processing method and device, medium and electronic equipment

Citations (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN105468717A (en) * 2015-11-20 2016-04-06 北京百度网讯科技有限公司 Database operation method and device
CN107169032A (en) * 2017-04-17 2017-09-15 北京五八信息技术有限公司 Database operation method and database server

Family Cites Families (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CA2954037A1 (en) * 2016-01-21 2017-07-21 Wal-Mart Stores, Inc. Codeless information service for abstract retrieval of disparate data

Patent Citations (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN105468717A (en) * 2015-11-20 2016-04-06 北京百度网讯科技有限公司 Database operation method and device
CN107169032A (en) * 2017-04-17 2017-09-15 北京五八信息技术有限公司 Database operation method and database server

Also Published As

Publication number Publication date
CN110109983A (en) 2019-08-09

Similar Documents

Publication Publication Date Title
CN108920222B (en) Business processing method and device based on rule engine
CN108846753B (en) Method and apparatus for processing data
CN110263277B (en) Page data display method, page data updating device, page data equipment and storage medium
CN111400061A (en) Data processing method and system
CN110109983B (en) Method and device for operating Redis database
CN110858202A (en) Method and device for generating where clause in database query statement
CN111666293A (en) Database access method and device
CN113626223A (en) Interface calling method and device
CN113641700A (en) Data processing method and device based on Spring boot frame
CN111125064A (en) Method and device for generating database mode definition statement
CN113190517B (en) Data integration method and device, electronic equipment and computer readable medium
CN113760961B (en) Data query method and device
CN108959294B (en) Method and device for accessing search engine
CN110764769B (en) Method and device for processing user request
US10908924B2 (en) System and methods for loading objects from hash chains
CN115687815A (en) Page information display method, device, equipment and medium
CN113760969A (en) Data query method and device based on elastic search
US10652341B2 (en) Restful interface system for an application
CN113760240A (en) Method and device for generating data model
CN113779018A (en) Data processing method and device
CN112214500A (en) Data comparison method and device, electronic equipment and storage medium
US10884937B2 (en) Reference cache maintenance optimizer
CN111143408B (en) Event processing method and device based on business rule
CN113779078A (en) Cache data acquisition method and device
CN115309404A (en) File generation method and device, electronic equipment and storage medium

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
GR01 Patent grant
GR01 Patent grant