Data types and operations for Redis

Source: Internet
Author: User
Tags key string

    • Strings

The simplest type, a key corresponding to a value,string type is binary safe. A Redis string can contain any data, slice, or serialized object

    • Operation
      • Set: Sets the value of the key corresponding to a string of type value, such as set name Fahy
      • SETNX: Sets the value of the key corresponding to string type value, if key already exists, returns 0, such as setnx name Fahy
      • Setex: Sets the value of the key corresponding to string type value and specifies the validity period of this key-value pair, such as Setex name Fahy
      • Setrange: Sets the substring of the value of the specified key, replacing characters of the same length, if a pair of key-value is [email protected], Setrange email 8 qq.com will make [email protected]
      • Mset: Setting the value of multiple keys at once, successfully returning OK indicates that the value is set, and failure returns 0 to indicate that no value is set, such as Mset Key1 Huangweijian key2 Others
      • MSETNX: Setting the value of multiple keys at once, successfully returning OK indicates that all values are set, failure returns 0 means no value is set, but does not overwrite the key that exists, such as Msetnx Key3 Huangweineng key4 Yellow
      • Get: Gets the value corresponding to key, if key does not exist, returns Nill, such as Get Key1
      • Getset: Sets the value of key and returns the old value of key, such as Getset Key2 Huangsurong
      • GetRange: Gets a substring of the value of key, such as GetRange Key1 0 4, which returns from No. 0 to 4th characters "Huang"
      • Mget: Gets the value of more than one key at a time, and returns nil if the corresponding key does not exist, such as Mget Key1 Key2 Key3
      • INCR: Do the + + operation on the value of key, and return the new value, when key does not exist, set the key value to 0 and then add the operation, such as INCR Key5
      • Incrby: The value of the key is added to the specified value operation, when key does not exist, set the key value of 0 and then add operations, such as Incrby Key6 7
      • DECR: The value of key is done-operation, when key does not exist, set the key value of 0 and then reduce the operation, such as DECR Key7
      • Decrby: The value of the key is reduced by the specified value operation, when key does not exist, set the key value of 0 and then reduce the operation, such as Decrby Key8 7
      • Append: Appends value to the specified key string, returns the length of the new string value, and when key does not exist, adds the key, such as append key1 @sina. com
      • Strlen: Returns the value length of the specified key, such as Strlen Key1
      • Del: Delete the specified key, such as Del Key1
  • Hashes: Can create a hash table

The Redis hash is a string field (field) and a mapping table of value, and its addition and deletion operations are all O (I) (average). Ideal for storing objects, consuming less memory, and saving the entire object

    • action
      • hset: Set hash field to the specified value, if key does not exist, create first, such as Hset hashtabl e field Hello
      • hsetnx: Sets the hash field to the specified value and if key does not exist, it is created first if there is a return of 0, such as Hsetnx Hashtable field1 World
      • Hmse T: multiple fields with hash set, such as Hmset Hashtable field2 Helloween field3 Christmas
      • hget: Gets the specified hash field, such as Hget Hashtable Field
      • hmget: Gets all specified hash field, such as Hmget Hashtable field field1 field2
      • Hincrby: The specified hash field plus a specific value, such as Hincrby Hashtable field4 5
      • Hdecrby: Specify a hash field minus a specific value, such as Hdecrby Hashtable field4 5
      • hexists: Judging Specifies whether the field exists, such as Hexists Hashtable field4
      • Hlen: Returns the number of field for the specified hash, such as Hlen Hashtable
      • hdel: Delete field with the specified hash , such as Hdel Hashtable field
      • Hkeys: Returns all field of the hash, such as Hkeys Hashtable
      • hvals: Returns all value of hash, such as hvals hash Table
      • hgetall: Gets all field value in a hash, such as Hgetall Hashtable
  • Lists: A list structure, the main function is Push,pop, get all the values of a range and so on, the operation of key is understood as the name of the list.

