Five types of data and basic operations in Redis

Source: Internet
Author: User
Tags delete key redis

Redis supports 5 types of data, namely String,list,hash,set,zset and Hash.
  
Redis is a high-performance key-value database that introduces key-related commands before introducing its 5 data types.
   key-Key

You typically manage Redis keys with the following key commands:
  
   1 DEL Key
This command is used to delete key when key exists.
   2 DUMP Key
Serializes the given key and returns the serialized value.
   3 EXISTS Key
Checks whether a given key exists.
   4 EXPIRE key seconds
Sets the expiration time for the given key.
   5 expireat key timestamp
The Expireat function is similar to expire and is used to set the expiration time for the key. The difference is that the time parameter that the Expireat command accepts is the UNIX timestamp (Unix timestamp).
   6 Pexpire key milliseconds
Sets the expiration time for a key in milliseconds.
   7 pexpireat Key Milliseconds-timestamp
Set the time stamp for key expiration (Unix timestamp) in milliseconds
   8 KEYS pattern
Finds all keys that conform to the given pattern (patterns).
   9 Move Key DB
Moves the key of the current database to the given database db.
   Ten PERSIST Key
Key will be persisted when the key is removed from its expiration time.
   One pttl key
Returns the remaining expiration time for the key in milliseconds.
   TTL Key
Returns the remaining lifetime of the given key (Ttl,time to live) in seconds. Return-1 means never expire, and return-2 indicates that it has expired.
   Randomkey
Returns a key randomly from the current database.
   RENAME Key Newkey
Change the name of a key
   Renamenx Key Newkey
Rename the key to Newkey only if the Newkey does not exist.
   TYPE Key
Returns the type of the value stored by the key.string-String

The string type is the most basic data type of redis, and it is binary safe, that is, the Redis string type can hold any data, such as a picture or a serialized object.
The following are the related commands for managing string type data:
  
   1 SET Key value
Sets the value of the specified key
   2 Get key
Gets the value of the specified key.
   3 getrange key start end
Returns the substring of the string value in the key. (Subscript starting from 0, GetRange key 0-1 means returning the entire string)
   4 Getset Key value
Set the value of the given key to value and return the old value of the key.
   5 getbit key offset
Gets the bits (bit) on the specified offset for the string value stored by the key.
   6 mget Key1 [key2 ...]
Gets the value of all (one or more) given key.
   7 setbit key offset value
Sets or clears the bits (bit) on the specified offset for the string value stored by the key.
   8 Setex key seconds value
Associates the values value with the key and sets the key expiration time to seconds (in seconds).
   9 setnx Key value
The value of the key is set only if the key does not exist.
   Ten SETRANGE key offset value
The value parameter is used to override the string values stored by the fixed key, starting with offset offsets.
   One STRLEN key
Returns the length of the string value stored by the key.
   mset key value [key value ...]
Set one or more key-value pairs at the same time.
   msetnx key value [key value ...]
Set one or more key-value pairs at the same time, if and only if all the given keys do not exist to set success.
   Psetex key milliseconds value
This command is similar to the Setex command, but it sets the lifetime of the key in milliseconds, rather than in seconds, as the Setex command does.
   INCR Key
Increases the number of values stored in the key by one.
   Incrby Key Increment
Adds the value stored by the key to the given increment value (increment).
   incrbyfloat Key Increment
Adds the value stored by the key to the given floating-point increment value (increment).
   DECR Key
Reduce the number of values stored in the key by one.
   Decrby Key Decrement
The value stored by the key subtracts the given decrement value (decrement).
   APPEND Key Value
If the key already exists and is a string, the APPEND command appends value to the end of the key's original value.
  
   list-List

List type lists are simple string lists that are sorted in order of insertion, can add an element to the head (left) of the list, or the tail (right), and the bottom is actually a linked list.
The related commands for the list data type are as follows:
  
   1 Blpop key1 [Key2] timeout
Removes and gets the first element of the list, if no elements block the list until the wait time out or the discovery of an element that can eject.
   2 Brpop key1 [Key2] timeout
Moves out and gets the last element of the list, if no elements block the list until the wait time out or the discovery of a popup element.
   3 Brpoplpush Source Destination Timeout
POPs a value from the list, inserts the pop-up element into another list, and returns it; if no elements in the list block the list until the wait time out or the discovery of a popup element.
   4 lindex Key index
Get elements from a list by index
   5 Linsert Key before| After pivot value
Inserts an element before or after the element in the list
   6 Llen Key
Get list length
   7 Lpop Key
Move from the head of the list and get the first element
   8 lpush key value1 [value2]
Inserts one or more values into the list header
   9 lpushx Key value
Inserts one or more values into the existing list header
   Ten Lrange key start Stop
Gets the element within the specified range of the list, Lrange key 0-1 represents the entire element
   Lrem Key Count value
To remove a list element with a specified number and value starting from the head of the list
   LSET Key index value
To set the value of a list element through an index
   LTRIM key Start Stop
Trim a list (trim), which means that the list retains only the elements within the specified range, and elements that are not within the specified range are deleted.
   Rpop Key
Move from the tail of the list and get the first element
   Rpoplpush Source Destination
Removes an element from the end of the list, puts it in the head of another list, and returns.
   rpush key value1 [value2]
