Redis基础使用


4个月前 201次点击 来自 后端

1. String

  • 存储:set key value
  • 取值:get key
  • 删除:del key
  • 查看所有键:keys *
  • 增1:incr key
  • 减1:decr key
192.168.25.128:6379> set str1 123
OK
192.168.25.128:6379> set str2 abc
OK
192.168.25.128:6379> set str3 xixi
OK
192.168.25.128:6379> get str1
"123"
192.168.25.128:6379> get str2
"abc"
192.168.25.128:6379> get str3
"xixi"
192.168.25.128:6379> del str1
(integer) 1
192.168.25.128:6379> keys *
 1) "str2"
2) "str3"
192.168.25.128:6379> set str1 3
OK
192.168.25.128:6379> incr str1
(integer) 4

2. Hash

  • 存储: hset key field value
  • 取值: hget key field
  • 查看某个键对应的map里面的所有key: hkeys key
  • 查看某个键对应的map里面的所有的value: hvals key
  • 查看某个键的map: hgetall key
192.168.25.128:6379> hset hone field1 123
(integer) 1
192.168.25.128:6379> hset hone field2 abc
(integer) 1
192.168.25.128:6379> hset hone field3 haha
(integer) 1
192.168.25.128:6379> hget hone field1
"123"
192.168.25.128:6379> hget hone field2
"abc"
192.168.25.128:6379> hget hone field3
"haha"
192.168.25.128:6379> hkeys hone
1) "field1"
2) "field2"
3) "field3"
192.168.25.128:6379> hvals hone
1) "123"
2) "abc"
3) "haha"
192.168.25.128:6379> hgetall hone
1) "field1"
2) "123"
3) "field2"
4) "abc"
5) "field3"
6) "haha"

3. List

  • 存储:push,分为lpush list v1 v2 v3 v4 …(左边添加),rpush list v1 v2 v3 v4 …(右边添加)
  • 取值:pop,分为lpop lpop list(左边取,移除list最左边的值) ,rpop rpop list(右边取,移除list最右边的值)
  • 查看list:lrange key 0 -1(0 -1表示查看所有)
192.168.25.128:6379> lpush list1 1 2 3 4 5 6
(integer) 6
192.168.25.128:6379> rpush list1 a b c d e 
(integer) 11
192.168.25.128:6379> lrange list1 0 -1
 1) "6"
 2) "5"
 3) "4"
 4) "3"
 5) "2"
 6) "1"
 7) "a"
 8) "b"
 9) "c"
10) "d"
11) "e"
192.168.25.128:6379> lpop list1
"6"
192.168.25.128:6379> lrange list1 0 -1
 1) "5"
 2) "4"
 3) "3"
 4) "2"
 5) "1"
 6) "a"
 7) "b"
 8) "c"
 9) "d"
10) "e"
192.168.25.128:6379> rpop list1
"e"
192.168.25.128:6379> lrange list1 0 -1
1) "5"
2) "4"
3) "3"
4) "2"
5) "1"
6) "a"
7) "b"
8) "c"
9) "d"

4. Set

  • 存储:sadd key v1 v2 v3 …
  • 移除:srem key v
  • 查看set集合: smembers key
    另外还提供了差集,交集,并集操作
  • 差集:sdiff seta setb(seta中有setb中没有的元素)
  • 交集:sinter seta setb
  • 并集:sunion seta setb
192.168.25.128:6379> sadd set a b a b c d
(integer) 4
192.168.25.128:6379> srem set a
(integer) 1
192.168.25.128:6379> smembers set
1) "d"
2) "b"
3) "c"
192.168.25.128:6379> sadd seta a b c d e
(integer) 5
192.168.25.128:6379> sadd setb c d e f g 
(integer) 5
192.168.25.128:6379> sdiff seta setb(差集,seta有setb没有)
1) "a"
2) "b"
192.168.25.128:6379> sdiff setb seta (差集,setb有seta没有)
1) "g"
2) "f"
192.168.25.128:6379> sinter seta setb(交集)
1) "d"
2) "e"
3) "c"
192.168.25.128:6379> sunion seta setb(并集)
1) "a"
2) "b"
3) "d"
4) "g"
5) "f"
6) "e"
7) "c"

5. SortedSet

  • 存储:存储的时候要求对set进行排序,需要对存储的每个value值进行打分,默认排序是分数由低到高。zadd key 分数1 v1 分数2 v2 分数3 v3…
  • 取值:取指定的值 zrem key value
  • 取所有的值(不包括分数):zrange key 0 -1,降序取值用zrevrange key 0 -1
  • 取所有的值(带分数):zrange(zrevrange) key 0 -1 withscores
192.168.25.128:6379> zadd zset1 1 a 3 b 2 c 5 d(要求给定分数,从而达到排序效果,默认升序)
(integer) 4
192.168.25.128:6379> zrange zset1 0 -1
1) "a"
2) "c"
3) "b"
4) "d"
192.168.25.128:6379> zrem zset1 a 
(integer) 1
192.168.25.128:6379> zrange zset1 0 -1
1) "c"
2) "b"
3) "d"
192.168.25.128:6379> zrevrange zset1 0 -1(按分数降序排)
1) "d"
2) "b"
3) "c"
192.168.25.128:6379> zrevrange zset1 0 -1 withscores
1) "d"
2) "5"
3) "b"
4) "3"
5) "c"
6) "2"

6. key命令

  • 设置key的过期时间:expired key 过期时间(秒)
  • 查看key的有效剩余时间:ttl key
  • 清除key的过期时间,持久化该key:persist key
    -1:表示持久化
    -2: 表示该key不存在
192.168.25.128:6379> expire zone 60
(integer) 1
192.168.25.128:6379> ttl zone
(integer) 55
192.168.25.128:6379> ttl zone
(integer) 51
192.168.25.128:6379> ttl zone
(integer) 48
192.168.25.128:6379> ttl zone
(integer) 37
192.168.25.128:6379> ttl zone
(integer) 16
192.168.25.128:6379> ttl zone
(integer) -2  -->(该key已经不存在)
192.168.25.128:6379> expire sone 30
(integer) 1
192.168.25.128:6379> ttl sone
(integer) 22
192.168.25.128:6379> persist sone
(integer) 1
192.168.25.128:6379> ttl sone
(integer) -1  -->(该key是持久化的)

Made with in Shangrao,China By Devler.

Copyright © Devler 2012 - 2022

赣ICP备19009883号-1

Top ↑