Adventure Redis depth (7) - common operations and basic data structure

A basic command

0.-class instruction

// 登陆redis客户端
redis-cli -h ipAddress -p port -a password
// 查看redis信息
info <memory>
// 尝试连接
ping
// 查看key是否存在
exists key 
// 设置key的过期时间(秒)
expire key seconds
// 设置key的过期时间(毫秒) 
pexpire key millseconds
// 通过正则表达式查询key
keys pattern
// 将key移到其他db
move key dbName
// 取消过期时间
persist key
// 查看key的过期时间(秒)
ttl key
// 查看key的过期时间(毫秒)
pttl key
// 随机返回一个key
randomkey
// 重命名key
rename oldKeyName newKeyName
// 查看key的类型
type key
// 删除key
del key

The basic commands 1.string

Buffer and a simple logic data string used to achieve hot domain, and the realization of distributed lock.

// 添加key和value
set key value
// 获取key的value
get key
// 如果key不存在则添加
setnx key value 
// 添加key并设置过期时间
setex key value
// 在key不存在时添加key并设置过期时间
set key value ex expireTime nx
// 批量添加key-value
mset key1 value1 key2 value2 ...
// 获取key的value并设置新的value
getset key newValue
// 批量获取key
mget key1 key2 ...
// 查看key的value长度
strlen key
// 自增key的value
incr key
// 增量递增key的value
incrby key increment
// 自减key
decr key 
// 增量递减key的value
decrby key decrement
// 在key的value之后追加
append key value

The basic commands 2.list

To limit the list of common stock as well as stack and queue, use blpop brpop or more can achieve a simple blocking queue

// 右push
rpush listName v1 v2 v3
// 左push
lpush listName v1 v2 v3
// 右pop
rpop listName
// 左pop
lpop listName
// 在等待时间内阻塞左读取
blpop listName timeOut
// 在等待时间内阻塞右读取
brpop listName timeOut
// 查看长度
llen listName
// 遍历
lrange key startIndex endIndex
// 修改指定位置的元素
lset listName index value
// 删除指定位置的元素
lrem ket count value
// 将制定位置的链表截取出来替代原链表
ltrim key startIndex endIndex

The basic commands 3.hash

Common hash and store relational entity, such as the use of key storage student id, and to store the name and age as the field name and age. In the same field, it is recommended to replace the string hash data store

// 添加key-filed-value对
hset key field value
// 批量添加
mhset key filed1 value1 field2 value2 ...
// 获取key-field的值
hget key field
// 批量获取key-field的值
hmget key field1 field2...
// 查看key的field数
hlen key
// 获取到key的所有field和值
hkeys key
// 获取到key的所有field和值
hgetall key
// 批量获取key的field
hmget key field1 field2 ...

The basic commands 4.set

A collection of popular buddy relationship to achieve mutual friend and thumbs up to the number of weight and other (due to the set itself based on hash achieved with de-emphasis function)

// 在集合中添加元素
sadd key value1 value2 ...
// 获取集合中元素的个数
scard key
// 查看集合1中相对于集合1的差集
sdiff key1 key2
// 查看集合1与2的交集
sinter key1 key2
// 查看集合1与2的并集
sunion key1 key2
// 查看member是否为key的元素
sismember key member
// 获取key的所有元素
smembers key
// smove key1 key2 member
将key1中的member移动到key2中
// spop key
移除并获取key的随机元素
// srem key member1 member2 ...

5.zset basic commands

Ordered set is often used to achieve real-time rankings, standings and so needs a strong demand for real-time ordering

// 在有序集合中添加多个带权重的元素(或修改)
zadd key score1 value1 score2 value2 ...
// 获取集合中的元素个数
zcard key
// 获取集合中制定权重范围的元素个数
zcount key score1 score2
// 增量递增集合中某个元素的权重
zincrby key increment value
// 以权重为排序返回特定权重区间的值和对应的权重
zrangebyscore key score0 score1 withscores
// 查询特定元素的排名(通过权重排序)
zrank key value
// 删除集合中指定元素
zrem key value0 value1 ...
// zrembyscore key score0 score1
删除指定权重区间的元素
// 查询指定权重区间的元素和权重「升序」 
zrevrange key score0 score1 withscores
// 查看指定元素的排名「升序」
zrevrank key value
// 查询指定成员的权重
zscore key value
// 区两个集合的并集并且存放在第三个集合中
zunionstore key3 2 key1 key2

II. Overview of data types to achieve

1.string, list, hash, set, zset underlying data structure

  1. Based on the underlying string byte array (length T paradigm modifications may be initialized to a byte or short, to save space), the size of which is variable string, similar to the Java ArrayList
  2. The bottom list is a doubly linked list, similar in Java LinkedList
  3. + Underlying hash linked list is an array, using the method to solve the conflict fastener, similar to the Java HashMap
  4. Underlying hash is set, the set value is the hash of the key, so it can not be repeated
  5. Zset bottom jump table that, when the data is less than 128, the list is stored by compressing

2.string expansion of operations

string is assigned a maximum capacity when creating capacity, when the current capacity greater than the number len, will operate for expansion:

  1. When the size of the string <1MB, eachMultiplied expansion
  2. When the size of the string> = 1MB, eachWith 1MB increments expansion, the maximum can not exceed 512M
    (To save space, the size of the string will be used are not simultaneously stored in different formats)

3.hash fastener method, the rehash progressive, bis hashtable

hash of the design is very clever, it is in the expansion, unlike HashMap as the total amount of rehash, all of the index and re-compute the hash value of the new index values ​​into the new array, butObjective progressive rehash to achieve performance optimization: the presence of two hash structure haashtable, one old and one new. When the expansion occurs, the first part of the new rehash of the hashtable, while the rest remain in the old hashtable, the remaining data will be transferred in the operation after the timer or re gradually transferred to the new hash table

4.zset Why a jump table as the underlying implementation

Because zset must ensure orderly, then there are two options, the first one is an orderly array, this scheme can do an orderly inserted dichotomy, but considering the size of the required dynamic expansion, the array can not be used; the second species is the use of a sorted linked list, dynamic expansion of the size, but can not quickly find the insertion point; third jump table is employed,It is an ordered array of binding to quickly find and list advantages as the insertion point of the dynamic expansion, to achieve O (logn) time complexity order to insert the data and allowed

TIPS: the number of elements is less than 128 zset when compression is used as the underlying data structure list.

Published 309 original articles · won praise 205 · Views 300,000 +

Guess you like

Origin blog.csdn.net/pbrlovejava/article/details/103851572