Common operation command base type Redis
feature:
- No necessary link between data
- Internal use of single-threaded mechanism to work
- high performance
- Support multiple data types
- String type String
- List Type List
- Hash type Map
- Set collection type
- Ordered set of type SortedSet
- Persistence Support
Scenarios
- For the hot data to speed up queries; such as: hot commodity, hot news, hot IT high-traffic information.
- Task Queue; such as: spike, buy, tickets and the like.
- Instant information inquiry; such as: leaderboards.
- Timeliness information control; such as: codes, voting control.
- Distributed data sharing; such as: a distributed architecture session like.
- message queue
- Distributed Lock
The basic data types: String
A single memory space for the string type 512MB
string basic operations
-
Add or modify data
set key value
-
retrieve data
get key
-
delete data
del key
-
Set the expiration time (in seconds by default)
expire key second
-
Additional information back to the initial value
append key value
-
Add / Edit a plurality of data
mset key1 value1 key2 value2...
-
Get more data
mget key1 key2 ...
-
Get string length
strlen key
-
Set key life cycle life cycle control
setex key seconds value (秒) psetex key millisexxonds value (毫秒)
-
key settings conventions
The database tables - the primary key - one-field
Table Name Primary key name Primary key Field name eg1 order id 443523454 name EG2 equire id 435432543 type eg3 news id 45435454 title
The underlying data type: Hash
- Storage requirements: a series of stored data layout, easy management, typical application storage object information
- Storage structure: a memory storing a plurality of key-value pairs of data
- hash Type: underlying hash table structure for data storage
hash storage structure optimization
- If fewer number field, an array of storage structures to optimize the structure of the class
- If a larger number field, storage structures using HashMap structure
Basic operations hash type
-
Add / modify data
hset key field value
-
retrieve data
hget key field hgetall key
-
delete data
hdel key field [field2 ...]
-
Add or delete multiple data
hmset key field1 value1 field2 value2 ...
-
Get more data
hmget key field1 field2 ...
-
Gets the hash table of the number of fields
hlen key
-
The existence of the specified field to obtain the hash table
hexists key field
-
Acquiring the field value or field name used in the hash table
hkeys key hvalues key
-
Specified field data is provided to increase the value specified numeric range
hincrby key field increment hincrbyfloat key field increment
-
If the field in the key value exists then no operation is added to it does not exist
hsetnx key field value
Note hash type of data manipulation
-
only the type of hash value stored in a string, stores are disallowed other data types, nested phenomenon does not exist, if the data is not acquired, the corresponding value (nil)
-
Each hash key stored upper limit of
$$
2 ^ {32} -1
$$
key-value pairs -
very close to the hash stored in the form of an object type, and the flexibility to delete object properties. But hash is designed not to store large amounts of objects designed, can not remember the abuse, but can not use the hash as the object list
-
hgetall operation can get all the properties, if too much internal field, traversing the whole data is inefficient, it could become a data access bottleneck
Scenarios
- Electricity supplier website shopping cart design and implementation
The basic data types: list
- Data storage requirements: storing a plurality of data, and the data are sequentially stored into the key distinguishing Jon
- Storage structure needed: a memory storing a plurality of data, and the data can be reflected into the order
- Type list: storing a plurality of data storage underlying structure implemented using a doubly linked list
list types are stored in a doubly linked list
list the basic operation
-
Add / modify data
lpush key value1 value2 [value3] ... //从list链表左侧添加 rpush key value1 value2 [value3] ... //从list右侧添加
-
retrieve data
lrange key start stop //指定链表起始结束位置中的value //在获取未知长的的list类型的时候,想查看所有的value可以使用 -1表示倒数第一个 lrange key start -1 lindex key index //获取链表中指定位置的值 llen key //获取链表的长度
-
Retrieves and removes data
lpop key rpop key
-
Retrieves and removes the specified time data (blocking data acquisition)
blpop key1 [key2] timeout //指定时间内取出并移除key值对应的value,若timeout超时仍未取出则返回空值(nil) 若本来没有 其他客户端在等待的时候添加了这个key的value则做操作 任务队列 brpop key1 [key2] timeout
-
Remove the specified data
lrem key count value
list type Data Handling Precautions
- The list is stored in the data string types, the limited total capacity of data elements up to 2 ^ 32-1
- list with the index concept, but is usually carried out in the form of operation data queue enqueue dequeue operations, or in the form of a stack pop operation of stack
- All data acquisition operation ends index is set to -1
- Paging list can operate on the data, the information is typically the first page from the list, the second page, and additional data by a database query loading
The underlying data type: set
- Storage requirements: storing large amounts of data, providing greater efficiency in terms of query
- Storage structure: capable of storing large amounts of data, an internal storage mechanism efficient, easy to query
- set type: hash storage structure is identical, only the storage key is not stored value (nil), and null values are not allowed
storage structure set unordered collection of type string, hash storage structure stored internally, and therefore add, find, delete complexity is O (1)
set basic operations
-
Add unique data
sadd key value
-
Get all the data stored
smembers key
-
delete data
strem key member1 [member2]
-
Gets a collection of the total amount of data
scard key
-
Determining whether the collection contains the specified data
sismember key member
-
Obtaining a random number of data specified in the collection
srandmember key [count]
-
Obtaining a random data set and the data set out
apop key
-
Two sets of cross, and, difference
sinter key1 [key2] sunion key1 [key2] adiff key1 [key2]
-
Two sets of intersection, and difference between set and stored in the designated collection
sinterstore destination key1 [key2] sunionstore destination key1 [key2] sdiffstore destination key1 [key2]
-
The specified data is moved from the original set to the target set
smove source destination member
Precautions
-
set type does not allow duplicate data, if the data add already exists in the set, only to keep a
-
Although the set with the same hash storage structure, but can not be stored in the space hash value is enabled
Basic data types: sortedSet
Storage requirements: data ordering is conducive to display performance data, you need to provide a way it can be sorted according to their characteristics
Storage structure: the sort of data can be saved
Storage Type: sort field may be added on the storage structure of the set
Basic Operations
-
adding 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 ...]
-
Conditional query data
zrangebyscore key min max [withscores] [limit] zrevrangebyscore key max min [withscores]
-
Deleting data
zremrangebyrank key start stop //start stop 表示索引的开始结束位置 zremrangebyscore key min max //min max表示排序的最小到最大位置
-
Gets a collection of the amount of data
zcard key zcount key min max
-
Set intersection and union operations
zinterstore destination numkeys key [key ...] zunionstore destination key [key ...]