127.0.0.1:6379> set lin 'hello'
OK
127.0.0.1:6379> get lin
"hello"
127.0.0.1:6379> getrange lin 1 3
"ell"
127.0.0.1:6379> setbit lin 7 0
(integer) 1
127.0.0.1:6379> setbit lin 7 1
(integer) 0
127.0.0.1:6379> getbit lin 1
(integer) 1
127.0.0.1:6379> getbit lin 0
(integer) 0
127.0.0.1:6379> mset lin1 'hello word' lin2 "I am using redis"
OK
127.0.0.1:6379> mget lin lin1 lin2
1) "hello"
2) "hello word"
3) "I am using redis"
127.0.0.1:6379> getset lin redis-cli
"hello"
127.0.0.1:6379> setex lin 120 'lin'
OK
127.0.0.1:6379> ttl lin
(integer) 115
127.0.0.1:6379> get lin
"lin"
127.0.0.1:6379> setnx lin3 'lin3'
(integer) 1
127.0.0.1:6379> setnx lin3 'lin4'
(integer) 0
127.0.0.1:6379> get lin1
"hello word"
127.0.0.1:6379>
127.0.0.1:6379> setrange lin1 6 redis
(integer) 11
127.0.0.1:6379> get lin1
"hello redis"
127.0.0.1:6379> get lin1
"hello redis"
127.0.0.1:6379> strlen lin1
(integer) 11
127.0.0.1:6379> msetnx lin 'redis' lin4 'use redis'
(integer) 1
127.0.0.1:6379> msetnx lin 'redis' lin4 'use redis'
(integer) 0
redis> PSETEX mykey 1000 "Hello"
"OK"
redis> PTTL mykey
(integer) 999
redis> GET mykey
"Hello"
127.0.0.1:6379> set name 11
OK
127.0.0.1:6379> get name
"11"
127.0.0.1:6379> incr name
(integer) 12
127.0.0.1:6379> get name
"12"
127.0.0.1:6379> incrby name 10
(integer) 22
127.0.0.1:6379> get name
"22"
127.0.0.1:6379> incrbyfloat name 0.5
"22.5"
127.0.0.1:6379> set num 10
OK
127.0.0.1:6379> decr num
(integer) 9
127.0.0.1:6379> decrby num 3
(integer) 6
127.0.0.1:6379> get num
"6"
127.0.0.1:6379> get lin2
"I am using redis"
127.0.0.1:6379> append lin2 ' end'
(integer) 20
127.0.0.1:6379> get lin2
"I am using redis end"
127.0.0.1:6379> lpush list1 'list' 'list1' 'list2'
(integer) 3
127.0.0.1:6379> lpop list1
"list2"
127.0.0.1:6379> lrange list1 0 10
1) "list1"
2) "list"
127.0.0.1:6379> lrange list1 0 10
1) "list1"
2) "list"
127.0.0.1:6379>
127.0.0.1:6379> lpush list1 list2
(integer) 3
127.0.0.1:6379> lrange list1 0 10
1) "list2"
2) "list1"
3) "list"
127.0.0.1:6379> rpush list1 'list3' 'list4'
(integer) 5
127.0.0.1:6379> lrange list1 0 10
1) "list2"
2) "list1"
3) "list"
4) "list3"
5) "list4"
127.0.0.1:6379> rpop list1
"list"
127.0.0.1:6379> rpushx list1 'list'
(integer) 3
127.0.0.1:6379> lrange list1 0 10
1) "list2"
2) "list1"
3) "list"
127.0.0.1:6379> llen list1
(integer) 5
127.0.0.1:6379> lrange list1 0 10
1) "list2"
2) "list1"
3) "list"
4) "list3"
5) "list4"
127.0.0.1:6379> linsert list1 before 'list4' 'list5'
(integer) 6
127.0.0.1:6379> lrange list1 0 10
1) "list2"
2) "list1"
3) "list"
4) "list3"
5) "list5"
6) "list4"
127.0.0.1:6379> linsert list1 after 'list4' 'list6'
(integer) 7
127.0.0.1:6379> lrange list1 0 10
1) "list2"
2) "list1"
3) "list"
4) "list3"
5) "list5"
6) "list4"
7) "list6"
127.0.0.1:6379> lrange list1 0 10
1) "list2"
2) "list1"
3) "list"
4) "list3"
5) "list5"
6) "list4"
7) "list6"
127.0.0.1:6379> lindex list1 3
"list3"
127.0.0.1:6379> lrange list1 0 10
1) "list2"
2) "list1"
3) "list"
4) "list3"
5) "list5"
6) "list4"
7) "list6"
127.0.0.1:6379> lset list1 3 'list7'
OK
127.0.0.1:6379> lrange list1 0 10
1) "list2"
2) "list1"
3) "list"
4) "list7"
5) "list5"
6) "list4"
7) "list6"
127.0.0.1:6379> lrange list1 0 10
1) "list2"
2) "list1"
3) "list"
4) "list7"
5) "list5"
6) "list4"
7) "list6"
127.0.0.1:6379>
127.0.0.1:6379> lrem list1 2 'list'
(integer) 1
127.0.0.1:6379> lrange list1 0 10
1) "list2"
2) "list1"
3) "list7"
4) "list5"
5) "list4"
6) "list6"
127.0.0.1:6379> lrange list1 0 10
1) "list2"
2) "list1"
3) "list7"
4) "list5"
5) "list4"
6) "list6"
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379> ltrim list1 0 2
OK
127.0.0.1:6379> lrange list1 0 10
1) "list2"
2) "list1"
3) "list7"
127.0.0.1:6379> lrange list1 0 10
1) "list2"
2) "list1"
3) "list7"
127.0.0.1:6379> blpop list1 5
1) "list1"
2) "list2"
127.0.0.1:6379> lrange list1 0 10
1) "list1"
2) "list7"
127.0.0.1:6379> lrange list1 0 10
1) "list1"
2) "list7"
127.0.0.1:6379> brpop list1 5
1) "list1"
2) "list7"
127.0.0.1:6379> lrange list1 0 10
1) "list1"
127.0.0.1:6379> rpoplpush list list1
"list"
127.0.0.1:6379> lrange list 0 -1
1) "list5"
2) "list4"
3) "list3"
4) "list2"
5) "list1"
127.0.0.1:6379> lrange list1 0 -1
1) "list"
127.0.0.1:6379> sadd set 'set1' 'set2' 'set3' 'set4' 'set5'
(integer) 5
127.0.0.1:6379> smembers set
1) "set2"
2) "set1"
3) "set4"
4) "set3"
5) "set5"
127.0.0.1:6379> scard set
(integer) 5
127.0.0.1:6379> srandmember set
"set2"
127.0.0.1:6379> sismember set set2
(integer) 1
127.0.0.1:6379> sismember set set6
(integer) 0
127.0.0.1:6379> smembers set
1) "set2"
2) "set5"
3) "set1"
4) "set3"
5) "set4"
127.0.0.1:6379> srem set 'set1'
(integer) 1
127.0.0.1:6379> smembers set
1) "set2"
2) "set5"
3) "set3"
4) "set4"
127.0.0.1:6379> SMEMBERS set
1) "set2"
2) "set5"
3) "set3"
4) "set4"
127.0.0.1:6379> SMEMBERS set1
1) "set5"
2) "set2"
3) "set4"
127.0.0.1:6379> SDIFF set set1
1) "set3"
SDIFFSTORE destination key1 [key2]
127.0.0.1:6379> sdiffstore set1 set
(integer) 4
127.0.0.1:6379> sdiff set
1) "set5"
2) "set2"
3) "set4"
4) "set3"
127.0.0.1:6379> sdiff set1
1) "set2"
2) "set5"
3) "set3"
4) "set4"
127.0.0.1:6379> SMEMBERS set
1) "set2"
2) "set5"
3) "set3"
4) "set4"
127.0.0.1:6379> SMEMBERS set1
1) "set5"
2) "set2"
3) "set4"
127.0.0.1:6379> sinter set set1
1) "set5"
2) "set2"
3) "set4"
127.0.0.1:6379> SMEMBERS set
1) "set2"
2) "set5"
3) "set3"
4) "set4"
127.0.0.1:6379> SMEMBERS set1
1) "set5"
2) "set2"
3) "set4"
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379> SINTERSTORE set3 set set1
(integer) 3
127.0.0.1:6379> SMEMBERS set3
1) "set2"
2) "set5"
3) "set4"
127.0.0.1:6379> SMEMBERS set
1) "set2"
2) "set5"
3) "set3"
4) "set4"
127.0.0.1:6379> SADD set4 'set6' 'set7' 'set8'
(integer) 3
127.0.0.1:6379> SUNION set set4
1) "set2"
2) "set5"
3) "set7"
4) "set6"
5) "set3"
6) "set4"
7) "set8"
127.0.0.1:6379> SUNIONSTORE set5 set set4
(integer) 7
127.0.0.1:6379> SMEMBERS set5
1) "set2"
2) "set5"
3) "set7"
4) "set6"
5) "set3"
6) "set4"
7) "set8"
SMOVE source destination member
127.0.0.1:6379> SMEMBERS set
1) "set2"
2) "set5"
3) "set3"
4) "set4"
127.0.0.1:6379> SMEMBERS set1
1) "set5"
2) "set2"
3) "set4"
127.0.0.1:6379> SMOVE set set1 'set3'
(integer) 1
127.0.0.1:6379> SMEMBERS set1
1) "set5"
2) "set2"
3) "set3"
4) "set4"
127.0.0.1:6379> spop set1
"set5"
127.0.0.1:6379> SMEMBERS set1
1) "set2"
2) "set3"
3) "set4"
127.0.0.1:6379> SSCAN set1 0
1) "0"
2) 1) "set2"
2) "set3"
3) "set4"
127.0.0.1:6379> ZADD zset 1 'one' 2 'two'
(integer) 2
127.0.0.1:6379> ZADD zset 4 'four'
(integer) 1
127.0.0.1:6379> ZRANGE zset 0 10
1) "one"
2) "two"
3) "four"
127.0.0.1:6379> ZRANGE zset 0 10 WITHSCORES
1) "one"
2) "1"
3) "two"
4) "2"
5) "four"
6) "4"
127.0.0.1:6379> ZCARD zset
(integer) 3
127.0.0.1:6379> ZCOUNT zset 0 10
(integer) 3
ZINCRBY key increment member
127.0.0.1:6379> ZRANGE zset 0 10 WITHSCORES
1) "one"
2) "1"
3) "two"
4) "2"
5) "four"
6) "4"
127.0.0.1:6379> ZINCRBY zset 2 'four'
"6"
127.0.0.1:6379> ZRANGE zset 0 10 WITHSCORES
1) "one"
2) "1"
3) "two"
4) "2"
5) "four"
6) "6"
127.0.0.1:6379> ZLEXCOUNT zset - +
(integer) 3
127.0.0.1:6379> ZLEXCOUNT zset [o [f
(integer) 0
127.0.0.1:6379> ZLEXCOUNT zset [a [z
(integer) 3
127.0.0.1:6379> ZRANGE zset 0 10 WITHSCORES
1) "one"
2) "1"
3) "two"
4) "2"
5) "four"
6) "6"
127.0.0.1:6379> ZRANGE zset 0 10
1) "one"
2) "two"
3) "four"
127.0.0.1:6379> ZRANGEBYLEX zset - +
1) "one"
2) "two"
3) "four"
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379> ZRANGEBYLEX zset [a [f
(empty array)
127.0.0.1:6379> ZRANGEBYLEX zset [a [z
1) "one"
2) "two"
3) "four"
127.0.0.1:6379> ZRANGEBYSCORE zset 0 10
1) "one"
2) "two"
3) "four"
127.0.0.1:6379> ZRANGEBYSCORE zset 0 5
1) "one"
2) "two"
127.0.0.1:6379> ZRANK zset one
(integer) 0
127.0.0.1:6379> ZRANK zset two
(integer) 1
127.0.0.1:6379> ZRANK zset four
(integer) 2
127.0.0.1:6379> ZREM zset one
(integer) 1
127.0.0.1:6379> ZRANGE zset 0 10
1) "two"
2) "four"
127.0.0.1:6379> ZRANGE zset 0 10 withscores
1) "two"
2) "2"
3) "four"
4) "6"
127.0.0.1:6379> ZREMRANGEBYLEX zset [a [z
(integer) 2
127.0.0.1:6379> ZRANGE zset 0 10 withscores
(empty array)
127.0.0.1:6379> ZADD zset 0 one 6 two 5 three 3 four 2 five 4 fix
(integer) 6
127.0.0.1:6379> ZRANGE zset 0 10 withscores
1) "one"
2) "0"
3) "five"
4) "2"
5) "four"
6) "3"
7) "fix"
8) "4"
9) "three"
10) "5"
11) "two"
12) "6"
127.0.0.1:6379> ZREMRANGEBYRANK zset 0 10
(integer) 6
127.0.0.1:6379> ZRANGE zset 0 10 withscores
(empty array)
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379> ZADD zset 0 one 6 two 5 three 3 four 2 five 4 fix
(integer) 6
127.0.0.1:6379> ZREMRANGEBYRANK zset 0 3
(integer) 4
127.0.0.1:6379> ZRANGE zset 0 10 withscores
1) "three"
2) "5"
3) "two"
4) "6"
127.0.0.1:6379> ZADD zset 0 one 6 two 5 three 3 four 2 five 4 fix
(integer) 6
127.0.0.1:6379> ZRANGE zset 0 10 withscores
1) "one"
2) "0"
3) "five"
4) "2"
5) "four"
6) "3"
7) "fix"
8) "4"
9) "three"
10) "5"
11) "two"
12) "6"
127.0.0.1:6379> ZREMRANGEBYSCORE zset 0 3
(integer) 3
127.0.0.1:6379> ZRANGE zset 0 10 withscores
1) "fix"
2) "4"
3) "three"
4) "5"
5) "two"
6) "6"
127.0.0.1:6379> ZRANGE zset 0 10 withscores
1) "one"
2) "0"
3) "five"
4) "2"
5) "four"
6) "3"
7) "fix"
8) "4"
9) "three"
10) "5"
11) "two"
12) "6"
127.0.0.1:6379> ZREVRANGE zset 0 10
1) "two"
2) "three"
3) "fix"
4) "four"
5) "five"
6) "one"
127.0.0.1:6379> ZREVRANGE zset 0 10 WITHSCORES
1) "two"
2) "6"
3) "three"
4) "5"
5) "fix"
6) "4"
7) "four"
8) "3"
9) "five"
10) "2"
11) "one"
12) "0"
127.0.0.1:6379> ZREVRANGEBYSCORE zset 10 0 WITHSCORES
1) "two"
2) "6"
3) "three"
4) "5"
5) "fix"
6) "4"
7) "four"
8) "3"
9) "five"
10) "2"
11) "one"
12) "0"
127.0.0.1:6379> ZREVRANK zset five
(integer) 4
127.0.0.1:6379> ZSCORE zset five
"2"
127.0.0.1:6379> ZRANGE zset 0 10
1) "one"
2) "five"
3) "four"
4) "fix"
5) "three"
6) "two"
127.0.0.1:6379> ZRANGE zset1 0 10
1) "one"
2) "two"
3) "nink"
4) "server"
5) "link"
127.0.0.1:6379> ZINTERSTORE zset3 2 zset1 zset
(integer) 2
127.0.0.1:6379>
127.0.0.1:6379> ZRANGE zset3 0 10
1) "one"
2) "two"
127.0.0.1:6379> ZRANGE zset 0 10
1) "one"
2) "five"
3) "four"
4) "fix"
5) "three"
6) "two"
127.0.0.1:6379> ZRANGE zset1 0 10
1) "one"
2) "two"
3) "nink"
4) "server"
5) "link"
127.0.0.1:6379> ZUNIONSTORE zset2 2 zset zset1
(integer) 9
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379> ZRANGE zset2 0 10
1) "one"
2) "five"
3) "four"
4) "fix"
5) "three"
6) "nink"
7) "server"
8) "link"
9) "two"
127.0.0.1:6379> ZSCAN zset2 0
1) "0"
2) 1) "one"
2) "0"
3) "five"
4) "2"
5) "four"
6) "3"
7) "fix"
8) "4"
9) "three"
10) "5"
11) "nink"
12) "7"
13) "server"
14) "8"
15) "link"
16) "9"
17) "two"
18) "12"
127.0.0.1:6379> HSET hset name 'lin'
(integer) 1
127.0.0.1:6379> HGETALL hset
1) "name"
2) "lin"
127.0.0.1:6379> HGET hset name
"lin"
127.0.0.1:6379> HGETALL hset
1) "name"
2) "lin"
127.0.0.1:6379> HEXISTS hset name
(integer) 1
127.0.0.1:6379> HEXISTS hset na
(integer) 0
127.0.0.1:6379> HSETNX hset name 'hello'
(integer) 0
127.0.0.1:6379> HSETNX hset age 18
(integer) 1
127.0.0.1:6379> HGETALL hset
1) "name"
2) "lin"
3) "age"
4) "18"
127.0.0.1:6379> HKEYS hset
1) "name"
2) "age"
127.0.0.1:6379> HVALS hset
1) "lin"
2) "18"
127.0.0.1:6379> HLEN hset
(integer) 2
127.0.0.1:6379> HMGET hset name age
1) "lin"
2) "18"
127.0.0.1:6379> HMSET hset gander '男' date '2023-7-21'
OK
127.0.0.1:6379>
127.0.0.1:6379> HVALS hset
1) "lin"
2) "18"
3) "\xe7\x94\xb7"
4) "2023-7-21"
127.0.0.1:6379> HINCRBY hset age 3
(integer) 21
127.0.0.1:6379> HVALS hset
1) "lin"
2) "21"
3) "\xe7\x94\xb7"
4) "2023-7-21"
127.0.0.1:6379> HINCRBYFLOAT hset float 1.5
"14"
127.0.0.1:6379> HGETALL hset
1) "name"
2) "lin"
3) "age"
4) "21"
5) "gander"
6) "\xe7\x94\xb7"
7) "date"
8) "2023-7-21"
9) "float"
10) "14"
127.0.0.1:6379> HDEL hset gander
(integer) 1
127.0.0.1:6379> HGETALL hset
1) "name"
2) "lin"
3) "age"
4) "21"
5) "date"
6) "2023-7-21"
7) "float"
8) "14"
127.0.0.1:6379> HSCAN hset 1
1) "0"
2) 1) "name"
2) "lin"
3) "age"
4) "21"
5) "date"
6) "2023-7-21"
7) "float"
8) "14"
setbit
127.0.0.1:6379> setbit bit1 10 1
(integer) 0
getbit
127.0.0.1:6379> GETBIT bit1 10
(integer) 1
bitcount [start end]
127.0.0.1:6379> BITCOUNT bit1
(integer) 4
bitop and(or/not/xor) [key…]
127.0.0.1:6379> bitop and dest lin2
(integer) 20
PFADD key element [element ...]
127.0.0.1:6379> PFADD hyper 'redis' 'mysql' 'mongodb'
(integer) 1
PFCOUNT key [key ...]
127.0.0.1:6379> PFCOUNT hyper
(integer) 3
PFMERGE destkey sourcekey [sourcekey ...]
127.0.0.1:6379> PFMERGE hyper hyper1
OK
geoadd key longitude latitude member [longitude latitude member...]
127.0.0.1:6379> geoadd china:city 121.47 31.23 shanghai
(integer) 1
127.0.0.1:6379> geoadd china:city 106.50 29.53 chongqing 114.05 22.52 shenzhen 116.38 39.90 beijing
(integer) 3
geopos key member [member...]
127.0.0.1:6379> GEOPOS china:city shanghai chongqing shenzhen beijing
1) 1) "121.47000163793563843"
2) "31.22999903975783553"
2) 1) "106.49999767541885376"
2) "29.52999957900659211"
3) 1) "114.04999762773513794"
2) "22.5200000879503861"
4) 1) "116.38000041246414185"
2) "39.90000009167092543"
geodist key member1 member2 [m\|km\|ft\|mi]
127.0.0.1:6379> GEODIST china:city shanghai chongqing
"1447673.6920"
georadius key longitude latitude radius [m\|km\|ft\|mi]
127.0.0.1:6379> georadius china:city 110 30 1000 km
1) "chongqing"
2) "shenzhen"
在redis.conf中配置
# The filename where to dump the DB
dbfilename dump.rdb
在redis.conf中配置
# Note that you must specify a directory here, not a file name.
# dir ./
dir '/usr/local/redis/data/' # 文件配置路径
格式:save 秒钟 写操作次数
save 900 1 # 在900s内如果有1条数据被写入,则产生一次快照。
save 300 10 # 在300s内如果有10条数据被写入,则产生一次快照
save 60 10000 # 在60s内如果有10000条数据被写入,则产生一次快照
stop-writes-on-bgsave-error yes # 如果为yes则表示,当备份进程出错的时候,主进程就停止进行接受新的写入操作,这样是为了保护持久化的数据一致性的问题。
在redis.conf中配置
# appendonly no
appendonly yes # 修改为yes
配置选项
选项 | 同步频率 |
---|---|
always | 每个redis写命令都要同步写入硬盘,严重降低redis速度 |
everysec | 每秒执行一次同步显式的将多个写命令同步到磁盘 |
no | 由操作系统决定何时同步 |