In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
Redis currently supports five data types, namely
String (string)
List (list)
Hash (dictionary)
Set (collection)
Sorted Set (ordered set)
Redis data type
1. String type
SET key value sets key=value
The value corresponding to GET key or key key
GETRANGE key start end gets the substring of the string stored in a key
GETSET key value sets the string value of the key and returns the old value
GETBIT key offset returns the offset of the string value stored in the key value
MGET key1 [key2..] Get the values of all the given keys
SETBIT key offset value sets or clears the offset of the bit in the string value stored in the key
Set the value when the SETEX key seconds value key expires
SETNX key value sets the value of the key only if the key does not exist
SETRANGE key offset value overrides the offset of part of the string from the specified key
STRLEN key gets the length of the value stored in the key
MSET key value [key value...] Set multiple keys and values
MSETNX key value [key value...] Set multiple keys and multiple values, only when there is no key
Millisecond value and expiration time of PSETEX key milliseconds value setting key
INCR key increases the integer value of the key once
INCRBY key increment increments the integer value of keys by a given number of keys
INCRBYFLOAT key increment increases the floating-point value of keys by a given number of keys
The integer value of the DECR key decrement key once
DECRBY key decrement is the integer value of a given number of decreasing keys
APPEND key value add value to a key
Operation management command
DEL key if there is a delete key
DUMP key returns the serialized version of the value stored in the specified key
EXISTS key this command checks whether the key exists
EXPIRE key seconds specifies the expiration time of the key
The key expiration time specified by EXPIREAT key timestamp. In this case, the time is in Unix timestamp format
PEXPIRE key milliseconds setting key expires in milliseconds
The PEXPIREAT key milliseconds-timestamp setting key expires in milliseconds specified in the Unix timestamp
KEYS pattern finds all keys that match the specified pattern
MOVE key db move key to another database
PERSIST key removes expired keys
PTTL key gets the expiration key for the remaining time in milliseconds.
TTL key gets the remaining time for the key to expire.
RANDOMKEY returns a random key from Redis
The name of the RENAME key newkey change key
RENAMENX key newkey rename key, if the new key does not exist
TYPE key returns the value of the data type stored in the key.
Examples of operational use
Redis 127.0.0.1 set baidu http://www.baidu 6379
OK
Redis 127.0.0.1 6379 > append baidu .com
(integer) 20
Redis 127.0.0.1 6379 > get baidu
"http://www.baidu.com"
Redis 127.0.0.1 6379 > set visitors 0
OK
Redis 127.0.0.1 6379 > incr visitors
(integer) 1
Redis 127.0.0.1 6379 > incr visitors
(integer) 2
Redis 127.0.0.1 6379 > get visitors
"2"
Redis 127.0.0.1 6379 > incrby visitors
(integer) 102
Redis 127.0.0.1 6379 > get visitors
"102"
Redis 127.0.0.1 6379 > type baidu
String
Redis 127.0.0.1 6379 > type visitors
String
Redis 127.0.0.1 6379 > ttl baidu
(integer)-1
Redis 127.0.0.1 6379 > rename baidu baidu-site
OK
Redis 127.0.0.1 6379 > get baidu
(nil)
Redis 127.0.0.1 6379 > get baidu-site
"http://www.baidu.com"
2. List (list)
A Redis list is a simple list of strings, which can be compared to the std::list in C++. It is simply a linked list or a queue. You can add elements to the Redis list from the head or tail. The maximum length of a list is 2 ^ 32-1, which means that each list supports more than 4 billion elements.
The implementation of Redis list is a two-way linked list, that is, it can support reverse search and traversal, which is more convenient to operate, but it brings some additional memory overhead. Many implementations within Redis, including send buffer queues, also use this data structure.
Application scenario
There are many application scenarios of Redis list, and it is also one of the most important data structures of Redis. For example, the watch list and fan list of twitter can be implemented using the list structure of Redis. For example, some applications use the list type of Redis to implement a simple lightweight message queue, producer push, consumer pop/bpop.
Related command
BLPOP
BLPOP key1 [key2] timeout takes out and gets the first element in the list, or blocks until it is available
BRPOP
BRPOP key1 [key2] timeout takes out and gets the last element in the list, or blocks until it is available
BRPOPLPUSH
BRPOPLPUSH source destination timeout pops a value from the list, pushes it to another list and returns it, or blocks until it is available
LINDEX
LINDEX key index gets the corresponding element from a list with its index
LINSERT
LINSERT key BEFORE | AFTER pivot value inserts an element after or before other elements in the list
LLEN
LLEN key gets the length of the list
LPOP
LPOP key gets and fetches the first element in the list
LPUSH
LPUSH key value1 [value2] preceded by a list of one or more values
LPUSHX
LPUSHX key value adds a values list in front of it, only if it exists in the list
LRANGE
LRANGE key start stop gets various elements from a list
LREM
LREM key count value removes an element from the list
LSET
LSET key index value sets the value of an element in the index in the list
LTRIM
LTRIM key start stop trim list to the specified range
RPOP
RPOP key takes out and gets the last element in the list
RPOPLPUSH
RPOPLPUSH source destination deletes the list of the last element, appends it to another list, and returns it
RPUSH
RPUSH key value1 [value2] add one or more values to the list
RPUSHX
RPUSHX key value adds a list of values only if it exists in the list
Use the example
Redis 127.0.0.1 6379 > lpush list1 redis
(integer) 1
Redis 127.0.0.1 6379 > lpush list1 hello
(integer) 2
Redis 127.0.0.1 6379 > rpush list1 world
(integer) 3
Redis 127.0.0.1 6379 > llen list1
(integer) 3
Redis 127.0.0.1 6379 > lrange list1 0 3
1) "hello"
2) "redis"
3) "world"
Redis 127.0.0.1 6379 > lpop list1
"hello"
Redis 127.0.0.1 6379 > rpop list1
"world"
Redis 127.0.0.1 6379 > lrange list1 0 3
1) "redis"
3. Hash (dictionary, hash table)
Similar to the dict type in C# or the hash_map type in C++.
Redis Hash is actually a HashMap inside the Value. In fact, there are two different implementations. When the number of members of this Hash is small, the Redis will use a compact storage method similar to an one-dimensional array to save memory, instead of using the real HashMap structure. The encoding of the corresponding value redisObject is zipmap. When the number of members increases, it will be automatically converted to the real HashMap, and the encoding will be ht.
Application scenario
Suppose there are multiple users and corresponding user information, which can be used to store user ID as key, and serialize user information into such as json format as value to save.
Related command
HDEL
HDEL key field [field...] Delete one or more property fields of an object, and attributes that do not exist will be ignored
HEXISTS
HEXISTS key field to see if the property field exists for the object
HGET
HGET key field gets the value of the field attribute field in the object
HGETALL
HGETALL key gets all property fields and values of an object
HINCRBY
HINCRBY key field value increases the value of the specified field in the object by a given value, and the atomic self-increment operation can only be used for the attribute value of integer.
HINCRBYFLOAT
HINCRBYFLOAT key field increment increases the value of the specified field in the object by a given floating point number
HKEYS
HKEYS key gets all the property fields of an object
HVALS
HVALS key gets all attribute values of an object
HLEN
HLEN key gets the total number of all property fields of the object
HMGET
HMGET key field [field...] Gets the value of one or more specified fields of the object
HSET
HSET key field value sets the value of the specified field of the object
HMSET
HMSET key field value [field value...] Set the value of one or more fields in the object at the same time
The HSETNX:HSETNX command is used to set fields in the hash of stored key values, only if the field does not exist.
If the key does not exist, the new key is created by the hash. If the field already exists, the operation has no effect.
HSETNX key field value sets the value of the field only if the specified field does not exist in the object
HSCAN
HSCAN key cursor [MATCH pattern] [COUNT count] is similar to the SCAN command
Use the example
127.0.0.1 purl 6379 > hset person name jack
(integer) 1
127.0.0.1 6379 > hset person age 20
(integer) 1
127.0.0.1 purl 6379 > hset person sex famale
(integer) 1
127.0.0.1 purl 6379 > hget person name
"jack"
127.0.0.1 purl 6379 > hgetall person
1) "name"
2) "jack"
3) "age"
4) "20"
5) "sex"
6) "famale"
127.0.0.1 purl 6379 > hkeys person
1) "name"
2) "age"
3) "sex"
127.0.0.1 purl 6379 > hvals person
1) "jack"
2) "20"
3) "famale"
4. Set (collection)
It can be understood as a list of non-duplicate values, similar to the concept of sets in the field of mathematics, and Redis also provides operations such as intersection, union, difference and so on.
The internal implementation of set is a HashMap in which value is always null. In fact, the weight is arranged quickly by calculating hash, which is why set can determine whether a member is in the collection or not.
Application scenario
Redis set external function is similar to list is a list function, the special feature is that set can automatically arrange weight, when you need to store a list of data, but do not want to duplicate data, set is a good choice, and set provides an important interface to determine whether a member is in a set collection, which list can not provide.
Or in the Weibo application, where there is a collection of people that each user follows, it is easy to find a common friend of two people.
Related command
SADD
SADD key member [member...] Add one or more elements to the set
SACRD
SCARD key gets the number of elements in the collection
SDIFF
SDIFF key [key...] Get elements that do not exist in the queue
SDIFFSTORE
SDIFFSTORE destination key [key...] Get elements that do not exist in the queue and store them in a key result set
SINTER
SINTER key [key...] Get the intersection of two sets
SINTERSTORE
SINTERSTORE destination key [key...] Get the intersection of two sets and store them in one set
SISMEMBER
SISMEMBER key member determines that a given value is a member of a collection
SMEMBERS
SMEMBERS key gets all the key in the collection
SMOVE
SMOVE source destination member moves one key from a collection to another collection
SPOP
SPOP key [count] gets and deletes elements from a collection
SRANDMEMBER
SRANDMEMBER key [count] randomly fetches an element from the collection
SREM
SREM key member [member...] Delete one or more elements from the collection, and elements that do not exist will be ignored
SUNION
SUNION key [key...] Add multiple set elements
SUNIONSTORE
SUNIONSTORE destination key [key...] Merge set elements and store the results in a new set
SSCAN
SSCAN key cursor [MATCH pattern] [COUNT count] iterates over the elements in set
Use the example
Redis > SADD myset "Hello"
(integer) 1
Redis > SADD myset "World"
(integer) 1
Redis > SMEMBERS myset
1) "World"
2) "Hello"
Redis > SADD myset "one"
(integer) 1
Redis > SISMEMBER myset "one"
(integer) 1
Redis > SISMEMBER myset "two"
(integer) 0
A typical use case for using a collection data structure is the implementation of a friend list:
Redis 127.0.0.1 6379 > sadd friends:leto ghanima paul chani jessica
(integer) 4
Redis 127.0.0.1 6379 > sadd friends:duncan paul jessica alia
(integer) 3
Redis 127.0.0.1 6379 > sismember friends:leto jessica
(integer) 1 # No matter how many friends a user has, we can efficiently (O (1) time complexity) identify whether user X is a friend of user Y.
Redis 127.0.0.1 6379 > sismember friends:leto vladimir
(integer) 0
Redis 127.0.0.1 6379 > sinter friends:leto friends:duncan # We can check whether two or more people have common friends.
1) "paul"
2) "jessica"
Redis 127.0.0.1 redis 6379 > sinterstore friends:leto_duncan friends:leto friends:duncan # can store the result in a new keyword
(integer) 2
5. Sorted Set (ordered set)
Redis ordered collections are similar to Redis collections, except that a function is added, that is, collections are ordered. Each member of an ordered set has a score for sorting.
The time complexity of adding, deleting, and testing Redis ordered sets is all O (1) (fixed time, regardless of the number of sets of elements contained in it). The maximum length of the list is 2 ^ 32-1 elements (4294967295, more than 4 billion sets of each element).
Redis sorted set internal use HashMap and jump table (SkipList) to ensure data storage and order, HashMap is put in the member to score mapping, while the jump table is stored in all the members, sorting according to the score stored in HashMap, the use of jump table structure can achieve higher search efficiency, and relatively simple in implementation.
Working with scen
The usage scenario of Redis sorted set is similar to that of set, except that set is not automatically ordered, while sorted set can sort members by providing an additional parameter of score, and it is inserted in order, that is, automatic sorting. When you need an ordered and non-repeating list of collections, you can choose the sorted set data structure. For example, twitter's public timeline can be stored as a score with the publication time, so that the acquisition is automatically sorted according to time.
For example, the needs of users' points rankings can be realized through orderly collections. There are also lightweight message queues using List described above, which can also be implemented with priority or weighted queues through Sorted Set.
Related command
ZADD
ZADD key score1 member1 [score2 member2] add one or more members to the ordered collection, or update its score if it already exists
ZCARD
The number of ordered set members obtained by ZCARD key
ZCOUNT
ZCOUNT key min max calculates the scores within the range of members and given values of an ordered set
ZINCRBY
ZINCRBY key increment member increases the score of members in an ordered set
ZINTERSTORE
ZINTERSTORE destination numkeys key [key...] Multiple cross-sorted collections are stored to generate a new key ordered set.
ZLEXCOUNT
ZLEXCOUNT key min max calculates the number of ordered set members between a given dictionary range
ZRANGE
ZRANGE key start stop [WITHSCORES] returns an ordered collection of member ranges (from low to high) from the index
ZRANGEBYLEX
ZRANGEBYLEX key min max [LIMIT offset count] returns an ordered collection of member ranges (by dictionary scope)
ZRANGEBYSCORE
ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT] returns all members of the ordered set key whose score values are between min and max (including those equal to min or max). The members of the ordered set are arranged in the order of increasing score values (from smallest to largest).
ZRANK
ZRANK key member determines the ordered set in the index of the member
ZREM
ZREM key member [member...] Remove one or more members from the ordered collection, and members that do not exist will be ignored
ZREMRANGEBYLEX
ZREMRANGEBYLEX key min max deletes the ordered set of all members between a given dictionary range
ZREMRANGEBYRANK
ZREMRANGEBYRANK key start stop deletes an ordered collection of all members within a given index
ZREMRANGEBYSCORE
ZREMRANGEBYSCORE key min max deletes an ordered set of all members within a given score
ZREVRANGE
ZREVRANGE key start stop [WITHSCORES] returns an ordered collection of members, sorted by scores, by index, from high to low
ZREVRANGEBYSCORE
ZREVRANGEBYSCORE key max min [WITHSCORES] returns an ordered collection of member ranges, sorted from highest to lowest in socre
ZREVRANK
ZREVRANK key member determines the index of the members of an ordered collection, sorted by scores, from high to low
ZSCORE
ZSCORE key member gets the scores associated with a given member in an ordered set
ZUNIONSTORE
ZUNIONSTORE destination numkeys key [key...] Add multiple sets to sort, and the resulting sort set is stored in a new key
ZSCAN
ZSCAN key cursor [MATCH pattern] [COUNT count] incremental iterative sorting element set and related scores
Use the example
Redis 127.0.0.1 redis 6379 > zadd dbs 100
(integer) 1
Redis 127.0.0.1 memcached 6379 > zadd dbs 98
(integer) 1
Redis 127.0.0.1 mongodb 6379 > zadd dbs 99
(integer) 1
Redis 127.0.0.1 leveldb 6379 > zadd dbs 99
(integer) 1
Redis 127.0.0.1 6379 > zcard dbs
(integer) 4
Redis 127.0.0.1 6379 > zcount dbs 10 99
(integer) 3
Redis 127.0.0.1 6379 > zrank dbs leveldb
(integer) 1
Redis 127.0.0.1 6379 > zrank dbs other
(nil)
Redis 127.0.1 6379 > zrangebyscore dbs 98 100
1) "memcached"
2) "leveldb"
3) "mongodb"
4) "redis"
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.