Inserts one or more values into the tail of the list
   rpushx Key Value
Inserts one or more values into the tail of an existing list
  
Action Example:
  
  
   hash-Hash

The Redis hash type is a mapping between a string field and a string value, so it is ideal for storing objects.
  
The related commands for the hash type are as follows:
  
   1 Hdel key field2 [Field2]
Delete one or more hash fields
   2 hexists key field
To determine whether a hash field exists or not
   3 hget key field
Gets the value stored in the specified key hash field
   4 Hgetall Key
Let all fields and values be stored in a hash at the specified key
   5 hincrby key field increment
The integer value of the hash field that is incremented by the given number
   6 hincrbyfloat key field increment
Floating-point value for the given Increment hash field
   7 Hkeys Key
Gets all the hash fields in the
   8 Hlen Key
Get the number of hash fields
   9 hmget key field1 [Field2]
Gets the value of all the given hash fields
   hmset key field1 value1 [Field2 value2]
Set multiple values for multiple hash fields
   One hset key field value
Set the string value of a hash field
   hsetnx key field value
Sets the value of a hash field, only if the field does not exist
   hvals Key
Get all the values in the hash
   hscan key cursor [MATCH pattern] [count Count]
Incremental iteration hash field and related values
  
Action Example:
  
  
   set-Collection

The set of Redis is a unordered collection of string types. Collection members are unique, which means that duplicate data cannot occur in the collection.
The collection in Redis is implemented by a hash table, so the complexity of adding, deleting, and finding is O (1).
  
The related commands for the set type are as follows:
  
   1 sadd key member1 [Member2]
To add one or more members to a collection
   2 SCard Key
Gets the number of members of the collection
   3 Sdiff key1 [Key2]
Returns the difference set for a given set of all
   4 sdiffstore destination key1 [Key2]
Returns the difference set for a given set of all sets and is stored in destination
   5 sinter key1 [Key2]
Returns the intersection of all collections given
   6 Sinterstore destination Key1 [Key2]
Returns the intersection of the given set and is stored in the destination
   7 sismember Key member
To determine if the member element is part of a collection key
   8 smembers Key
Return all members in a collection
   9 Smove Source Destination member
Move member elements from the source collection to the destination collection
   Ten Spop Key
Removes and returns a random element in the collection
   One srandmember key [Count]
Returns one or more random numbers in a collection
   srem Key Member1 [Member2]
Remove one or more members from a collection
   sunion key1 [Key2]
Returns the set of all the given sets
   sunionstore Destination Key1 [Key2]
The collection of all the given sets is stored in the destination collection
   sscan key cursor [MATCH pattern] [count Count]
Iterating through the elements of a collection
  
Action Example:
  
   zset-ordered set

Redis ordered set Zset is similar to set, except that each element is associated with a double type of score. Redis is the sort of small to large members of a collection through fractions.
The members of an ordered set are unique, but fractions (score) can be repeated.
  
The related operation commands for the Zset type are as follows:
  
   1 zadd key score1 member1 [Score2 member2]
Add one or more members to an ordered collection, or update the scores of existing members
   2 Zcard Key
Gets the number of members in an ordered collection
   3 zcount key min Max
Calculates the number of members that specify interval fractions in an ordered set
   4 Zincrby key increment member
Adds an increment to the fraction of a specified member in an ordered set increment
   5 Zinterstore destination Numkeys key [key ...]
Computes the intersection of the given one or more ordered sets and stores the result set in the new ordered set key
   6 zlexcount key min Max
Calculates the number of members in a specified dictionary interval in an ordered set
   7 zrange key start stop [Withscores]
Returns an ordered set of members within a specified interval through an index interval
   8 zrangebylex key min Max [LIMIT offset Count]
The members of an ordered set are returned through the dictionary interval, and the points in the ordered set must be the same, and the Min and Max should be added [] or () respectively, to include or not contain, or to use-and + to represent the minimum and maximum values of the score. Where the limit limit outputs count elements backwards from the offset index of the specified interval
   9 zrangebyscore Key min Max [withscores] [LIMIT]
Returns the members within a specified interval of an ordered set by a fraction
   Ten Zrank key member
Returns the index of a specified member in an ordered collection
   One zrem key member [member ...]
Remove one or more members from an ordered collection
   zremrangebylex Key min Max
Removes all members of a given dictionary interval in an ordered collection
   Zremrangebyrank key Start Stop
Removes all members of a given ranking interval in an ordered collection
   zremrangebyscore Key min Max
Removes all members from a given fractional interval in an ordered set
   zrevrange key start stop [Withscores]
Returns the members of a specified interval in an ordered set by indexing the score from the high end
   zrevrangebyscore key Max min [Withscores]
Returns the members of a specified fractional interval in an ordered set, sorted from highest to lowest score
   Zrevrank Key member
Returns the rank of a specified member in an ordered set, with ordered set members descending by fractional values (from large to small)
   Zscore Key member
Returns the fractional value of a member in an ordered set
   zunionstore Destination Numkeys key [key ...]
Calculates the set of one or more ordered sets of a given set and stores them in a new key
   zscan key cursor [MATCH pattern] [count Count]
An element in an ordered set of iterations (including element members and element scores)
  
Usage examples:
  
  
  
  

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.