Redis默认存在16个库,类似于数组下标从0开始(0-15),初始默认使用的是0号库.
select <number>:number的取值是0-15,用来切换不同的库.
一、Key
keys * | 查看当前库中所有的键 |
exists <key> | 判断某个键是否存在,存在返回1,不存在返回0 |
type <key> | 判断键的类型 |
dbsize | 查看当前库中key的数量 |
del <key> | 删除某个键 |
expire <key> <seconds> | 为键设置过期时间,单位为(秒),如果不设置代表永久有效 |
ttl <key> | 查看键还有多久过期,-1代表永不过期,-2代表已失效 |
flushdb | 清空当前库 |
flushall | 清空所有库 |
1.1、keys *:查看当前库中所有的键
127.0.0.1:6379> clear
127.0.0.1:6379> set key1 value1
OK
127.0.0.1:6379> set key2 value2
OK
127.0.0.1:6379> set key3 value3
OK
127.0.0.1:6379> keys *
1) "key1"
2) "key3"
3) "key2"
1.2、exists <key>:判断某个键是否存在
127.0.0.1:6379> set key1 value1 OK 127.0.0.1:6379> set key2 value2 OK 127.0.0.1:6379> set key3 value3 OK 127.0.0.1:6379> exists key1 (integer) 1 127.0.0.1:6379> exists key2 (integer) 1 127.0.0.1:6379> exists key3 (integer) 1 127.0.0.1:6379> exists key4 (integer) 0
1.3、type <key>:判断键的类型
127.0.0.1:6379> set key1 value1
OK
127.0.0.1:6379> hset person name kogmaw
(integer) 1
127.0.0.1:6379> type key1
string
127.0.0.1:6379> type person
hash
1.4、dbsize:查看当前库中key的数量
127.0.0.1:6379> set key1 value1
OK
127.0.0.1:6379> set key2 value2
OK
127.0.0.1:6379> set key3 value3
OK
127.0.0.1:6379> dbsize
(integer) 3
1.5、del <key>:删除某个键
127.0.0.1:6379> set key1 value1
OK
127.0.0.1:6379> set key2 value2
OK
127.0.0.1:6379> set key3 value3
OK
127.0.0.1:6379> keys *
1) "key1"
2) "key3"
3) "key2"
127.0.0.1:6379> del key3
(integer) 1
127.0.0.1:6379> keys *
1) "key1"
2) "key2"
1.6、expire <key> <seconds>:为键设置过期时间,单位(秒),如果不设置代表永久有效
1.7、ttl <key>:查看键还有多久过期,-1代表永久有效,-2代表已经失效
127.0.0.1:6379> set key1 value1
OK
127.0.0.1:6379> set key2 value2
OK
127.0.0.1:6379> expire key1 20
(integer) 1
127.0.0.1:6379> ttl key1
(integer) 16
127.0.0.1:6379> ttl key2
(integer) -1
127.0.0.1:6379> ttl key1
(integer) -2
127.0.0.1:6379> ttl key2
(integer) -1
1.8、flushdb:清空当前库
127.0.0.1:6379> set key1 value1
OK
127.0.0.1:6379> set key2 value2
OK
127.0.0.1:6379> set key3 value3
OK
127.0.0.1:6379> dbsize
(integer) 3
127.0.0.1:6379> keys *
1) "key1"
2) "key3"
3) "key2"
127.0.0.1:6379> flushdb
OK
127.0.0.1:6379> dbsize
(integer) 0
127.0.0.1:6379> keys *
(empty list or set)
1.9、flushall:清空所有库
127.0.0.1:6379> set key01 value01
OK
127.0.0.1:6379> set key02 value02
OK
127.0.0.1:6379> set key03 value03
OK
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> set key11 value11
OK
127.0.0.1:6379[1]> set key12 value12
OK
127.0.0.1:6379[1]> set key13 value13
OK
127.0.0.1:6379[1]> flushall
OK
127.0.0.1:6379[1]> dbsize
(integer) 0
127.0.0.1:6379[1]> keys *
(empty list or set)
127.0.0.1:6379[1]> select 0
OK
127.0.0.1:6379> dbsize
(integer) 0
127.0.0.1:6379> keys *
(empty list or set)
二、String
set <key> <value> | 添加键值对 |
get <key> | 获取某个键对应的值 |
mset <key1> <value1> <key2> <value2>..... | 同时设置一个或者多个key-value对 |
mget <key1> <key2> <key3>..... | 同时获取一个或者多个value |
setnx <key> <value> | 只有key不存在的情况下才设置key-value |
msetnx <key1> <value1> <key2> <value2>..... | 同时设置一个或者多个key-value,当且仅当所有的key都不存在的时候设置,只要有一个key存在则全部失败 |
strlen <key> | 获取某个key对应的value长度 |
setex <key><过期时间><value> | 设置key-value的同时设置key的过期时间 |
incr <key> | 将key存储的数字值增加1 |
decr <key> | 将key存储的数字值减少1 |
incrby <key> 步长 | 将key存储的数字值按照步长增加,步长可以自定义 |
decrby <key> 步长 | 将key存储的数字值按照步长减少,步长可以自定义 |
setrange <key> <起始位置> <value> | 用value值覆盖key存储的字符串值,从起始位置开始 |
getrange <key> <起始位置> <结束位置> | 获取值的范围,类似于java中的substring |
append <key> <value> | 将value追加到key存储的值后面 |
2.1、set <key> <value> / get <key>
127.0.0.1:6379> set key1 value1
OK
127.0.0.1:6379> set key2 value2
OK
127.0.0.1:6379> set key3 value3
OK
127.0.0.1:6379> get key1
"value1"
127.0.0.1:6379> get key2
"value2"
127.0.0.1:6379> get key3
"value3"
2.2、mset <key1> <value1> <key2> <value2>..../ get <key1> <key2> .....
127.0.0.1:6379> mset key1 value1 key2 value2 key3 value3 key4 value4
OK
127.0.0.1:6379> keys *
1) "key4"
2) "key1"
3) "key3"
4) "key2"
127.0.0.1:6379> dbsize
(integer) 4
127.0.0.1:6379> mget key1 key2 key3 key4
1) "value1"
2) "value2"
3) "value3"
4) "value4"
2.3、setnx key value:只有当key不存在的情况下,才会设置key-value
127.0.0.1:6379> set key1 value1
OK
127.0.0.1:6379> set key2 value2
OK
127.0.0.1:6379> dbsize
(integer) 2
127.0.0.1:6379> keys *
1) "key1"
2) "key2"
127.0.0.1:6379> setnx key3 value3
(integer) 1
127.0.0.1:6379> dbsize
(integer) 3
127.0.0.1:6379> keys *
1) "key1"
2) "key3"
3) "key2"
127.0.0.1:6379> setnx key2 value22222
(integer) 0
127.0.0.1:6379> dbsize
(integer) 3
127.0.0.1:6379> keys *
1) "key1"
2) "key3"
3) "key2"
2.4、msetnx <key1> <value1> <key2> <value2> ....同时设置一个或者多个key-value对,当且仅当这些key都不存在的情况才设置,只要有一个key存在则全部设置失败
127.0.0.1:6379> mset key1 value1 key2 value2 key3 value3
OK
127.0.0.1:6379> keys *
1) "key1"
2) "key3"
3) "key2"
127.0.0.1:6379> msetnx key3 value333 key4 value4 key5 value5
(integer) 0
127.0.0.1:6379> keys *
1) "key1"
2) "key3"
3) "key2"
127.0.0.1:6379> mset key4 value4 key5 value5 key6 value6
OK
127.0.0.1:6379> keys *
1) "key6"
2) "key4"
3) "key2"
4) "key3"
5) "key1"
6) "key5"
2.5、strlen <key>:获取key对应的value的长度
127.0.0.1:6379> set key hello,xiaomaomao
OK
127.0.0.1:6379> strlen key
(integer) 16
2.6、setex <key> <过期时间> <value>:设置key-value的同时设置key的有效过期时间
127.0.0.1:6379> setex key1 20 value1
OK
127.0.0.1:6379> ttl key1
(integer) 16
127.0.0.1:6379> ttl key1
(integer) 2
2.7、getset <key> <value>:以旧换新,设置新值的同时获取旧值
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> getset key1 value1
(nil)
127.0.0.1:6379> set key1 value1
OK
127.0.0.1:6379> getset key1 value11111
"value1"
127.0.0.1:6379> get key1
"value11111"
2.8、incr <key>/decr <key>/incrby <key> 步长/decrby <key> 步长(只能对数字进行操作)
127.0.0.1:6379> set key 100
OK
127.0.0.1:6379> incr key
(integer) 101
127.0.0.1:6379> incr key
(integer) 102
127.0.0.1:6379> decr key
(integer) 101
127.0.0.1:6379> decr key
(integer) 100
127.0.0.1:6379> incrby key 5
(integer) 105
127.0.0.1:6379> incrby key 5
(integer) 110
127.0.0.1:6379> decrby key 3
(integer) 107
127.0.0.1:6379> decrby key 3
(integer) 104
127.0.0.1:6379> decrby key 3
(integer) 101
2.9、setrange <key> <起始位置> <value>:从起始位置开始,用value值覆盖原来key存储的值
127.0.0.1:6379> set key abcdefghijklmn
OK
127.0.0.1:6379> get key
"abcdefghijklmn"
127.0.0.1:6379> setrange key 3 12345
(integer) 14
127.0.0.1:6379> get key
"abc12345ijklmn"
2.10、getrange <key> <开始位置> <结束位置>:获取值的范围,类似于substring
127.0.0.1:6379> set key abcdefghijklmn
OK
127.0.0.1:6379> get key
"abcdefghijklmn"
127.0.0.1:6379> getrange key 2 6
"cdefg"
2.11、append <key> <value>:将value追加到key存储的值后面
127.0.0.1:6379> set key 123456
OK
127.0.0.1:6379> get key
"123456"
127.0.0.1:6379> append key abcd
(integer) 10
127.0.0.1:6379> get key
"123456abcd"
三、List
lpush / rpush <key> <value1> <value2> | 从左边/右边插入一个或者多个值 |
lpop / rpop <key> | 从左边/右边弹出一个值,值在键在,值光键亡 |
rpoplpush <key1> <key2> | 从key1列表的右边弹出一个值插入到key2的左边 |
lrange <key> <start> <stop> | 按照索引下标获取值,lrange <key> 0 -1获取所有值 |
lindex <key> <index> | 按照索引下标获得元素(从左到右) |
llen <key> | 获得列表长度 |
lrem <key> <n> <value> | 从左边删除n个value(从左到右) |
linsert <key> before/after <value> <newvalue> | 在<value>的前面/后面插入<newvalue> 插入值 |
3.1、lpush / rpush <key> <value1> <value2>:从左边/右边插入一个或者多个值
127.0.0.1:6379> lpush key1 1 2 3 4 5 6 (integer) 6 127.0.0.1:6379> lrange key1 0 -1 1) "6" 2) "5" 3) "4" 4) "3" 5) "2" 6) "1" 127.0.0.1:6379> rpush key2 a b c d e f (integer) 6 127.0.0.1:6379> lrange key2 0 -1 1) "a" 2) "b" 3) "c" 4) "d" 5) "e" 6) "f"
3.2、lpop/rpop:从左边/右边吐出一个值,值在键在,值光键亡
127.0.0.1:6379> lpush key1 f e c b a
(integer) 5
127.0.0.1:6379> lrange key1 0 -1
1) "a"
2) "b"
3) "c"
4) "e"
5) "f"
127.0.0.1:6379> lpop key1
"a"
127.0.0.1:6379> lpop key1
"b"
127.0.0.1:6379> rpop key1
"f"
127.0.0.1:6379> rpop key1
"e"
127.0.0.1:6379> rpop key1
"c"
127.0.0.1:6379> rpop key1
(nil)
127.0.0.1:6379> lrange key1 0 -1
(empty list or set)
127.0.0.1:6379> keys *
(empty list or set)
3.3、rpoplpush <key1> <key2>:从key1列表右边弹出一个值,添加到key2左边
127.0.0.1:6379> lpush key1 5 4 3 2 1
(integer) 5
127.0.0.1:6379> lrange key1 0 -1
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
127.0.0.1:6379> rpush key2 a b c d e
(integer) 5
127.0.0.1:6379> lrange key2 0 -1
1) "a"
2) "b"
3) "c"
4) "d"
5) "e"
127.0.0.1:6379> rpoplpush key1 key2
"5"
127.0.0.1:6379> lrange key1 0 -1
1) "1"
2) "2"
3) "3"
4) "4"
127.0.0.1:6379> lrange key2 0 -1
1) "5"
2) "a"
3) "b"
4) "c"
5) "d"
6) "e"
3.4、lrange <key> <start> <stop>:按照索引下标获取元素
127.0.0.1:6379> rpush key a b c d e f
(integer) 6
127.0.0.1:6379> lrange key 0 2
1) "a"
2) "b"
3) "c"
127.0.0.1:6379> lrange key 2 4
1) "c"
2) "d"
3) "e"
127.0.0.1:6379> lrange key 0 -1
1) "a"
2) "b"
3) "c"
4) "d"
5) "e"
6) "f"
3.5、len <key> / lindex <key> <index>
127.0.0.1:6379> rpush key a b c d e f g h i j k
(integer) 11
127.0.0.1:6379> lrange key 0 -1
1) "a"
2) "b"
3) "c"
4) "d"
5) "e"
6) "f"
7) "g"
8) "h"
9) "i"
10) "j"
11) "k"
127.0.0.1:6379> llen key
(integer) 11
127.0.0.1:6379> lindex key 0
"a"
127.0.0.1:6379> lindex key 1
"b"
127.0.0.1:6379> lindex key 10
"k"
3.6、lrem <key> <n> <value>:从左边删除key存储的列表中的n个value值
127.0.0.1:6379> rpush key a b c d a a b b c c d
(integer) 11
127.0.0.1:6379> lrange key 0 -1
1) "a"
2) "b"
3) "c"
4) "d"
5) "a"
6) "a"
7) "b"
8) "b"
9) "c"
10) "c"
11) "d"
127.0.0.1:6379> lrem key 3 a // 从左边开始删除key存储的列表中的3个a
(integer) 3
127.0.0.1:6379> lrange key 0 -1
1) "b"
2) "c"
3) "d"
4) "b"
5) "b"
6) "c"
7) "c"
8) "d"
127.0.0.1:6379> lrem key 2 b // 从左边开始删除key存储的列表中的两个b
(integer) 2
127.0.0.1:6379> lrange key 0 -1
1) "c"
2) "d"
3) "b"
4) "c"
5) "c"
6) "d"
3.7、linsert <key> before/after <value> <newvalue> : 在<value>的前面/后面插入<newvalue> 插入值
127.0.0.1:6379> rpush key a b c d
(integer) 4
127.0.0.1:6379> lrange key 0 -1
1) "a"
2) "b"
3) "c"
4) "d"
127.0.0.1:6379> linsert key before b 123 // 在 b 前面插入 123
(integer) 5
127.0.0.1:6379> lrange key 0 -1
1) "a"
2) "123"
3) "b"
4) "c"
5) "d"
127.0.0.1:6379> linsert key after c 456 // 在 c后面插入 456
(integer) 6,
127.0.0.1:6379> lrange key 0 -1
1) "a"
2) "123"
3) "b"
4) "c"
5) "456"
6) "d"
四、Hash
1、redis的hash数据结构,其实就是string的升级版,它把string 数据结构的key value,中的value类型升级为hash(和java的hash一样的结构)
2、Map<String, HashMap<String,String>> hash=new HashMap<String,HashMap<String,String>>();
每个 hash的存储大小: 可以存储 2的(32 - 1)方的 键值对(40多亿)
hset <key> <field> <value> | 给key对应的hash中的field键赋值value |
hget <key> <field> | 获取key对应的field存储的value值 |
hmset <key> <field1> <value1> <field2> <key2>... | 同时设置多个值 |
hget <key> <field1> <field2>... | 同时获取多个值 |
hkeys <key> / hvals <key> | 查看所有的field值和所有的value值 |
hexists <key> <field> | 查看哈希表key中给定的field是否存在 |
hsetnx <key> <field> | 当field不存在时,为field对应的value设置值 |
hincrby <key> <field> increment | 为field存储的value值添加增量,increment可取负数 |
4.1、hset <key> <field> <value>:获取key对应的field字段所对应的value值
4.2、hget <key> <field>:获取key对应的field存储的value值
127.0.0.1:6379> hset role role:9527:id 9527 // 设置值
(integer) 1
127.0.0.1:6379> hget role role:9527:id // 获取值
"9527"
4.3、hmset <key> <field1> <value1> <field2> <value2>...:同时设置多个值
4.4、hmget <key> <field1> <field2>...:同时获取多个值
4.5、hkeys <key>:获取所有的field值
4.6、hvals <key>:获取所有的value值
127.0.0.1:6379> hmset role role:9527:id 9527 role:9527:name xiaomaomao role:9527:age 27 // 设置多个value值 OK 127.0.0.1:6379> hmget role role:9527:id role:9527:name role:9527:age // 同时获取多个field存储的value值 1) "9527" 2) "xiaomaomao" 3) "27" 127.0.0.1:6379> hkeys role // 获取key对应的所有的field值 1) "role:9527:id" 2) "role:9527:name" 3) "role:9527:age"127.0.0.1:6379> hvals role // 获取所有的value值 1) "9527" 2) "xiaomaomao" 3) "27"
4.7、hexists <key> <field> :判断key对应的field是否存在
4.8、hsetnx <key> <filed> <value>:当field不存在时,为field设置值
4.9、hincrby <key> <field> increment:为field存储的value值添加增量
127.0.0.1:6379> hkeys role // 获取所有的field值 1) "role:9527:id" 2) "role:9527:name" 3) "role:9527:age" 127.0.0.1:6379> hvals role // 获取所有的value值 1) "9527" 2) "xiaomaomao" 3) "27" 127.0.0.1:6379> hexists role role:9527:name // 判断field role:9527:name是否存在 (integer) 1 127.0.0.1:6379> hexists role role:9527:birthday // 判断field role:9527:birthday是否存在 (integer) 0 127.0.0.1:6379> hincrby role role:9527:age 2 // 为field role:9527:age存储的value值增加2 (integer) 29 127.0.0.1:6379> hincrby role role:9527:age 2 (integer) 31 127.0.0.1:6379> hincrby role role:9527:age -3 // 为field role:9527:age存储的value值增加-3 (integer) 28 127.0.0.1:6379> hincrby role role:9527:age -3 (integer) 25 127.0.0.1:6379> hincrby role role:9527:age -3 (integer) 22 127.0.0.1:6379> hsetnx role role:9527:birthday 10.10 // 因为原来的key中的field不存在 role:9527:birthday,所以能设置值10.10 (integer) 1 127.0.0.1:6379> hsetnx role role:9527:id 6 // 因为已经存在了field role:9527:id,所以设置值失败了 (integer) 0
五、SET
sadd <key> <value1> <value2> | 往集合<key>中添加一个或多个member,如果集合中已经存在该member,则不会重复添加 |
smembers <key> | 取出集合<key>中的所有member |
sismember <key> <member> | 判断集合<key>中是否存在该member,存在返回1,不存在返回0 |
srem <key> <member> | 从集合<key>中移除指定的memeber |
scard <key> | 获取集合<key>中的元素个数 |
spop <key> <count> | 从集合<key>中吐出count个值,如果count大于scard <key>则吐出全部的member,吐完了集合为空 |
srandmemeber <key> <n> | 从集合<key>中随机吐出n个值,不会删除集合key中的元素 |
sinter <key1> <key2> | 返回两个集合<key1> <key2>的交集 |
sunion <key1> <key2> | 返回两个集合<key1> <key2>的并集 |
sdiff <key1> <key2> | 返回两个集合<key1> <key2>的差集 |
5.1、sadd <key> <value1> <value2>:将一个或者多个member元素添加到集合key中,如果集合中已经存在member,则忽略不再添加
5.2、smembers <key>:取出集合<key>中的所有元素
5.3、sismember <key> <member>:判断集合中是否存在该member,如果存在返回1,如果不存在返回0
5.4、srem <key> <member>:从集合<key>中移除某一个member
5.5、scard <key>:获取集合<key>中元素的个数
5.6、spop <key> <count>:随机从集合<key>中吐出count个值,如果count大于集合<key>的总数,则吐出全部的值
5.7、srandmember <key> <n>:随机从key中吐出n个值,但是不会删除原来的集合中的元素
127.0.0.1:6379> sadd world China American England France Germany Australia // 往集合world中添加member China American England France Germany Australia
(integer) 6
127.0.0.1:6379> smembers world //获取集合world中所有的member
1) "France"
2) "American"
3) "Germany"
4) "China"
5) "England"
6) "Australia"
127.0.0.1:6379> scard world // 获取集合world中所有member的个数
(integer) 6
127.0.0.1:6379> sismember world China // 判断China是不是world中的元素,如果是返回1
(integer) 1
127.0.0.1:6379> sismember world Japan // 判断Japan是不是world中的元素,如果不是返回0
(integer) 0
127.0.0.1:6379> srem world Australis // 从集合world中移除member Australia
(integer) 0
127.0.0.1:6379> smembers world
1) "Germany"
2) "China"
3) "France"
4) "England"
5) "Australia"
6) "American"
127.0.0.1:6379> srandmember world 2 // 随机从集合world中弹出2个member,弹出后不会改变原来的集合world
1) "England"
2) "France"
127.0.0.1:6379> smembers world
1) "Germany"
2) "China"
3) "France"
4) "England"
5) "Australia"
6) "American"
127.0.0.1:6379> spop world 2 // 从集合world中吐出两个member,吐出之后这两个member将会从集合world中永久移除
1) "Australia"
2) "China"
127.0.0.1:6379> smembers world
1) "Germany"
2) "France"
3) "England"
4) "American"
127.0.0.1:6379> spop world 4
1) "Germany"
2) "England"
3) "France"
4) "American"
127.0.0.1:6379> smembers world
(empty list or set)
5.8、sinter <key1> <key2>:返回两个集合的交集
5.9、sunion <key1> <key2>:返回两个集合的的并集
5.10 sdiff <key1> <key2>:返回两个集合的差集
127.0.0.1:6379> sadd key1 1 2 3 4 5 // 向集合key1中添加member 1 2 3 4 5
(integer) 5
127.0.0.1:6379> smembers key1
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
127.0.0.1:6379> sadd key2 2 4 6 8 10 // 向集合key2中添加member 2 4 6 8 10
(integer) 5
127.0.0.1:6379> smembers key2
1) "2"
2) "4"
3) "6"
4) "8"
5) "10"
127.0.0.1:6379> sinter key1 key2 // 取集合key1和集合key2的交集
1) "2"
2) "4"
127.0.0.1:6379> sunion key1 key2 // 取集合key1和集合key2的并集
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
6) "6"
7) "8"
8) "10"
127.0.0.1:6379> sdiff key1 key2 // 取集合key1与集合key2的差集
1) "1"
2) "3"
3) "5"
127.0.0.1:6379> sdiff key2 key1 // 取集合key2与集合key1的差集
1) "6"
2) "8"
3) "10"