Redis (3) ------Five major data types, three special data types
Redis is an open source, in-memory data structure storage system that can be used as a database, cache and messaging middleware MQ
It supports multiple types of data structures, such as: String, Hashes, Lists, Sets, Sorted Sets and range queries, cardinality statistics (Hyperloglogs), Bitmaps and Geospatial index radius queries
Redis has built-in replication, LUA Scripting, LRU Eviction, transactions and different levels of disk persistence.
5、Redis-Key
127 .0.0.1:6379> keys * # 查看所有的key(empty list or set)
127 .0.0.1:6379>set name zzz # set key
OK
127 .0.0.1:6379> keys *
1 )"name"
127 .0.0.1:6379>set age 1
OK
127 .0.0.1:6379> keys *
1 )"age"
2 )"name"
127 .0.0.1:6379> EXISTS name # 判断当前的key是否存在,返回1说明存在(integer) 1
127 .0.0.1:6379> EXISTS name1 # 返回0说明不存在(integer) 0
127 .0.0.1:6379> move name 1 # 移动当前的key到指定数据库(integer) 1
127 .0.0.1:6379> keys *
1 )"age"
127.0.0.1:6379>select 1 # 切换到数据库1
OK
127.0.0.1:6379[1]> keys *
1)"name"
127.0.0.1:6379[1]> get name
"zzz"
127.0.0.1:6379[1]>select 0 # 切换到数据库0
OK
127.0.0.1:6379> keys *
1)"age"
127.0.0.1:6379>set name zzz
OK
127.0.0.1:6379> keys *
1)"age"
2)"name"
127.0.0.1:6379> get name
"zzz"
127.0.0.1:6379> EXPIRE name 10 #设置key的过期时间,单位是秒(integer) 1
127.0.0.1:6379> ttl name # 查看当前key的剩余时间(integer) 4
127.0.0.1:6379> ttl name
(integer) 3
127.0.0.1:6379> ttl name
(integer) 2
127.0.0.1:6379> ttl name
(integer) 1
127.0.0.1:6379> ttl name
(integer) -2
127.0.0.1:6379> get name # 就会自动删除 keys *也就没了(nil)# 表示为空
127.0.0.1:6379>type name # 查看当前key的一个类型!
string
127.0.0.1:6379>type age
string
In addition to being a string, value can also be a number.
counter
Count the number of multiple units
Number of fans
Object cache storage
6.2 List
Values in List can be repeated
In Redis, List can be operated as a stack, queue, or blocking queue.
Most List commands start with L or R, and Redis is not case-sensitive.
6.2.1 Basic usage
Order
describe
LPUSH/RPUSH key value1[value2]
PUSH value (one or more) from left/right to list
LRANGE key n1 n2
Intercept the starting and ending elements of the list (index increases from left to right), [0,-1] gets all values
LLEN key
View list length
LINDEX key n
Get the nth element of the list by index
LSET key n value
Update the value of element n in the list by index
LPOP/RPOP key
Removes value from leftmost/rightmost and returns removed value
RPOPLPUSH current list name new list name
Pop the rightmost value of the list, return it, and add it to the head of another list
BRPOPLPUSH current list name new list name timeout
Pop the rightmost value from the list, return it, and add it to the head of another list. If there are no elements in the list, the list will be blocked until the wait times out or a pop-up element is found.
LTRIM key n1 n2
Intercept the list within the range [n1,n2]
LREM key n value
n>0: Search from the head, delete the specified value, delete at most n. n<0: Search from the end, delete the specified value, and delete at most -n values. n=0: delete all specified values in the list
BLPOP/BRPOP key1[key2] timeout
移出并获取列表第一/最后一个元素,如果列表没有元素阻塞会阻塞,直到等待超时或发现可弹出元素为止
LINSERT key BEFORE/AFTER “目标值” “插入值”
在key列表中,目标值元素前/后插入指定插入值
实例
------------ LPUSH RPUSH ------------
# 取值 LRANGE# 将一个值或者多个值,插入到列表头部
127.0.0.1:6379> LPUSH list one
(integer) 1
127.0.0.1:6379> LPUSH list two
(integer) 2
127.0.0.1:6379> LPUSH list three
(integer) 3
------------ LRANGE ------------
# 获取list所有值
127.0.0.1:6379> LRANGE list 0 -1
1 )"three"
2 )"two"
3 )"one"# 通过区间获取具体的值
127.0.0.1:6379> LRANGE list 0 1
1 )"three"
2 )"two"# 将一个值或者多个值,插入到列表尾部 (右)
127.0.0.1:6379> rpush list right
(integer) 4
127.0.0.1:6379> lrange list 0 -1
1)"three"
2)"two"
3)"one"
4)"right"
------------ LPOP RPOP ------------
# 双端队列的感觉# 移除list的第一个元素,并返回值
127.0.0.1:6379> Lpop list
"three"# 移除list的最后一个元素,并返回值
127.0.0.1:6379> Rpop list
"right"
127.0.0.1:6379> LRANGE list 0 -1
1 )"two"
2 )"one"
------------ Lindex ------------
127.0.0.1:6379> LRANGE list 0 -1
1 )"two"
2 )"one"# 获取list中下标为1的值
127.0.0.1:6379> lindex list 1
"one"
127.0.0.1:6379> lindex list 0
"two"
------------ Llen key ------------
# 返回列表的长度
127.0.0.1:6379> Llen list
(integer) 2
# 移除指定的值 可移除多个
127.0.0.1:6379> lpush list three
(integer) 4
127.0.0.1:6379> lrange list 0 -1
1)"three"
2)"three"
3)"two"
4)"one"# 移除list集合中指定个数的value,精确匹配# 移除1个one
127.0.0.1:6379> lrem list 1 one
(integer) 1
127.0.0.1:6379> lrange list 0 -1
1)"three"
2)"three"
3)"two"# 移除2个three
127.0.0.1:6379> lrem list 2 three
(integer) 2
127.0.0.1:6379> lrange list 0 -1
1)"two"
------------ ltrim ------------
# 修剪 没有rtrim
127.0.0.1:6379> rpush mylist hello hello1 hello2 hello3
(integer) 4
127.0.0.1:6379> LRANGE mylist 0 -1
1)"hello"
2)"hello1"
3)"hello2"
4)"hello3"# 通过下标截取指定的长度,list已改变,只剩下截取的元素# 截取区间[1,2]数据,其他移除
127.0.0.1:6379> LTRIM mylist 1 2
OK
127.0.0.1:6379> LRANGE mylist 0 -1
1)"hello1"
2)"hello2"
------------ RPOPLPUSH ------------
# 移除列表的最后一个元素,将他移动到新的列表中# rpoplpush list名称 新list名称
127.0.0.1:6379> LRANGE mylist 0 -1
1)"hello"
2)"hello1"
3)"hello2"
4)"hello3"
127.0.0.1:6379> RPOPLPUSH mylist mylistnew
"hello3"
127.0.0.1:6379> LRANGE mylist 0 -1
1)"hello"
2)"hello1"
3)"hello2"
127.0.0.1:6379> LRANGE mylistnew 0 -1
1)"hello3"
------------ lset ------------
# 将列表中的指定下标的值替换为另外一个值,更新操作
127.0.0.1:6379> exists list
(integer) 0
# 如果不存在列表去更新会报错
127.0.0.1:6379> lset list 0 value
(error) ERR no such key
127.0.0.1:6379> lpush list value1
(integer) 1
127.0.0.1:6379> lrange list 0 0
1)"value1"# 如果存在,更新当前下标的值
127.0.0.1:6379> lset list 0 item
OK
127.0.0.1:6379> lrange list 0 0
1)"item"# 如果不存在该下标,报错
127.0.0.1:6379> lset list 1 other
(error) ERR index out of range
------------ linsert ------------
# 将某个具体的value插入到列表中某个元素的前面或后面
127.0.0.1:6379> rpush mylist "hello""world"(integer) 2
# 将new插入到world前
127.0.0.1:6379> linsert mylist before "world""new"(integer) 3
127.0.0.1:6379> lrange mylist 0 -1
1)"hello"
2)"new"
3)"world"# 将!插入到world后
127.0.0.1:6379> linsert mylist after "world""!"(integer) 4
127.0.0.1:6379> lrange mylist 0 -1
1)"hello"
2)"new"
3)"world"
4)"!"