2-2通用命令
1 2 3 4 5 6 7 8
| keys * #遍历所有key del key [key…] #删除指定的key-value dbsizee 算出key的总数 expire key seconds #key在sc秒后过期 ttl key #查看key剩余的过期时间(-1 代表没有过期时间 -2代表已过期) persist key #去掉key的过期时间 exists key #检查一个key是否存在 type key #返回key的类型,结果为string hash list set zset none(key不存在)
|

2-3 数据结构和内部编码


2-4 单线程


单线程为什么还这么快?
其它注意点
1
| keys,flushall,flushdb,slow lua script,mutil/exec,operate big value(collection)
|
- 其实不是全是单线程,如下面的命令
fysnc file descriptor
, close file descriptor
2-5 字符串
场景: 缓存, 计数器,分布式锁
特点
key都为字符串 value可为 字符串(json) 整数 二进制
API
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| 注:以下命令 时间复杂度均为 o(1)
get key
set key value
del key
incr key
decr key o(1)
incrby key k
decr key k
set key value
setnx key value
set key value xx
|
1 2 3 4 5 6 7
| 注:以下命令 时间复杂度均为 o(n)
mget keyl key2 key3...
mset key1 valuel key2 value2 key3 value3
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| 注:以下命令 时间复杂度均为 o(1)
getset key newvalue
append key value
strlen key
incrbyfloat key 3.5
getrange key start end
setrange key index value
|
2.5 hash
特点
key还是String类型 ,值则分成 field和value,可以添加field来对属性进行扩充
redis本身就是一个map结构的,所以可以把这种数据结构理解成 “mapmap”,其中 field就像key一样不能相同 。

API所有命令都是以h开头
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| 命令 时间复杂度均为 o(1)
hget key field #获取hash key对应的field的value
hset key field value #设置hash key对应field的value
hdel key field #删除hash key对应field的value
hexists key field #判断hash key是否有field
hlen key #获取hash key field的数量
hsetnx key field value #设置hash key对应field的value(如field已经存在,则失败)
hincrby key field intCounter #hash key对应的field的value自增intCounter
hincrbyfloat key field floatCounter #hincrby浮点数版
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| 命令 时间复杂度均为 o(n)
hmget key field1 field2....fieldN #批量获取hash key的一批field对应的值
hmset key fieldI valuel field2 value2..fieldN valueN #批量设置hash key的一批field value
hgetall key #返回hash key对应所有的field和value
hvals key #返回hash key对应所有field的value
hkeys key #返回hash key对应所有field
|

