Redis所有的数据结构都是以 唯一的key字符串 作为名称,然后通过该唯一key值来获取相应的value值
> set name zhongmingmao OK > get name "zhongmingmao" > exists name (integer) 1 > del name (integer) 1 > get name (nil)
批量对多个字符串进行读写, 节省网络开销
> set name1 zhongmingmao OK > set name2 zhongmingwu OK > mget name1 name2 name3 1) "zhongmingmao" 2) "zhongmingwu" 3) (nil) > mset name1 a name2 b name3 unknown OK > mget name1 name2 name3 1) "a" 2) "b" 3) "unknown"
> set name zhongmingmao OK > get name "zhongmingmao" > expire name 5 (integer) 1 > get name # 等5S后 (nil) > setex name 5 zhongmingmao OK > get name "zhongmingmao" > get name # 等5S后 (nil) > setnx name zhongmingmao (integer) 1 > get name "zhongmingmao" > setnx name zhongmingwu (integer) 0 # name已存在,set失败 > get name "zhongmingmao" # 结果不变
如果value是一个 整数 ,可以对其进行 自增 操作,自增是有范围的,即 signed long 的最大值和最小值
> set age 30 OK > incr age (integer) 31 > incrby age 5 (integer) 36 > incrby age -5 (integer) 31 > set age 9223372036854775807 # Long.Max OK > incr age (error) ERR increment or decrement would overflow
O(1)
,但 索引定位 的时间复杂度为 O(n)
> rpush books python java golang (integer) 3 > llen books (integer) 3 > lpop books "python" > lpop books "java" > lpop books "golang" > lpop books (nil)
> rpush books python java golang (integer) 3 > rpop books "golang" > rpop books "java" > rpop books "python" > rpop books (nil)
get(int index)
方法,需要对链表进行 遍历 ,性能随着参数index增大而变差
index=-1
表示 倒数第一 个元素 > rpush books python java golang (integer) 3 > lindex books 1 # O(n) "java" > lrange books 0 -1 # O(n) 1) "python" 2) "java" 3) "golang" > ltrim books 1 -1 # O(n) OK > lrange books 0 -1 1) "java" 2) "golang" > ltrim books 1 0 # 清空整个列表,因为区间范围长度为负 OK > llen books (integer) 0
ziplist
,即 压缩列表 quicklist
ziplist
结合起来组成 quicklist
ziplist
使用 双向指针 串联,这样即能满足 快速的插入删除性能 ,又 不会出现太大的空间冗余 > hset books java 'think in java' (integer) 1 > hset books golang 'concurrency in go' (integer) 1 > hset books python 'python cookbook' (integer) 1 > hgetall books 1) "java" 2) "think in java" 3) "golang" 4) "concurrency in go" 5) "python" 6) "python cookbook" > hlen books (integer) 3 > hget books java "think in java" > hset books golang 'learning go programming' # 因为是更新操作,所以返回0 (integer) 0 > hget books golang "learning go programming" > hmset books java 'effective java' python 'learning python' golang 'modern golang programming' # 批量set OK > hincrby zhongmingmao age 18 (integer) 18 > hget zhongmingmao age "18"
NULL
> sadd books python (integer) 1 > sadd books python (integer) 0 # 重复 > sadd books java golang (integer) 2 > smembers books # 无序 1) "golang" 2) "java" 3) "python" > sismember books java # contains (integer) 1 > sismember books rust (integer) 0 > scard books # count (integer) 3 > spop books "golang"
> zadd books 9.0 'think in java' (integer) 1 > zadd books 8.9 'java concurrency' (integer) 1 > zadd books 8.6 'java cookbook' (integer) 1 > zrange books 0 -1 1) "java cookbook" 2) "java concurrency" 3) "think in java" > zrevrange books 0 -1 1) "think in java" 2) "java concurrency" 3) "java cookbook" > zcard books # count (integer) 3 > zscore books 'java concurrency' "8.9000000000000004" # double存储,存在精度问题 > zrank books 'java concurrency' (integer) 1 > zrangebyscore books 0 8.91 1) "java cookbook" 2) "java concurrency" > zrangebyscore books -inf 8.91 withscores 1) "java cookbook" 2) "8.5999999999999996" 3) "java concurrency" 4) "8.9000000000000004" > zrem books 'java concurrency' (integer) 1 > zrange books 0 -1 1) "java cookbook" 2) "think in java"
> set name zhongmingmao OK > expire name 600 (integer) 1 > ttl name (integer) 597 > set name zhongmingwu OK > ttl name (integer) -1