In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
Redis basic types and common operations, commands, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.
Concept: Redis is an open source high-performance key-value database developed in C language.
Characteristics:
There is no necessary connection between the data.
Single-thread mechanism is used to work internally.
High performance
Multiple data types support string type String list type List hash type Map collection type Set ordered collection type SortedSet
Persistence support
Application scenario
Speed up the query for hot data, such as hot commodities, hot news, hot information and other high traffic information.
Task queue, such as: second kill, rush purchase, ticket purchase, etc.
Instant information query, such as: ranking, etc.
Timeliness information control, such as CAPTCHA, voting control, etc.
Distributed data sharing, such as session in distributed architecture.
Message queue
Distributed lock
Basic data type: String
The storage space for a single string type is 512MB
Basic operation of string
Add or modify data
Set key value
Get data
Get key
Delete data
Del key
Set expiration time (default unit is seconds)
Expire key second
Append information to the initial value
Append key value
Add / modify multiple data
Mset key1 value1 key2 value2...
Get multiple data
Mget key1 key2...
Get string length
Strlen key
Set the life cycle of key to control the life cycle
Setex key seconds value (seconds) psetex key millisexxonds value (milliseconds)
The setting convention of key corresponds to the table-primary key-field in the database one by one corresponding to the table name, primary key name, primary key value field name, eg1orderid443523454nameeg2equireid435432543typeeg3newsid45435454title.
Basic data type: Hash
Storage requirements: arrange a series of stored data for easy management. Typical applications store object information.
Storage structure: a storage space stores multiple key-value pairs of data
Hash type: the underlying layer uses hash table structure to implement data storage
Optimization of hash storage structure
If the number of field is small, the storage structure is optimized to be a class array structure.
If the number of field is large, the storage structure uses the HashMap structure
Basic operation of hash type
Add / modify data
Hset key field value
Get data
Hget key field hgetall key
Delete data
Hdel key field [field2...]
Add or delete multiple data
Hmset key field1 value1 field2 value2...
Get multiple data
Hmget key field1 field2...
Get the number of fields in the hash table
Hlen key
Gets whether the specified field exists in the hash table
Hexists key field
Gets the field name or field value used in the hash table
Hkeys keyhvalues key
Set the numeric data of the specified field to increase the value of the specified range
Hincrby key field increment hincrbyfloat key field increment
Add it if the field under the key value exists and does not exist.
Hsetnx key field value
Considerations for hash type data manipulation
Value under hash type can only store strings, but not other data types. There is no nesting phenomenon. If the data is not obtained, the corresponding value is (nil).
The maximum number of key-value pairs stored in each hash is $2 ^ {32}-1 $$.
The hash type is very close to the storage form of the object and has the flexibility to delete object properties. However, hash is not designed to store a large number of objects, so don't abuse it, let alone use hash as an object list.
The hgetall operation can get all the attributes. If there are too many internal field, the efficiency of traversing the overall data will be very low, and it may become the bottleneck of data access.
Application scenario
Design and implementation of Shopping cart for E-commerce website
Basic data type: list
Data storage requirements: store multiple data and distinguish the key order in which the data enters the storage Jon
Required storage structure: a storage space stores multiple data, and the data can reflect the entry order
List type: saves multiple data, and the underlying layer is implemented using a two-way linked list storage structure
The list type is stored in a two-way linked list
Basic operation of list
Add / modify data
Lpush key value1 value2 [value3]. / / add rpush key value1 value2 [value3] from the left side of the list linked list. / / add from the right side of the list
Get data
Lrange key start stop / / specify the value / / in the start and end position of the linked list. When you want to view all value types of unknown length, you can use-1 to represent the penultimate lrange key start-1 lindex key index / / to get the value llen key / / at the specified position in the linked list.
Get and remove data
Lpop key rpop key
Get and remove data within a specified period of time (blocking data acquisition)
Blpop key1 [key2] timeout / / takes out and removes the value corresponding to the key value within the specified time. If the timeout timeout has not been taken out, a null value (nil) is returned. If no other client has added the value of this key while waiting, do the operation task queue brpop key1 [key2] timeout.
Remove specified data
Lrem key count value
Considerations for data manipulation of list type
The data stored in list is of string type, and the total data capacity is limited, up to 2 ^ 32-1 elements.
List has the concept of index, but the operation of data is usually carried out in the form of queue or in and out of the stack in the form of stack.
The index of getting all operation end data is set to-1
List can paginate the data. Usually, the information on the first page comes from list, and the second page and more data information is queried and loaded through the database.
Basic data type: set
Storage requirements: store a large amount of data to provide higher efficiency in query
Storage structure: can save a large amount of data, efficient internal storage mechanism, easy to query
Set type: identical to hash storage structure, only keys are stored, values are not stored (nil), and values are not allowed to be empty
Set storage structure is an unordered collection of string type, and hash storage structure is used for internal storage, so the complexity of adding, searching and deleting is O (1).
Basic operation of set
Add non-duplicated data
Sadd key value
Get all stored data
Smembers key
Delete data
Strem key member1 [member2]
Get the total amount of aggregate data
Scard key
Determine whether the collection contains the specified data
Sismember key member
Randomly get a specified amount of data in the collection
Srandmember key [count]
Randomly get some data in the collection and move the data out of the collection
Apop key
The intersection, union and difference of two sets
Sinter key1 [key2] sunion key1 [key2] adiff key1 [key2]
Find the intersection, union and difference sets of two sets and store them in the specified set
Sinterstore destination key1 [key2] sunionstore destination key1 [key2] sdiffstore destination key1 [key2]
Moves the specified data from the original collection to the target collection
Smove source destination member
Matters needing attention
Duplicate data is not allowed in set type. If the added data already exists in set, only one copy will be retained.
Although set has the same storage structure as hash, it cannot enable the space for storing values in hash
Basic data type: sortedSet
Storage requirements: data sorting is conducive to the display of data, and it is necessary to provide a way to sort data according to its own characteristics.
Storage structure: you can save sorted data
Storage type: add sortable fields to the storage structure of set
Basic operation
Add data
Zadd key scorel member [score2 member2]
Get all the data
Zrange key start stop [witchscores] zrevrange key star stop [witchscores]
Delete data
Zrem key member [member...]
Query data by condition
Zrangebyscore key min max [withscores] [limit] zrevrangebyscore key max min [withscores]
Conditional deletion data
Zremrangebyrank key start stop / / start stop indicates the start and end position of the index zremrangebyscore key min max / / min max indicates the minimum to maximum position of the sort
Get the number of collection data
Zcard key zcount key min max
Set intersection and union operation
Zinterstore destination numkeys key [key...] Zunionstore destination key [key...] After reading the above, have you mastered the basic types of Redis and the methods of common operations and commands? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
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.