In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces you how to call Redis in Java, the content is very detailed, interested friends can refer to, hope to be helpful to you.
Call the relevant methods of Redis
1. String data Type (String) API description
String type is the most basic data storage type in Redis, and it is binary safe in Redis, which means that this type can accept data in any format, such as JPEG image data or Json object description information. The maximum length of data that a string type Value can hold in Redis is 512m.
# set the Key to hold the specified string Value, and overwrite its original value if the Key already exists.
Void set (finalString key, finalString value)
# get the Value of the specified Key, and return null if the Key does not exist.
Byte [] get (finalString key)
# determine whether the key exists. If it exists, it returns 1, otherwise 0.
Booleanexists (final String key)
/ / Delete the specified Key
Longdelete (final String... Keys)
/ / rename the specified Key. If the commands of the two Keys in the parameters are the same, or the source Key does not exist, the command will return the relevant error message. If newKey already exists, it is overwritten directly.
Voidrename (final String oldkey, final String newkey)
/ / if the new value does not exist, change the original value in the parameter to the new value. Other conditions are consistent with RENAME.
Booleanrenamenx (final String oldkey, final String newkey)
/ / sets the expiration time (in seconds) of a key. After that time, the Key is automatically deleted. If the Key is modified before the timeout, the timeout associated with the key is removed.
Booleanexpire (final String key, final int seconds)
# EXPIREAT is similar to EXPIRE in that it is used to set the time to live for key. The difference is that the time parameter accepted by the EXPIREAT command is the UNIX timestamp (unixtimestamp).
BooleanexpireAt (final String key, final long unixTime)
# check the remaining survival time (in seconds) of the specified Key through the ttl command. 0 means it has expired and-1 means it will never expire. Long ttl (final Stringkey)
# move the mysetkey key from the current database to the database whose ID is dbIndex
Booleanmove (final String key, final int dbIndex)
# atomicity sets the Key to the specified Value, returns the original value of the Key, and returns null if the Key does not exist before.
Byte [] getSet (final String key, final String value)
# returns a list of Values for the specified Keys.
List mget (final String... Keys)
# if the specified Key does not exist, set the Key to hold the specified string Value, which is equivalent to the SET command. Conversely, if the Key already exists, the command does nothing and returns.
Booleansetnx (final String key, final String value)
Booleansetex (final String key, final int seconds, final String value)
# this command atomically completes the setting operation of all key/value in the parameters. If any Key already exists in this batch of Keys, then all the operations will be rolled back, that is, all changes will not take effect.
Booleanmsetnx (final String... Keysvalues)
# will specify an increment of 1 for the Value atomicity of Key. If the Key does not exist, its initial value is 0, and its value is 1 after incr, which returns the incremented value.
VoidincrBy (final String key, final long integer) {
Void incr (finalString key)
# will specify that the Value atomicity of the Key is decremented by 1. If the Key does not exist, its initial value is-1, and its value is 1 after incr, returning the decremented value.
LongdecrBy (final String key, final long integer)
Long decr (finalString key)
# if the Key already exists, the APPEND command appends the data of the parameter Value to the end of the existing Value. If the Key does not exist, the APPEND command will create a new Key/Value. Returns the length of the appended Value.
Longappend (final String key, final String value)
two。 Character list (List) data type interface description
In Redis, the List type is a linked list of strings sorted in the order of insertion. Like a normal linked list in a data structure, we can add new elements to its header (left) and tail (right). On insertion, if the key does not exist, Redis creates a new linked list for the key. In contrast, if all elements in the linked list are removed, the key will also be deleted from the database. The maximum number of elements that can be contained in a List is 4294967295 (about 4.2 billion).
# insert all the Values given in the tail parameter of the List Value associated with the specified Key. If the Key does not exist, the command creates an empty linked list associated with the Key before insertion, and then inserts the data from the tail of the linked list. If the Value of the key is not a linked list type, the command returns the relevant error message.
Void rpush (finalString key, final String... String)
# all Values given in the header insertion parameter of the List Value associated with the specified Key. If the Key does not exist, the command creates an empty linked list associated with the Key before insertion, and then inserts the data from the header of the linked list. If the Value of the key is not a linked list type, the command returns the relevant error message.
Void lpush (finalString key, final String... String)
# returns the number of elements in the linked list associated with the specified Key, or 0 if the Key does not exist. If the type of Value associated with the Key is not a linked list, the relevant error message is returned.
Long llen (finalString key)
# returns a list of elements in the specified range. The parameters start and end of this command are both 0-based. That is, 0 represents the first element of the linked list header (leftmost). Where the value of start can also be negative,-1 will represent the last element in the linked list, the tail element,-2 will represent the penultimate element, and so on. When this command fetches elements, the elements in the start and end positions are also taken out. If the value of start is greater than the number of elements in the linked list, the empty linked list will be returned. If the value of end is greater than the number of elements, the command gets all the remaining elements in the linked list starting with start (including start).
Listlrange (final String key, final long start, final long end)
# this command retains only the elements within the specified range, thus ensuring that the number of elements in the link is relatively constant. The start and stop parameters are both 0 words based on 0 indicating the header element. Like other commands, start and stop can also be negative values, with-1 representing the trailing element. If the start is greater than the tail of the linked list, or if the start is greater than stop, the command returns an empty linked list, and the Key is deleted. If the stop is greater than the number of elements, all remaining elements from start are retained.
Void ltrim (finalString key, final long start, final long end)
# this command returns the element at the specified position (index) in the linked list. Index is 0-based, representing the header element, and if index is-1, the trailing element. If the Key is not associated with a linked list, the command returns the relevant error message.
Byte [] lindex (final String key, final long index)
# sets the value of the specified position in the linked list to the new value, where 0 represents the first element, the header element, and-1 represents the trailing element. If the index value Index exceeds the number of elements in the linked list, the command returns the relevant error message.
Void lset (finalString key, final long index, finalString value)
# in the linked list associated with the specified Key, delete the elements whose previous count values are equal to value. If count is greater than 0, iterate through and delete, if count is less than 0, traverse and delete from tail to head. If count equals 0, all elements in the linked list that are equal to value are deleted. If the specified Key does not exist, 0 is returned directly, and the number of elements deleted is returned.
Long lrem (finalString key, long count, finalString value)
# returns and pops up the first element in the linked list associated with the specified Key, the header element. If the Key is not saved, return null.
Byte [] lpop (final String key)
# returns and pops up the last element in the linked list associated with the specified Key, the trailing element. If the Key is not saved, return nil.
Byte [] rpop (final String key)
# atomically pops an element from the end of the linked list associated with the srckey key, while inserting the popup element into the head of the linked list associated with the dstkey key. If the srckey key does not exist, the command returns null without doing anything else. If srckey and dstkey are the same key, it is equivalent to atomically moving the tail element of its associated linked list to the head of the linked list.
Byte [] rpoplpush (final String srckey, final String dstkey)
3. Description of Hashes data type API
We can think of the Hashes type in Redis as a map container with String Key and String Value. So this type is very suitable for storing information about value objects. Such as Username, Password and Age. If the Hash contains very few fields, then this type of data will take up only a small amount of disk space. Each Hash can store 4294967295 key-value pairs.
# sets the Field/Value pair for the specified Key. If the Key does not exist, the command will create a new Key with the Field/Value pair in the parameter, and if the Field in the parameter already exists in the Key, overwrite its original value with the new value.
Booleanhset (final String key, final String field, final String value)
# returns the associated value of Field in the parameter, and returns null if the Key or Field in the parameter does not exist.
Void hget (finalString key, finalString field)
# only if the Key or Field in the parameter does not exist, set the Field/Value pair for the specified Key, otherwise the command will not do anything.
Voidhsetnx (final String key, final String field, final String value)
# set the Field/Value pairs given in the parameters one by one. If one of the Field already exists, overwrite the original value with the new value. If Key does not exist, create a new Key and set the Field/Value in the parameters.
Void hmset (finalString key, final Map hash)
# gets a set of Values associated with the specified Fields in the parameter. If the requested Field does not exist, its value returns null. If Key does not exist, the command treats it as an empty Hash, so it returns a set of null.
Byte [] hmget (finalString key, final String... Fields)
# increase the value of the Value associated with the specified Field in the specified Key. If Key or Field does not exist, the command creates a new Key or Field, initializes its associated Value to 0, and then specifies a numeric increment operation. Returns the value after operation
LonghincrBy (final String key, final String field, final long value)
# determine whether the specified Field exists in the specified Key.
Void hexists (finalString key, finalString field)
# multiple fields specified in the parameters are deleted from the Hashes Value of the specified Key. Fields that do not exist will be ignored. If Key does not exist, it is treated as an empty Hashes and returns 0. 0. Returns the number of Field actually deleted.
Void hdel (finalString key, final String... Fields)
# get the number of Field contained in the Key.
Void hlen (finalString key)
# returns all Fields names of the specified Key.
List hkeys (final String key)
# returns all Values names of the specified Key.
List hvals (final String key)
# get all the Field/Value contained in the key. The return format is one Field, one Value, and so on.
Map hgetAll (final String key)
4. Character set (Set) data type interface description
In Redis, we can think of a Set type as a collection of unsorted characters, and like the List type, we can add, delete, or determine the existence of an element on the data value of that type. It should be noted that the time complexity of these operations is O (1), that is, the second operation is completed in a constant time. The maximum number of elements that a Set can contain is 4294967295.
Unlike the List type, duplicate elements are not allowed in the Set collection, which is exactly the same as the set container in the C++ standard library. In other words, if you add the same element multiple times, only one copy of that element will be retained in Set. Compared with the List type, the Set type also has a very important functional feature, that is, it completes the aggregate computing operations between multiple Sets on the server side, such as unions, intersections and differences. Because these operations are done on the server side, it is extremely efficient and saves a lot of network IO overhead.
# if used during the insertion process, some members of the parameters already exist in the Set, that member will be ignored, while other members will still be inserted normally. If the Key does not exist before the command is executed, the command creates a new Set and then inserts the members of the parameter one after another. If the Value of the Key is not of type Set, the command returns the relevant error message.
Booleansadd (final String key, final String... Members)
# get all the members of the Set associated with the Key.
List smembers (final String key)
# Delete the member specified in the parameter from the Set associated with Key, and the parameter member that does not exist will be ignored. If the Key does not exist, it will be treated as an empty Set.
Void srem (finalString key, final String... Members)
# randomly remove and return a member of the Set. Because the layout of elements in Set is not externally controlled, it is not possible to determine which element is at the head or tail of the Set, as List does.
Byte [] spop (finalString key)
# atomically move the members of the parameter from the srckey key to the Set associated with the dstkey key. So at some point, the member appears either in source or in dstkey. If the member does not exist in srckey, the command will not do anything and return 0, otherwise, the member will be moved from srckey to dstkey. If the member already exists in the dstkey, the command simply removes the member from the srckey. If the Value associated with Key is not Set, the relevant error message is returned.
Booleansmove (final String srckey, final String dstkey,final String member)
# get the number of members in Set.
Long scard (finalString key)
# determines whether the specified member in the parameter already exists in the Set collection associated with Key.
Booleansismember (final String key, final String member)
# this command returns the intersection of all members in the Sets associated with Keys in the parameter. So if the Set associated with any of the Key in the parameter is empty, or if a Key does not exist, the result of the command will be an empty set.
List sinter (final String... Keys)
# this command and the sinter command are functionally identical, except that sinter returns the resulting members of the intersection, which is stored in the Set associated with the dstkey. If the dstkey key already exists, the operation will overwrite its members.
Voidsinterstore (final String dstkey, final String... Keys)
# this command returns the union of all members in the Sets associated with Keys in the parameter.
List sunion (final String... Keys)
# this command and the sunion command are functionally identical, except that sunion returns the resulting member of the union, which stores the union member in the Set associated with dstkey. If the dstkey key already exists, the operation will overwrite its members.
Voidsunionstore (final String dstkey, final String... Keys)
# returns the difference between the Set associated with the first Key in the parameter and the members of the Sets associated with all subsequent Keys. If Key does not exist, it is considered empty Set.
Listsdiff (final String... Keys)
# this command and the SDIFF command are functionally identical, except that SDIFF returns the resulting member of the difference, which stores the difference member in the Set associated with dstkey. If the dstkey key already exists, the operation will overwrite its members.
Voidsdiffstore (final String dstkey, final String... Keys)
# like SPOP, a member of the Set is returned randomly, except that the command does not delete the returned member.
Voidsrandmember (final String key)
5. Ordered set (Sorted-Sets) data type interface description
The Sorted-Sets and Sets types are very similar in that they are both collections of strings and do not allow duplicate members to appear in a Set. The main difference between them is that each member of the Sorted-Sets has a score associated with it. Redis uses scores to sort the members of the collection from small to large. However, it is important to note that although the members of the Sorted-Sets must be unique, the score is repeatable.
Adding, deleting, or updating a member in Sorted-Set is a very fast operation, and its time complexity is the logarithm of the number of members in the collection. Because the members in the Sorted-Sets are orderly in the collection, it is still very efficient to access the members in the middle of the collection. In fact, this feature of Redis is difficult to achieve in many other types of databases, in other words, it is very difficult to model in other databases to achieve the same efficiency as Redis at this point.
# in this command, we can specify multiple sets of score/member as parameters. If a member of the parameter already exists when it is added, the command updates the member's score to the new value and reorders the member based on the new value. If the key does not exist, the command creates a new Sorted-Sets Value for the key and inserts the score/member pair into it. If the key already exists, but the Value associated with it is not of type Sorted-Sets, the associated error message will be returned.
Booleanzadd (final String key, final double score, final String member)
# this command returns members in order within the range specified by the parameters start and stop, where the start and stop parameters are both 0-based, where 0 represents the first member and-1 represents the last member. If start is greater than the maximum index value in the Sorted-Set, or start > stop, an empty collection will be returned. If the stop is greater than the maximum index value, the command returns the last member from the start to the collection. If the command has the optional parameter WITHSCORES option, the command will include the score value of each member, such as value1,score1,value2,score2..., in the returned results.
List zrange (final String key, final long start,final long end)
# this command will remove the members specified in the parameter, and members that do not exist will be ignored. If the Value associated with the Key is not Sorted-Set, the corresponding error message will be returned.
Booleanzrem (final String key, final String... Members)
# this command increases the specified score for the specified member in the specified Key. If a member does not exist, the command adds the member and assumes that its initial score is 0, and then adds its score to increment. If the Key does not exist, the command creates the Key and its associated Sorted-Sets and contains the members specified by the parameter, with a score of the increment parameter. If the Key is not associated with a Sorted-Sets type, the associated error message will be returned.
Doublezincrby (final String key, final double score,final String member)
The members in # Sorted-Set are stored in order of score from lowest to highest, and this command returns the position value of the specified member in the parameter, where 0 represents the first member, which has the lowest score in the Sorted-Set.
Long zrank (finalString key, finalString member)
The function of this command is basically the same as that of ZRANK, except that the index obtained by this command is sorted from high to low, and 0 represents the first element, that is, the member with the highest score.
Longzrevrank (final String key, final String member)
# the function of this command is basically the same as that of ZRANGE, except that the command obtains the members of the specified location through reverse sorting, that is, from high to low. If the members have the same score, they are sorted in descending dictionary order.
Listzrevrange (final String key, final long start, final long end)
# this command will return all members whose scores are between min and max, that is, the expression min
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.