The Redis list type is actually a doubly linked list in which each child element is a string type. Can be used as a stack and as a heap

      • Operation
        • Lpush: Adds a string element to the header of the key corresponding to the list, such as Lpush list Hello
        • Rpush: Adds a string element to the end of the list of keys, such as Rpush list world
        • Linsert: Add a string before or after the key corresponds to a specific position in the list, such as Linsert list before world my or Linsert list after Hello
        • LSet: Sets the value of the element in the list specified in the following table, such as LSet list 1 He
        • Lrange: Returns elements within a specified range, such as Lrange list 0-1
        • Lrem: Remove n and value identical elements from the list of key, n<0 delete from tail, n=0 all deleted, such as Lrem list 1 you
        • LTrim: Preserves data within a specified range, such as LTrim list 1-1
        • Lpop: Removes the element from the list header and returns the delete element, such as Lpop list
        • Rpop: Removes the element from the list tail and returns the deleted element, such as the Rpop list
        • Rpoplpush: Removes the first list trailing element to the second list header, such as Rpoplpush List1 List2
        • Lindex: Returns an element of the index position of the list named key, such as Lindex list 1
        • Llen: Returns the length of the list for key, such as Llen list
    • Sets: Collection, is an unordered collection of type string

The set type of Redis is implemented by hash table, and the complexity of adding, deleting, and finding is O (1). Set, intersection, and difference sets can be taken for a collection.

    • action
      • sadd: Add elements to a set named key, such as Sadd settable Hello
      • srem: Removes an element in a set named key, such as Srem settable,
      • smember: Gets all the elements in a set named key, such as Smember settable
      • spop: With The machine returns and removes the elements in the set named key, such as Spop settable
      • sdiff: Returns elements that are key1, different from other keys, such as Sdiff Set1 Set2
      • skiffstore: Key1 as standard, unlike other key elements, stored in Key3, such as Sdiffstore Key3 key1 key2
      • sinter: Returns the intersection of all given keys, such as Sinter Set1 Set2
      • Sinterstore: Stores the intersection of all given keys in another key, such as Sinterstore key Key1 key2
      • sunion: Returns the set of all given keys, such as Sunion Set1 Set2
      • Sunionstore: Stores the set of all given keys in another key, such as Sunionstore key Key1 Key2
      • Smove: Removes the member from the set corresponding to the first key and adds it to the second corresponding set, such as Smove Set1 Set2 Hello
      • SCard: Returns the number of set elements named key, such as SCard Set1
      • Sismember: Tests whether member belongs to an element of set named key, such as Sismember set member
      • Srandmember: Randomly returns an element of a set named key, such as Srandmember set
  • Sorted sets: Added a sequential attribute on the set basis, this property can be set when adding the modified element, and after each designation, the Zset will be re-adjusted in the new value order, the sequence number can be the same, the value is not the same
    • Operation
        • Zadd: Adds an element to the set named key and specifies the ordinal, such as Zadd settable 1 Hello
        • zrange: Returns the element in the specified index range, adds Withscores, returns the ordinal, such as Zrange settable 0-1 withscores
        • zrem: Remove zset elements named key member, such as Zrem settable Hello
        • Zincrby: If the element member is already present in Zset with the name key, the score of the element increases increment, otherwise, the element is added to the collection, and the score value is increment, such as Zincrby settable 2 Earth
        • Zrank: Returns the rank of the member element in Zset named key (sorted by score from small to large), such as Zrank settable earth withscores
        • Zrevrank: Returns the rank of the member element in the Zset named key (sorted by score from large to small), such as  zrevrank settable earth withscores
        • Zrevrange: Returns all elements of the index from start to end, such as Zrevrange settable 0-1 withscores
        • , in Zset (score from large to small order) with the name key Zrangebyscore: Returns an element in the collection score at a given interval, such as Zrangebyscore settable 1 3 withscores
        • zcount: Returns the number of elements in the collection that score at a given interval, as Zcount settable 1 3
        • Zcard: Returns the number of elements in the collection, such as Zcard settable
        • zremrangebyrank: Deletes the index of the rank in the collection at the given interval element, as Zremrangebyrank settable 1 1
        • Zremrangebyscore: Removes elements from the collection score at a given interval, such as Zremrangebyscore settable 1 2

Data types and operations for Redis

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.