Redis的常用命令——string

string的一些常用命令


对string的操作可以分为3个种,可以作为字符串、数值或者位图操作。
下面是一些常用命令,有help帮助文档的英文介绍,并写上自己的理解。

面向字符串的操作

 > help set
 SET key value [expiration EX seconds|PX milliseconds] [NX|XX]

set除了可以设置k,v。还可以在后面跟上过期时间, 权限(nx代表只有当key不存在时才能设置value。只能添加。xx代表只有当key存在时才能设置value,只能更新。)

> help get
  GET key
  summary: Get the value of a key

获取key对应的value

> help mset
  MSET key value [key value ...]
  summary: Set multiple keys to multiple values

mset可以给多个键设置设置多个值,也就是同时给多个键赋值。

> help MSETNX 
  MSETNX key value [key value ...]
  summary: Set multiple keys to multiple values, only if none of the keys exist

给多个key设置value,只有当所有key都不存在时才能成功(类似事务,原子性的)。

> help append
  APPEND key value
  summary: Append a value to a key

可以给value追加值,末尾添加字符

> help getrange
  GETRANGE key start end
  summary: Get a substring of the string stored at a key

获取对应位置的子字符串。redis有一个非常很智能的点,不光可以正向索引还可以反向索引,如0代表第一位,而 -1代表着最后一位。

> help setrange
  SETRANGE key offset value
  summary: Overwrite part of a string at key starting at the specified offset

从偏移量的位置开始覆盖一段字符串

> help strlen
  STRLEN key
  summary: Get the length of the value stored in a key

获取字符串长度

> help getset
  GETSET key value
  summary: Set the string value of a key and return its old value

更新新值,返回旧值。这样一个命令包含了两个操作,减少一次io。

面向数值的操作

> help incr
  INCR key
  summary: Increment the integer value of a key by one

给key对应的整数value加一,如果value不是整数或者超出范围会报错(对应java的long类型的最大值2的63方-1)

> help DECR
  DECR key

value减一,最小值对应java的long类型最小值

> help DECRBY
  DECRBY key decrement
  summary: Decrement the integer value of a key by the given number

将key对应的value减去给定数字

> help INCRBY
  INCRBY key increment

对应的value加上给定数字

> help INCRBYFLOAT
  INCRBYFLOAT key increment
  summary: Increment the float value of a key by the given amount

加上一个浮点值,结果也会变成浮点值。 最多保留小数点后的有效数位是18位,小数点前最多19位。浮点型是以字符串保存的。

面向bitmap的操作

> help setbit
  SETBIT key offset value
  summary: Sets or clears the bit at offset in the string value stored at key

设置vaule某一位(从左到右,从0开始)的值,就是给二进制的某一位设置0或者1。

> help BITCOUNT
  BITCOUNT key [start end]
  summary: Count set bits in a string

统计二进制码中1的个数,指定第[start]个字节到第[end]个字节范围内。

> help bitpos
  BITPOS key bit [start] [end]
  summary: Find first bit set or clear in a string

查找一个二进制位(0或1)第一次出现在整个sting中的第几位。[start]与[end]指定的是第几个字节。

> help bitop
  BITOP operation destkey key [key ...]
  summary: Perform bitwise operations between strings

对一个或多个key对应的value进行位运算(and、or、xor、not),并将结果保存到 destkey 上。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值