table of Contents
Two, key commonly used commands
Fourth, the list of strings (lists)
Five, string collection (sets)
Six, ordered string collection (sorted sets)
1. Brief introduction
Learning reference
https://blog.csdn.net/liqingtx/article/details/60330555
https://www.runoob.com/redis/redis-keys.html
Redis is a key:value storage system written in C language, supporting network interaction, memory-based or persistent, and value supports five data types
Strings, lists of strings, sets, sorted sets, hashes
Two, key commonly used commands
command | description | usage | success | failure | Remarks | |
set | Create key | set key1 123 | OK | Create an existing key, it will overwrite the former | ||
setnx | When the key is created and the current keyname does not exist | setnx key1 123 | 1 | 0 | ||
setex | Create a key and set the expiration time (seconds) | setex key1 10 123 | OK | Create an existing key, it will overwrite the former | ||
psetex | Create a key and set the expiration time (milliseconds) | psetex key1 1000 123 | OK | |||
mset | Create multiple keys | mset key1 123 key2 456 | OK | |||
msetnx | When multiple keys are created and the current key does not exist | msetnx key1 123 key2 456 | 1 | 0 | ||
get | Return the value of the key | get key1 | value | nil | ||
mget | Return the value of multiple keys | mget key1 key2 | Multiple value | nil | ||
of | Delete key | of key1 | 1 | 0 | ||
exists | Check if the key exists | exists key1 | 1 | 0 | ||
keys | Find eligible keys | keys key1/keys key*/keys * | key name | Add * after the name, all names before * are searched | ||
rename | Modify the key name | rename key1 key2 | OK | no such key | Modifying an existing key will overwrite the existing key | |
renamenx | Modify the key name, only when the new key name does not exist | renamenx key1 key2 |
1 | 0 | ||
expire | Set key expiration time (seconds) | expire key1 10 | 1 | 0 | ||
expire | Set key expiration time (milliseconds) | perpire key1 1000 |
1 | 0 | ||
expireat | Set key expiration time (time stamp) | expireat key1 1293810000 |
1 | 0 | ||
persist | Remove key expiration time | persist key1 | 1 | 0 | ||
TTL | Return the remaining time of key expiration (seconds) | ttl key1 | Seconds remaining | -1 | ||
PTTL | Return the remaining time of key expiration (milliseconds) | pttl key1 | Ms remaining | -1 | ||
dump | Serialization key | dump key1 | Serialized value | nil | ||
scan | Iteration key | scan 0 | Output keyname | |||
move | Move the key to another database | move key1 1 | 1 | 0 | Another inventory with the same key will fail, select 1, move to the corresponding library, 0 represents the original one |
Three, strings (strings)
- Maximum data storage
- 512MB
- Numerical calculation maximum range (the maximum value of long in java)
- 9223372036854775807
command | description | example | success | failure | Remarks |
getrange | String interception, starting with subscript 0 | getrange key1 2 3 | Returns the content of subscripts 2 to 3 | Return empty string | |
setrange | Replace to the string, starting from a certain subscript | setrange key1 2 "xxx" | Returns the length of the string after replacement | Auto-fill spaces that exceed the length | |
append | Concatenate content after the original string | append key1 "xxx" | Returns the length of the splicing | If the key does not exist, the key-value will be created and set | |
strlen | Returns the length of the string | strlen key1 | Return length | 0 | |
incr | +1 the stored number | incr key1 | Return the added number | Non-number error, error with decimal point | |
incrby | Add the stored number according to the specified number | incrby key1 2 | Return the added number | Non-number error, error with decimal point | |
incrbyfloat | Add the stored number according to the specified floating-point number | incrbyfloat key1 0.5 | Return the added number | Integer error | |
decr | Number to be stored -1 | decr key1 | Return the subtracted number | ||
decrby | Subtract the stored number according to the specified | decrby key1 2 | Return the subtracted number |
四、字符串列表(lists)
底层数据结构为双向链表,插入快,查询快,操作类似于栈
最大存储232个vlaue
命令 | 描述 | 例子 | 成功 | 失败 | 备注 |
Lpush | 向左边头部添加一个或多个值 | lpush ll 123 456 | 返回长度 | ||
Rpushx | 向已存在的Key,左边头部添加一个或多个值 | lpushx ll 123 456 | 返回长度 | 0 | |
Rpush | 向右边尾部添加一个或多个值 | rpush ll 123 456 | 返回长度 | ||
Rpushx | 向已存在的Key,右边尾部添加一个或多个值 | rpushx ll 123 456 | 返回长度 | 0 | |
lset | 指定list索引修改 | lset ll 0 123 | OK | ||
Lpop | 移除掉左边第一个元素,并且返回 | lpop ll | 原list的第一个元素 | nil | |
Rpop | 移除掉右边尾部第一个元素,并且返回 | rpop ll | 原list的最后一个元素 | nil | |
Blpop | 设置时间(秒),移除掉左边第一个元素,并且返回key和该元素 | blpop ll 5 | key和元素 | 设置5秒内没有结果,则返回 nil和时间 | |
Lindex | 通过索引查询元素 | lindex ll 0 | 元素 | ||
llen | 返回list长度 | llen ll | 返回长度 | ||
ltrim | 指定区间保留元素,其它的移除 | ltrim ll 0 1 | OK |
五、字符串集合(sets)
底层是一个无序的集合
命令 | 描述 | 例子 | 成功 | 失败 | 备注 |
sadd | 添加一个或多个元素 | sadd set 11 22 | 返回长度 | 相同的key会覆盖 | |
smembers | 返回全部的元素 | smembers set | 全部元素 | ||
sismember | 判断元素存在 | sismember set 11 | 1 | 0 | |
scard | 获取集合长度 | scard set | 返回长度 | ||
spop | 随机移除一个元素 | spop set | 返回移除元素 | nil | |
srem | 移除一个或多个成员 | srem set 11 22 | 返回移除数量 | ||
sscan | 迭代元素 | sscan set | 返回全部元素 |
六、有序字符串集合(sorted sets)
有序集合,每个元素带有一个score进行定位
七、哈希(hashes)
一种键值对结构,相当于key-value 再套一层keyValue
hash 可以存储 232 - 1 个键值对
命令 | 描述 | 例子 | 成功 | 失败 | 备注 |
Hset | 设置hset | hset hkey1 cc 123 | 1 | 0 | 设置已经存在的field,会覆盖前者 |
Hsetnx | 设置hset,在当前fi字段不存在下 | hsetnx hkey1 cc 123 | 1 | 0 | |
Hmset | 一次设置多个字段 | Hmset hkey1 cc 123 bb 456 | OK | ||
Hget | 获取hkey的字段的值 | hget hkey1 cc | 输出单个值 | nil | |
Hgetall | 获取hkey的所有字段和值 | hgetall hkey1 | 输出全部字段和值 | ||
Hmget | 获取多个字段值 | hmget hkey1 cc bb | 输出多个值 | nil | |
Hkeys | 获取hkey的字段 | hkeys hkey1 | 输出全部的字段 | ||
Hvals | 获取hkey的所有值 | hvals hkey1 | 输出全部的值 | ||
Hscan | 迭代hkey所有字段和值 | hscan hkey1 0 | 输出全部字段和值 | ||
Hlen | 获取hash字段数量 | hlen hkey1 | 数量 | ||
Hincrby | 对字段的进行相加,按指定的整数 | hincrby hkey1 cc 2 | 返回相加后的 | ||
Hincrbyfloat | 对字段的进行相加,按指定的小数 | hincrbyfloat hkey1 cc 0.4 | 返回相加后的 |