Redis 复习笔记

杂项

  • 启动
~ redis-cli -h 127.0.0.1 -p 6379
  • redis 默认有16个库
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]>

redis笔记

键(key)

127.0.0.1:6379[1]> DBSIZE
(integer) 2
127.0.0.1:6379[1]> keys *
1) "queues:default"
2) "queues:default:reserved"
127.0.0.1:6379[1]> set k1 test
OK
127.0.0.1:6379[1]> set k2 test2
OK
127.0.0.1:6379[1]> keys k?
1) "k1"
2) "k2"
127.0.0.1:6379[1]> keys k?
1) "k1"
2) "k2"
127.0.0.1:6379[1]> move k2 2
(integer) 1
127.0.0.1:6379[1]> keys k?
1) "k1"
127.0.0.1:6379[1]> select 2
OK
127.0.0.1:6379[2]> keys *
1) "k2"
127.0.0.1:6379[2]> exists k2
(integer) 1
  • 查看还有多少秒过期 , ttl -> time to live , -1表示用不过期,-2 表示已过期
127.0.0.1:6379[1]> ttl k1
(integer) -1
  • 设置缓存时间,到期后key被清除
127.0.0.1:6379[1]> set k1 test
OK
127.0.0.1:6379[1]> expire k1 100 
(integer) 1
127.0.0.1:6379[1]> ttl k1
(integer) 92
127.0.0.1:6379[1]> get k1
"test"
127.0.0.1:6379[1]> set k1 test
OK
127.0.0.1:6379[1]> set k1 test2
OK
127.0.0.1:6379[1]> get k1
"test2"
127.0.0.1:6379[1]> type k1
string
  • 字符串(string)

Redis 笔记

127.0.0.1:6379[1]> set k2 test2
OK
127.0.0.1:6379[1]> append k2 web
(integer) 8
127.0.0.1:6379[1]> get k2
"test2web"
127.0.0.1:6379[1]> strlen k2
(integer) 8
127.0.0.1:6379[1]> set k3 1
OK
127.0.0.1:6379[1]> incr k3
(integer) 2
127.0.0.1:6379[1]> incr k3
(integer) 3
127.0.0.1:6379[1]> incr k3
(integer) 4

127.0.0.1:6379[1]> decr k3
(integer) 3
127.0.0.1:6379[1]> decr k3
(integer) 2

127.0.0.1:6379[1]> INCRBY k3 10
(integer) 12
127.0.0.1:6379[1]> INCRBY k3 10
(integer) 22

127.0.0.1:6379[1]> DECRBY k3 5
(integer) 17
127.0.0.1:6379[1]> DECRBY k3 5
(integer) 12
  • getrange 获取指定区间范围内的值
127.0.0.1:6379[1]> GETRANGE k2 0 -1
"test2web"
127.0.0.1:6379[1]> GETRANGE k2 0 1
"te"
  • setrange 设定指定区间范围内的值
127.0.0.1:6379[1]> get k2
"test2web"
127.0.0.1:6379[1]> SETRANGE k2 0 xxx
(integer) 8
127.0.0.1:6379[1]> get k2
"xxxt2web"
  • setex(set with expire)键秒值
127.0.0.1:6379[1]> setex k4 10 test4
OK
127.0.0.1:6379[1]> ttl k4
(integer) 6
  • setnx (set if not exists)
127.0.0.1:6379[1]> get k2
"xxxt2web"
127.0.0.1:6379[1]> setnx k2 test
(integer) 0
127.0.0.1:6379[1]> get k2
"xxxt2web"
127.0.0.1:6379[1]> get k5
(nil)
127.0.0.1:6379[1]> setnx k5 test5
(integer) 1
127.0.0.1:6379[1]> get k5
"test5"
127.0.0.1:6379[1]> mset k6 test6 k7 test7
OK
127.0.0.1:6379[1]> get k7
"test7"
127.0.0.1:6379[1]> get k6
"test6"
127.0.0.1:6379[1]> mget k6 k7
1) "test6"
2) "test7"

列表

Redis 笔记

127.0.0.1:6379[1]> lpush list01 1 2 3 4 5
(integer) 5
127.0.0.1:6379[1]> lrange list01 0 -1
1) "5"
2) "4"
3) "3"
4) "2"
5) "1"
127.0.0.1:6379[1]> rpush list02 1 2 3 4 5 
(integer) 5
127.0.0.1:6379[1]> lrange list02 0 -1
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
127.0.0.1:6379[1]> lpop list01 
"5"
127.0.0.1:6379[1]> lpop list02
"1"
127.0.0.1:6379[1]> lrange list02 0 -1
1) "2"
2) "3"
3) "4"
4) "5"
127.0.0.1:6379[1]> lindex list02 4
(nil)
127.0.0.1:6379[1]> lindex list02 3
127.0.0.1:6379[1]> llen list02
(integer) 4
  • l remove
