1.String type
command |
Explanation |
Case |
set |
Add key-value |
set username admin |
get |
According to acquire key data |
get username |
strlen |
Get the length of the key |
strlen key |
exists |
Determine whether there is key |
exists name Return 0 absent 1 present |
of the |
Delete the key redis |
del key |
Keys |
For queries that meet the conditions of the key |
keys * Query redis in all of the key keys n? me get the data using placeholders keys nam * get data beginning with nam |
mset |
Assignment multiple key-value |
mset key1 value1 key2 value2 key3 value3 |
mget |
Gets the value of multiple key |
mget key1 key2 |
append |
To be added to the value of a key |
append key value |
type |
Examination of a key type |
type key |
select |
Switching redis database |
select 0-15 redis total of 16 database |
flushdb |
Empty single database |
flushdb |
flushall |
Clear All Database |
flushall |
incr |
Automatic 1 |
incr key |
decr |
Automatic Save 1 |
decr key |
incrby |
Specify the value added |
incrby 10 |
decrby |
Save a specified value |
decrby 10 |
expire |
Effective time unit of seconds specified key |
expire key 20 After the second failure key20 |
pexpire |
Key expiration time specified in units of milliseconds |
pexpire key 2000 key 2000 milliseconds after failure |
ttl |
Check the key remaining survival time |
ttl key |
persist |
Undo key expiration time |
persist key |
2.Hash type
Note: You can save the object type attribute value using a hash
Examples: User Object {id: 2, name: Xiaoming, age: 19}
command |
Explanation |
Case |
hset |
Adding data objects |
hset key field value |
hget |
Gets the object's property values |
hget key field |
hexists |
Analyzing the object property exists |
HEXISTS key field 1 indicates the presence of 0 indicates absence of |
hdel |
Delete hash of property |
hdel user field [field ...] |
hgetall |
获取hash全部元素和值 |
HGETALL key |
hkyes |
获取hash中的所有字段 |
HKEYS key |
hlen |
获取hash中所有属性的数量 |
hlen key |
hmget |
获取hash里面指定字段的值 |
hmget key field [field ...] |
hmset |
为hash的多个字段设定值 |
hmset key field value [field value ...] |
hsetnx |
设置hash的一个字段,只有当这个字段不存在时有效 |
HSETNX key field value |
hstrlen |
获取hash中指定key的长度 |
HSTRLEN key field |
hvals |
获取hash的所有值 |
HVALS user |
3.List类型
说明:Redis中的List集合是双端循环列表,分别可以从左右两个方向插入数据.
List集合可以当做队列使用,也可以当做栈使用
队列:存入数据的方向和获取数据的方向相反
栈:存入数据的方向和获取数据的方向相同
命令 |
说明 |
案例 |
lpush |
从队列的左边入队一个或多个元素 |
LPUSH key value [value ...] |
rpush |
从队列的右边入队一个或多个元素 |
RPUSH key value [value ...] |
lpop |
从队列的左端出队一个元素 |
LPOP key |
rpop |
从队列的右端出队一个元素 |
RPOP key |
lpushx |
当队列存在时从队列的左侧入队一个元素 |
LPUSHX key value |
rpushx |
当队列存在时从队列的右侧入队一个元素 |
RPUSHx key value |
lrange |
从列表中获取指定返回的元素 |
LRANGE key start stop Lrange key 0 -1 获取全部队列的数据 |
lrem |
从存于 key 的列表里移除前 count 次出现的值为 value 的元素。 这个 count 参数通过下面几种方式影响这个操作:
|
LREM list -2 “hello” 会从存于 list 的列表里移除最后两个出现的 “hello”。 需要注意的是,如果list里没有存在key就会被当作空list处理,所以当 key 不存在的时候,这个命令会返回 0。 |
Lset |
设置 index 位置的list元素的值为 value |
LSET key index value |
4.redis事务命令
说明:redis中操作可以添加事务的支持.一项任务可以由多个redis命令完成,如果有一个命令失败导致入库失败时.需要实现事务回滚.
命令 |
说明 |
案例 |
multi |
标记一个事务开始 |
127.0.0.1:6379> MULTI OK |
exec |
执行所有multi之后发的命令 |
127.0.0.1:6379> EXEC OK |
discard |
丢弃所有multi之后发的命令 |
|