String基本命令:
127.0.0.1:6379> set key1 hello #设置值
OK
127.0.0.1:6379> get key1 # 获取值
"hello"
127.0.0.1:6379> keys * #获取所有的key
1) "key1"
127.0.0.1:6379> APPEND key1 world #追加字符串,如果当前key不存在,就相当于set命令
(integer) 10
127.0.0.1:6379> get key1
"helloworld"
127.0.0.1:6379> EXISTS key1 # 判断字符串是否存在
(integer) 1
127.0.0.1:6379> STRLEN key1 # 获取字符串的长度
(integer) 10
###################################################
#步长 i++
127.0.0.1:6379> set view 0
OK
127.0.0.1:6379> get view
"0"
127.0.0.1:6379> INCR view #自增1
(integer) 1
127.0.0.1:6379> INCR view
(integer) 2
127.0.0.1:6379> INCR view
(integer) 3
127.0.0.1:6379> DECR view #自减1
(integer) 2
127.0.0.1:6379> DECR view
(integer) 1
127.0.0.1:6379> INCRBY view 10 #设置步长指定增量
(integer) 11
###################################################
#字符串范围 range
127.0.0.1:6379> set key "hello world"
OK
127.0.0.1:6379> get key
"hello world"
127.0.0.1:6379> GETRANGE key 0 3 #截取字符串[0,3]
"hell"
127.0.0.1:6379> GETRANGE key 0 -1 #获取全部字符串和get key是一样的
"hello world"
# 替换
127.0.0.1:6379> SETRANGE key 5 fucking #指定开始字符位置
(integer) 12
127.0.0.1:6379> get key
"hellofucking"
###################################################
# setex(set with expire) #设置过期时间
# setnx(set if not exit) # 不存在则设置
127.0.0.1:6379> setex key3 10 "hello" #设置30s过期
OK
127.0.0.1:6379> ttl key3
(integer) 3
127.0.0.1:6379> set mykey "redis"
OK
127.0.0.1:6379> get mykey
"redis"
127.0.0.1:6379> setnx mykey "redis"
(integer) 1
127.0.0.1:6379> setnx mykey "mongodb"
(integer) 0
127.0.0.1:6379> get mykey
"redis"
###################################################
# mset 批量设置
# mget 批量获取
127.0.0.1:6379> MSET k1 v1 k2 v2 k3 v3
OK
127.0.0.1:6379> keys *
1) "k2"
2) "k1"
3) "k3"
127.0.0.1:6379> MGET k1 k2 k3
1) "v1"
2) "v2"
3) "v3"
127.0.0.1:6379> MSETNX k1 v1 k4 v4 # msetnx 是一个原子性操作,要么一起成功,要么一起失败
(integer) 0
127.0.0.1:6379> get k4
(nil)
###################################################
# getset 先获取再设置
127.0.0.1:6379> getset db "redis" #如果不存在返回nil
(nil)
127.0.0.1:6379> get db
"redis"
127.0.0.1:6379> GETSET db "mongodb" #如果存在,返回当前值再设置新的值
"redis"
127.0.0.1:6379> get db
"mongodb"
String的使用场景
list基本命令:
#LPUSH
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
127.0.0.1:6379> LRANGE list 0 -1 #获取list的值
1) "three"
2) "two"
3) "one"
127.0.0.1:6379> LRANGE list 0 1
1) "three"
2) "two"
#RPUSH
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
127.0.0.1:6379> LPOP list #移除list的第一个元素
"three"
127.0.0.1:6379> RPOP list #移除list最后一个元素
"right"
127.0.0.1:6379> LRANGE list 0 -1
1) "two"
2) "one"
# lindex
127.0.0.1:6379> lindex list 1 #通过下标获取值
"one"
127.0.0.1:6379> lindex list 0
"two"
127.0.0.1:6379> LLEN list #返回列表的长度
(integer) 2
#LREM 移除指定的值
127.0.0.1:6379> LRANGE list 0 -1
1) "three"
2) "three"
3) "two"
4) "one"
127.0.0.1:6379> LREM list 1 three #移除指定个数的value 精确匹配
(integer) 1
127.0.0.1:6379> LRANGE list 0 -1
1) "three"
2) "two"
3) "one"
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"
127.0.0.1:6379> LREM list 2 three
(integer) 2
127.0.0.1:6379> LRANGE list 0 -1
1) "two"
2) "one"
# trim 截取
127.0.0.1:6379> RPUSH list "hello"
(integer) 3
127.0.0.1:6379> RPUSH list "world"
(integer) 4
127.0.0.1:6379> LRANGE list 0 -1
1) "two"
2) "one"
3) "hello"
4) "world"
127.0.0.1:6379> LTRIM list 1 2 #通过下标截取指定的长度,这个list已经改变,只剩下截取的元素
OK
127.0.0.1:6379> LRANGE list 0 -1
1) "one"
2) "hello"
#rpoplpush 移除列表的最后一个元素并移动到新的列表中
127.0.0.1:6379> rpoplpush list mylist
"hello"
127.0.0.1:6379> LRANGE list 0 -1
1) "one"
127.0.0.1:6379> LRANGE mylist 0 -1
1) "hello"
#lset 将列表中指定下标的值替换新的值。更新操作
127.0.0.1:6379> EXISTS list
(integer) 1
127.0.0.1:6379> LRANGE list 0 -1
1) "one"
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 update # 如果不存在则报错
(error) ERR index out of range
# linsert 将某一个具体的值插入到列表中某个元素的前面或者后面
127.0.0.1:6379> LINSERT list before item preone
(integer) 2
127.0.0.1:6379> LRANGE list 0 -1
1) "preone"
2) "item"
127.0.0.1:6379> LINSERT list after item afterone
(integer) 3
127.0.0.1:6379> LRANGE list 0 -1
1) "preone"
2) "item"
3) "afterone"
list使用场景:
set中的值是不重复的,无序不重复集合
# sadd 添加元素
127.0.0.1:6379> sadd myset "hello"
(integer) 1
127.0.0.1:6379> sadd myset "world"
(integer) 1
127.0.0.1:6379> sadd myset "happy"
(integer) 1
127.0.0.1:6379> sadd myset "new"
(integer) 1
127.0.0.1:6379> sadd myset "year"
(integer) 1
127.0.0.1:6379> SMEMBERS myset #查看元素
1) "world"
2) "new"
3) "hello"
4) "happy"
5) "year"
127.0.0.1:6379> SISMEMBER myset hello #判断元素是否存在
(integer) 1
127.0.0.1:6379> SISMEMBER myset bb
(integer) 0
127.0.0.1:6379> SCARD myset #获取set集合中的内容元素个数
(integer) 5
127.0.0.1:6379> SREM myset happy #移除指定元素
(integer) 1
127.0.0.1:6379> SMEMBERS myset
1) "world"
2) "hello"
3) "year"
4) "new"
# SRANDMEMBER 随机抽选一个元素
127.0.0.1:6379> SRANDMEMBER myset
"world"
127.0.0.1:6379> SRANDMEMBER myset
"new"
127.0.0.1:6379> SRANDMEMBER myset
"year"
127.0.0.1:6379> SRANDMEMBER myset 2 #指定个数的元素
1) "new"
2) "year"
# SMOVE 将一个指定的值从当前set集合移动到另一个set集合
127.0.0.1:6379> SMOVE myset myset2 year
(integer) 1
127.0.0.1:6379> SMEMBERS myset
1) "world"
2) "hello"
3) "new"
127.0.0.1:6379> SMEMBERS myset2
1) "year"
# SDIFF差集 SINTER交集(共同好友) SUNION 并集
127.0.0.1:6379> SDIFF myset1 myset2
(empty list or set)
127.0.0.1:6379> SDIFF myset myset2
1) "world"
2) "hello"
127.0.0.1:6379> SINTER myset myset2
1) "new"
127.0.0.1:6379> SDIFF myset2 myset
1) "year"
127.0.0.1:6379> SUNION myset myset2
1) "world"
2) "year"
3) "new"
4) "hello"
Set的应用场景:
hash基本命令:
#hset
127.0.0.1:6379> hset myhash filed1 test #设置一个具体的key-value
(integer) 1
127.0.0.1:6379> hget myhash filed1
"test"
127.0.0.1:6379> hset myhash filed2 world
(integer) 1
127.0.0.1:6379> hmset myhash filed1 hello filed3 yes #设置多个key-value
OK
127.0.0.1:6379> hmget myhash filed1 filed2 filed3 #获取多个key的值
1) "hello"
2) "world"
3) "yes"
127.0.0.1:6379> HGETALL myhash #获取所有的key-value
1) "filed1"
2) "hello"
3) "filed2"
4) "world"
5) "filed3"
6) "yes"
# HDEL删除一个key
127.0.0.1:6379> HDEL myhash filed3
(integer) 1
127.0.0.1:6379> HGETALL myhash
1) "filed1"
2) "hello"
3) "filed2"
4) "world"
# 查看hash表的字段数量
127.0.0.1:6379> HLEN myhash
(integer) 2
# 判断key是否存在
127.0.0.1:6379> HEXISTS myhash filed3
(integer) 0
127.0.0.1:6379> HEXISTS myhash filed1
(integer) 1
# hkeys(只获取key) hvals(只获取value)
127.0.0.1:6379> hkeys myhash
1) "filed1"
2) "filed2"
127.0.0.1:6379> hvals myhash
1) "hello"
2) "world"
# HINCRBY 指定增量
127.0.0.1:6379> hset myhash filed3 5
(integer) 1
127.0.0.1:6379> hget myhash filed3
"5"
127.0.0.1:6379> HINCRBY myhash filed3 1
(integer) 6
127.0.0.1:6379> hget myhash filed3
"6"
127.0.0.1:6379> HINCRBY myhash filed3 -3
(integer) 3
Hash的应用场景:
zset在set的基础上增加了score,set k1 score v1
# zadd 添加一个值
127.0.0.1:6379> ZADD myset 1 one
(integer) 1
127.0.0.1:6379> ZADD myset 1 two
(integer) 1
127.0.0.1:6379> ZADD myset 2 three
(integer) 1
127.0.0.1:6379> ZADD myset 3 four
(integer) 1
127.0.0.1:6379> ZRANGE myset 0 -1
1) "one"
2) "two"
3) "three"
4) "four"
127.0.0.1:6379> ZADD myset 2 five 5 six #批量添加
(integer) 2
127.0.0.1:6379> ZRANGE myset 0 -1
1) "one"
2) "two"
3) "five"
4) "three"
5) "four"
6) "six"
# ZRANGEBYSCORE排序-升序
127.0.0.1:6379> zadd salary 500 zhangsan
(integer) 1
127.0.0.1:6379> zadd salary 5000 lisi
(integer) 1
127.0.0.1:6379> zadd salary 1000 wangwu
(integer) 1
127.0.0.1:6379> ZRANGE salary 0 -1
1) "zhangsan"
2) "wangwu"
3) "lisi"
127.0.0.1:6379> ZRANGEBYSCORE salary -inf +inf
1) "zhangsan"
2) "wangwu"
3) "lisi"
127.0.0.1:6379> ZRANGEBYSCORE salary -inf +inf withscores
1) "zhangsan"
2) "500"
3) "wangwu"
4) "1000"
5) "lisi"
6) "5000"
127.0.0.1:6379> ZRANGEBYSCORE salary (0 5000
1) "zhangsan"
2) "wangwu"
3) "lisi"
#ZREVRANGE 降序
127.0.0.1:6379> ZREVRANGE salary 0 -1 withscores
1) "lisi"
2) "5000"
3) "wangwu"
4) "1000"
5) "zhangsan"
6) "500"
# zcard 查询集合元素个数
127.0.0.1:6379> zcard salary
(integer) 3
# ZCOUNT 统计指定区间的元素数量
127.0.0.1:6379> ZCOUNT salary 500 1000
(integer) 2
Zset使用场景:
以上是Redis五大数据类型的基本命令以及使用场景介绍,更详细可以查看Redis官方文档