127.0.0.1:6379[1]> rpush list03 1 1 1 2 2 3 3 3 4 5
(integer) 10
127.0.0.1:6379[1]> lrem list03 2 1  #移除 2个1
(integer) 2
127.0.0.1:6379[1]> LRANGE list03 0 -1
1) "1"
2) "2"
3) "2"
4) "3"
5) "3"
6) "3"
7) "4"
8) "5"
127.0.0.1:6379[1]> LPUSH list04 1 2 3 4 5 6 7 8
(integer) 8
127.0.0.1:6379[1]> LTRIM list04 0 3
OK
127.0.0.1:6379[1]> LRANGE list04 0 -1
1) "8"
2) "7"
3) "6"
4) "5"
  • RPOPLPUSH list07 list08 -> rpop list07 lpush list08
127.0.0.1:6379[1]> lpush list07 1 2 3
(integer) 3
127.0.0.1:6379[1]> lpush list08 4 5 6
(integer) 3
127.0.0.1:6379[1]> RPOPLPUSH list07 list08
"1"
127.0.0.1:6379[1]> LRANGE list07
(error) ERR wrong number of arguments for 'lrange' command
127.0.0.1:6379[1]> LRANGE list07 0 -1
1) "3"
2) "2"
127.0.0.1:6379[1]> LRANGE list08 0 -1
1) "1"
2) "6"
3) "5"
4) "4"
127.0.0.1:6379[1]> LPUSH list09 1 2 3 4 5
(integer) 5
127.0.0.1:6379[1]> lset list09 2 b
OK
127.0.0.1:6379[1]> LRANGE list09 0 -1
1) "5"
2) "4"
3) "b"
4) "2"
5) "1"
127.0.0.1:6379[1]> linsert list09 before b a 
(integer) 6
127.0.0.1:6379[1]> linsert list09 after b c 
(integer) 7
127.0.0.1:6379[1]> LRANGE list09 0 -1
1) "5"
2) "4"
3) "a"
4) "b"
5) "c"
6) "2"
7) "1"

集合

Redis 笔记

127.0.0.1:6379[1]> sadd set01 1 1 2 2 3 3
(integer) 3
127.0.0.1:6379[1]> SMEMBERS set01
1) "1"
2) "2"
3) "3"
127.0.0.1:6379[1]> sismember set01 3
(integer) 1
127.0.0.1:6379[1]> sismember set01 a
(integer) 0
127.0.0.1:6379[1]> scard set01
(integer) 3
127.0.0.1:6379[1]> srem set01 3
(integer) 1
127.0.0.1:6379[1]> sadd set02 1 2 3 4 5 6 7 8 9 10
(integer) 10
127.0.0.1:6379[1]> srandmember set02 3
1) "4"
2) "6"
3) "5"
127.0.0.1:6379[1]> srandmember set02 3
1) "6"
2) "3"
3) "2"
127.0.0.1:6379[1]> srandmember set02 3
1) "10"
2) "5"
3) "9"
127.0.0.1:6379[1]> spop set02 
"8"
127.0.0.1:6379[1]> spop set02 
"2"
127.0.0.1:6379[1]> smembers set02
1) "1"
2) "3"
3) "4"
4) "5"
5) "6"
6) "7"
7) "9"
8) "10" 
127.0.0.1:6379[1]> smembers set02
1) "1"
2) "3"
3) "4"
4) "5"
5) "6"
6) "7"
7) "9"
8) "10"
127.0.0.1:6379[1]> sadd set03 a b c d 
(integer) 4
127.0.0.1:6379[1]> smove set02 set03 9
(integer) 1
127.0.0.1:6379[1]> smembers set03 
1) "a"
2) "d"
3) "c"
4) "b"
5) "9"
  • 差集
127.0.0.1:6379[1]>  SADD set03 1 2 3 4 5
(integer) 5
127.0.0.1:6379[1]> SADD set04 2 5 a b
(integer) 4
127.0.0.1:6379[1]> 
127.0.0.1:6379[1]> SDIFF set03 set04 #只在set03中,不在set04中
1) "1"
2) "3"
3) "4"
127.0.0.1:6379[1]> SDIFF set04 set03  #只在set04中,不在set03中
1) "a"
2) "b"
  • 交集
127.0.0.1:6379[1]> sinter set03 set04
1) "5"
2) "2"
127.0.0.1:6379[1]> sinter set04 set03
1) "5"
2) "2"
  • 并集
