redis
进入控制台
- redis-cil
- 需要输入密码的时候可以是用
-a
redis-cil -a abcd1234
redis 数据类型
- string
- hash
- list
- set
- zset( 有序集合)
键
select [index]
切换库 类似于 db 的use dbname
-
keys [pattern]
查看当前的键127.0.0.1:6379[1]> keys *
1) "test1"
2) "test" -
del key
删除键127.0.0.1:6379[1]> del test
(integer) 1 -
dump key
返回序列化之后的值127.0.0.1:6379[1]> dump test
"\x00\x0bhello-world\b\x00\xad5\x9a\xf9\xb0\xf8\xf2?" -
exists key
键是否存在127.0.0.1:6379[1]> exists test
(integer) 1
127.0.0.1:6379[1]> exists test1
(integer) 0 -
expire key seconds
设置过期时间127.0.0.1:6379[1]> expire test 20
(integer) 1
127.0.0.1:6379[1]> ttl test
(integer) 15
127.0.0.1:6379[1]> ttl test
(integer) 14
127.0.0.1:6379[1]> ttl test
(integer) 4 -
ttl key
获取过期时间127.0.0.1:6379[1]> ttl test
(integer) -2 127.0.0.1:6379[2]> ttl t1
(integer) -1键不存在则返回
-2
键永不过期返回-1
pttl key
使用毫秒返回剩余的过期时间pexpire key milliseconds
设置过期时间为毫秒expireat key timestamp
设置到指定时间戳过期pexpireat key milliseconds
-
move key db
移动 key 到某 db127.0.0.1:6379[1]> keys *
1) "t1"
127.0.0.1:6379[1]> move t1 2
(integer) 1
127.0.0.1:6379[1]> select 2
OK
127.0.0.1:6379[2]> keys *
1) "t1"
127.0.0.1:6379[2]> select 1
OK
127.0.0.1:6379[1]> keys *
(empty list or set) -
randomkey
返回随机的一个键127.0.0.1:6379[2]> randomkey
"t1"
127.0.0.1:6379[2]> randomkey
"t2" -
rename key newkey
重命名127.0.0.1:6379[2]> rename t1 ttttt
OK
127.0.0.1:6379[2]> keys *
1) "ttttt"
2) "t2" -
renamenx key newkey
当 newkey 不存在的时候重命名127.0.0.1:6379[2]> keys *
1) "tttt"
2) "ttttt"
127.0.0.1:6379[2]> set t1 3
OK
127.0.0.1:6379[2]> renamenx t1 tttt
(integer) 0
127.0.0.1:6379[2]> keys *
1) "t1"
2) "tttt"
3) "ttttt"
127.0.0.1:6379[2]> -
type key
获取 key 的类型127.0.0.1:6379[2]> type tttt
string
字符串
set key value
get key value
getrange key start end
-
getset key value
设置成新的 value 并返回原 value127.0.0.1:6379[2]> set t1 test
OK
127.0.0.1:6379[2]> get t1
"test"
127.0.0.1:6379[2]> getset t1 fff
"test"
127.0.0.1:6379[2]> get t1
"fff"
127.0.0.1:6379[2]> getrange t1 0 1
"ff"
注意: redis 的 range 是两边都是闭区间,并作为一个 feature
ps. 个人用起来觉得比较别扭
-
setex key seconds value
设置 value 并且设置过期时间127.0.0.1:6379[2]> setex t2 10 f
OK
127.0.0.1:6379[2]> ttl t2
(integer) 4 psetex key milliseconds value
-
setnx key value
不存在 key 的时候设置127.0.0.1:6379[2]> setex t2 10 f
OK
127.0.0.1:6379[2]> ttl t2
(integer) 4 -
mset k1 v1 k2 v2
设置多个值127.0.0.1:6379[2]> mset k1 v1 k2 v2
OK
127.0.0.1:6379[2]> keys *
1) "k1"
2) "ttttt"
3) "t1"
4) "k2"
5) "tttt"
6) "t2"
127.0.0.1:6379[2]> get k1
"v1"
127.0.0.1:6379[2]> get k2
"v2"
127.0.0.1:6379[2]> msetnx k1 v1 k2 v2
-
incr
值自增1127.0.0.1:6379[2]> set k1 1
OK
127.0.0.1:6379[2]> incr k1
(integer) 2
127.0.0.1:6379[2]> get k1
"2" -
decr
值子减1127.0.0.1:6379[2]> decr k1
(integer) 1 127.0.0.1:6379[2]> get k1
"1" -
append key value
追加 value 到原来的值127.0.0.1:6379[2]> append k1 test
(integer) 5
127.0.0.1:6379[2]> get k1
"1test"