Redis命令

一.String

1.Getrange 截取得到的子字符串

localhost:6379> set k1 "hello world"
OK
localhost:6379> GETRANGE k1 0 3
"hell"
2.Getbit     字符串值指定偏移量上的位(bit),当偏移量 OFFSET 比字符串值的长度大,或者 key 不存在时,返回 0 。

对不存在的 key 或者不存在的 offset 进行 GETBIT, 返回 0
localhost:6379> exists b1
(integer) 0
localhost:6379> getbit b1 10086
(integer) 0

对已存在的 offset 进行 GETBIT

localhost:6379> setbit b2 10086 1
(integer) 0
localhost:6379> getbit b2 10086
(integer) 1
3.Setbit  指定偏移量原来储存的位。

  
localhost:6379> setbit b3 10086 1
(integer) 0
localhost:6379> getbit b3 10086
(integer) 1
4.Decr 将 key 中储存的数字值减一,对不存在个key值操作会返回-1
(1).localhost:6379> set f1 10        
OK
localhost:6379> decr f1
(integer) 9

(2).localhost:6379> exists f2
(integer) 0
localhost:6379> decr f2
(integer) -1
 

(3).localhost:6379> set f4 abc
OK
localhost:6379> decr f4
(error) ERR value is not an integer or out of range
 

5.Decrby 将 key 所储存的值减去指定的减量值

(1).
localhost:6379> set c1 100
OK
localhost:6379> decrby c1 10
(integer) 90
(2).localhost:6379> exists c2
(integer) 0
localhost:6379> decrby c2 10
(integer) -10
6. Incrby  将 key 中储存的数字加上指定的增量值。

(1).

localhost:6379> incrby c1 10
(integer) 100

(2.)

localhost:6379> incrby c2 10
(integer) 0

(3.)localhost:6379> set c3 "hello world"
OK
localhost:6379> incrby c3
(error) ERR wrong number of arguments for 'incrby' command

7.Incrbyfloat 为 key 中所储存的值加上指定的浮点数增量值。

(1).

localhost:6379> set m1 10.1
OK
localhost:6379> incrbyfloat m1 0.5
"10.6"

(2).

localhost:6379> set m2 314e-2                  用 SET 设置的值可以是指数符号
OK
localhost:6379> incrbyfloat m2 1                但执行 INCRBYFLOAT 之后格式会被改成非指数符号
"4.14"

(3).

localhost:6379> set m4 5.000000              SET 设置的值小数部分可以是 0
OK
localhost:6379> incrbyfloat m4  1.5             INCRBYFLOAT 会将无用的 0 忽略掉
"6.5"

8.Setrange 用指定的字符串覆盖给定 key 所储存的字符串值,覆盖的位置从偏移量 offset 开始。
localhost:6379> set k4 "hello world"
OK
localhost:6379> setrange k4 6 "Resid"
(integer) 11
localhost:6379> get k4
"hello Resid"
9.Getset 用于设置指定 key 的值,并返回 key 旧的值。

ocalhost:6379> getset k5 "hello world"
(nil)
localhost:6379> getset k5 "你好世界"
"hello world"

二.Hash

 1.Hgetall 命令用于返回哈希表中,所有的字段和值。
localhost:6379> HSET m5 filed1 "a"
(integer) 1
localhost:6379> HSET m5 filed2 "b"
(integer) 1
localhost:6379> Hgetall m5
1) "filed1"
2) "a"
3) "filed2"
4) "b"
2.

2.Hincrby 用于为哈希表中的字段值加上指定增量值

localhost:6379> Hset m6 filed1 20
(integer) 1
localhost:6379> hincrby m6 filed1 1
(integer) 21
localhost:6379> hincrby m6 filed1 -1
(integer) 20

3.Hlen 用于获取哈希表中字段的数量

localhost:6379> hset mm1 filed1 "a"
(integer) 1
localhost:6379> hset mm1 filed2 "b"
(integer) 1
localhost:6379> hlen mm1
(integer) 2

4.Hdel 用于删除哈希表 key 中的一个或多个指定字段,不存在的字段将被忽略

localhost:6379> hdel mm1 filed1
(integer) 1
localhost:6379> hlen mm1
(integer) 1
5. Hvals 返回哈希表所有字段的值

localhost:6379> hset mm1 filed 1
(integer) 1
localhost:6379> hlen mm1
(integer) 2
localhost:6379> hvals mm1
1) "b"
2) "1"

6. Hincrbyfloat 用于为哈希表中的字段值加上指定浮点数增量值
localhost:6379> hset mm2 filed 6.6
(integer) 1
localhost:6379> hincrbyfloat mm2 filed 1.1
"7.7"

7. Hkeys 用于获取哈希表中的所有字段名

localhost:6379> hkeys mm1
1) "filed2"
2) "filed"

三.List

1.Blpop 移出并获取列表的第一个元素, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止

localhost:6379> blpop list1 100
(nil)
(100.09s)

如果列表为空,返回一个 nil 。 否则,返回一个含有两个元素的列表,第一个元素是被弹出元素所属的 key ,第二个元素是被弹出元素的值。

2. Rpush 用于将一个或多个值插入到列表的尾部(最右边)

localhost:6379> rpush list2 "abc"
(integer) 1
localhost:6379> rpush list2 "def"
(integer) 2
localhost:6379> lrange list2 0 -1
1) "abc"
2) "def"

3.Rpoplpush 用于移除列表的最后一个元素,并将该元素添加到另一个列表并返回
localhost:6379> rpush list2 "qwe"
(integer) 3
localhost:6379> lrange list2 0 -1
1) "abc"
2) "def"
3) "qwe"
localhost:6379> rpoplpush list2 myotherlist
"qwe"
localhost:6379> lrange list2 0 -1
1) "abc"
2) "def"

4.Lpush 将一个或多个值插入到列表头部

localhost:6379> lpush list2 "hello"
(integer) 3
localhost:6379> lrange list2 0 -1
1) "hello"
2) "abc"
3) "def"
5.Rpushx 用于将一个或多个值插入到已存在的列表尾部

localhost:6379> rpushx list2 "world"
(integer) 4
localhost:6379> lrange list2 0 -1
1) "hello"
2) "abc"
3) "def"
4) "world"

6.Rpop 用于移除并返回列表的最后一个元素

localhost:6379> rpop list2
"world"
localhost:6379> lrange list2 0 -1
1) "hello"
2) "abc"
3) "def"

7. Linsert 用于在列表的元素前或者后插入元素

localhost:6379> linsert list2 before "def" "world"
(integer) 4
localhost:6379> lrange list2 0 -1
1) "hello"
2) "abc"
3) "world"
4) "def"

8.Ltrim 对一个列表进行修剪(trim),就是说,让列表只保留指定区间内的元素,不在指定区间之内的元素都将被删除

localhost:6379> ltrim list2 1 -1
OK
localhost:6379> lrange list2 0 -1
1) "abc"
2) "world"
3) "def"
9.Llen 用于返回列表的长度。
localhost:6379> llen list2
(integer) 3





 



 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值