127.0.0.1:6379[1]> SUNION set03 set04
1) "a"
2) "3"
3) "2"
4) "4"
5) "5"
6) "1"
7) "b"

哈希

Redis 笔记

127.0.0.1:6379[1]> hset user id 11
(integer) 1
127.0.0.1:6379[1]> hget user id
"11"
127.0.0.1:6379[1]> hset user name zhao
(integer) 1
127.0.0.1:6379[1]> hget user name
"zhao"
127.0.0.1:6379[1]> hmset customer id 1 name zhao age 26 
OK
127.0.0.1:6379[1]> hmget customer id name age
1) "1"
2) "zhao"
3) "26"
127.0.0.1:6379[1]> hgetall customer
1) "id"
2) "1"
3) "name"
4) "zhao"
5) "age"
6) "26"
127.0.0.1:6379[1]> hgetall customer
1) "id"
2) "1"
3) "name"
4) "zhao"
5) "age"
6) "26"
127.0.0.1:6379[1]> hdel customer id 
(integer) 1
127.0.0.1:6379[1]> hgetall customer
1) "name"
2) "zhao"
3) "age"
4) "26"
127.0.0.1:6379[1]> hlen customer
(integer) 2
127.0.0.1:6379[1]> hexists customer id
(integer) 0
127.0.0.1:6379[1]> hexists customer name
(integer) 1
127.0.0.1:6379[1]> hkeys customer 
1) "name"
2) "age"
127.0.0.1:6379[1]> hvals customer
1) "zhao"
2) "26"
127.0.0.1:6379[1]> hgetall customer
1) "name"
2) "zhao"
3) "age"
4) "26"
127.0.0.1:6379[1]> hset customer score 90.5
(integer) 1
127.0.0.1:6379[1]> hgetall customer
1) "name"
2) "zhao"
3) "age"
4) "26"
5) "score"
6) "90.5"
127.0.0.1:6379[1]> hincrby customer age 2
(integer) 28
127.0.0.1:6379[1]> hincrbyfloat customer score 1.5
"92"
127.0.0.1:6379[1]> hgetall customer
1) "name"
2) "zhao"
3) "age"
4) "28"
5) "score"
6) "92"
127.0.0.1:6379[1]> hsetnx customer age 26
(integer) 0
127.0.0.1:6379[1]> hsetnx customer email 12345@qq.com
(integer) 1
127.0.0.1:6379[1]> hgetall customer
1) "name"
2) "zhao"
3) "age"
4) "28"
5) "score"
6) "92"
7) "email"
8) "12345@qq.com"

有序集合

Redis 笔记

zadd zset01 60 v1 70 v2 80 v3 90 v4 100 v5
(integer) 5
127.0.0.1:6379[1]> zrange zset01 0 -1
1) "v1"
2) "v2"
3) "v3"
4) "v4"
5) "v5"
127.0.0.1:6379[1]> ZRANGE zset01 0 -1 withscores
 1) "v1"
 2) "60"
 3) "v2"
 4) "70"
 5) "v3"
 6) "80"
 7) "v4"
 8) "90"
 9) "v5"
10) "100"
127.0.0.1:6379[1]> zrangebyscore zset01 60 90
1) "v1"
2) "v2"
3) "v3"
4) "v4"
127.0.0.1:6379[1]> zrangebyscore zset01 60 (90
1) "v1"
2) "v2"
3) "v3"
127.0.0.1:6379[1]> zrangebyscore zset01 (60 (90
1) "v2"
2) "v3"
127.0.0.1:6379[1]> zrange zset01 0 -1 withscores
 1) "v1"
 2) "60"
 3) "v2"
 4) "70"
 5) "v3"
 6) "80"
 7) "v4"
 8) "90"
 9) "v5"
10) "100"
127.0.0.1:6379[1]> zrem zset01 v5
(integer) 1
127.0.0.1:6379[1]> zrange zset01 0 -1 withscores
1) "v1"
2) "60"
3) "v2"
4) "70"
5) "v3"
6) "80"
7) "v4"
8) "90"
127.0.0.1:6379[1]> zcard zset01
(integer) 4
127.0.0.1:6379[1]> zcount zset01 60 80
(integer) 3
127.0.0.1:6379[1]> zcount zset01 60 80
(integer) 3
127.0.0.1:6379[1]> zrank zset01 v4
(integer) 3
127.0.0.1:6379[1]> zscore zset01 v4
"90"
127.0.0.1:6379[1]> zrevrange zset01 0 -1
1) "v4"
2) "v3"
3) "v2"
4) "v1"
127.0.0.1:6379[1]> zrange zset01 0 -1
1) "v1"
2) "v2"
3) "v3"
本作品采用《CC 协议》,转载必须注明作者和本文链接
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!