Redis(三):基础篇 - 字符串类型

热身

  先来几个比较基础的命令热身一下,打开redis-cli后,开始体验

1、获得符合规则的键名列表

KEYS pattern

pattern支持glob风格通配符格式,如下表:

符号含义
?匹配一个字符
*匹配任意个(包括0个)字符
[]匹配括号间的任一字符,可以使用“-”符号表示一个范围,如a[b-d]可以匹配“ab”、“ac”和“ad”
\x匹配字符x,用于转移符号。如要匹配“?”,就需要使用?
127.0.0.1:6379> keys *
1) "num"
2) "mylist"
3) "su"

注意 KEYS命令需要遍历Redis中的所有键,当键的数量较多时,会影响性能,不建议在生产环境中使用。

提示 Redis不区分命令大小写

2、判断一个键是否存在

EXISTS key

如果键存在则返回证书类型1,否则返回0,如:

127.0.0.1:6379> EXISTS su
(integer) 1
127.0.0.1:6379> EXISTS peng
(integer) 0

3、删除键

DEL key [key...]

可以删除一个或多个键,返回值是删除的键的个数,如:

127.0.0.1:6379> del su
(integer) 1
127.0.0.1:6379> del su
(integer) 0
127.0.0.1:6379> del num mylist
(integer) 2
127.0.0.1:6379> 

第二次执行DEL命令时因为su键已经被删除了,实际上并没有删除任何键,所以返回0.

技巧 DEL命令的参数不支持通配符,但我们可以结合Linux的管道和xargs命令自己实现删除所有符合规则的键。比如要删除所有以"key"开头的键,就可以执行redis-cli KEYS "key*" | xargs redis-cli DEL。另外由于DEL命令支持多个键作为参数,所以还可以执行redis-cli DEL `redis-cli KEYS “key*”`来达到同样的效果,但是性能更好。

127.0.0.1:6379> mset key1 va key2 va key3 va
OK

另开一个终端

[root@VM-0-17-centos ~]# redis-cli DEL `redis-cli KEYS "key*"`
(integer) 3
[root@VM-0-17-centos ~]# 

再查看键,已经被删除完了

127.0.0.1:6379> keys *
(empty array)

4、获取键值的数据类型

TYPE key

TYPE命令用来获得键值的数据类型,返回值可能是string(字符串类型)、hash(散列类型)、list(列表类型)、set(集合类型)、zset(有序集合类型)。如:

127.0.0.1:6379> set su 1
OK
127.0.0.1:6379> type su
string
127.0.0.1:6379> lpush list 1
(integer) 1
127.0.0.1:6379> type list
list

字符串类型(String)

  字符串类型是Redis中最基本的数据类型,它能存储任何形式的字符串,包括二进制数据,也可以用其存储用户的邮箱、JSON化的对象,甚至是一张图片。一个字符串类型键允许存储的数据的最大容量是512MB。

命令

1、赋值与取值

SET key value
GET key

SET和GET是Redis中最简单的两个命令,GET key,当键不存在时,会返回空结果。

127.0.0.1:6379> set mon abc
OK
127.0.0.1:6379> get mon
"abc"
127.0.0.1:6379> get tun
(nil)

2、递增数字

INCR key

当存储的字符串是证书形式时,Redis提供了一个实用的命令INCR,起作用是让当前键值递增,并返回递增后的值,如:

127.0.0.1:6379> set num 1
OK
127.0.0.1:6379> incr num
(integer) 2
127.0.0.1:6379> incr num
(integer) 3

当要操作的键不存在时,会默认键值为0,所以第一次递增后的结果是1,当键值不是整数时,Redis会提示错误:

127.0.0.1:6379> incr sat
(integer) 1
127.0.0.1:6379> incr mon
(error) ERR value is not an integer or out of range

提示 Redis命令都是原子操作。就是说即使多个客户端对同一个key发出INCR命令,也决不会导致竞争的情况。例如如下情况永远不可能发生:『客户端1和客户端2同时读出“10”,他们俩都对其加到11,然后将新值设置为11』。最终的值一定是12,read-increment-set操作完成时,其他客户端不会在同一时间执行任何命令。

3、增加指定的整数

INCRBY key increment

INCRBY 命令与INCR命令一样,只不过前者可以通过increment参数指定一次增加的数值,如:

127.0.0.1:6379> incrby num 3
(integer) 6
127.0.0.1:6379> incrby num 3
(integer) 9

4、减少指定的整数

DECR key
DECRBY key increment

DECR 命令与INCR命令用法相同,只不过是让键值递减,如:

127.0.0.1:6379> decr num
(integer) 8
127.0.0.1:6379> decrby num 2
(integer) 6

5、增加指定浮点数

INCRBYFLOAT key increment

INCRBYFLOAT 命令类似INCRBY命令,差别是前者可以递增一个双精度浮点数,如:

127.0.0.1:6379> incrbyfloat num 2.7
"8.7"

6、向尾部追加值

APPEND key value

APPEND作用是向键值的末尾追加value。如果键不存在则将该键的值设置为value,即相当于SET key value。返回值是追加后字符串的总长度。如:

127.0.0.1:6379> set key hello
OK
127.0.0.1:6379> append key " world"
(integer) 11
127.0.0.1:6379> get key
"hello world"

APPEND命令的第二个参数加了双引号,原因是该参数包含空格。

7、获取字符串长度

STRLEN key

STRLEN 命令返回键值的长度,如果键不存在则返回0.如:

127.0.0.1:6379> strlen key
(integer) 11
127.0.0.1:6379> strlen ssss
(integer) 0

8、同时获得/设置多个键值

MGET key [key...]
MSET key value [key value ...]
127.0.0.1:6379> mset key1 v1 key2 v2 key3 v3
OK
127.0.0.1:6379> mget key1 key3
1) "v1"
2) "v3"

9、位操作

GETBIT key offset
SETBIT key offset value
BITCOUNT key [start] [end]
BITOP operation destkey key [key...]

一个字节由8个二进制位组成,Redis提供了4个命令可以直接对二进制位进行操作。先设置foo键的值为bar:

127.0.0.1:6379> set foo bar
OK

bar的3个字母“b”、“a”和“r”对应的ASCII码分别为98、97和114,转换成二进制后分别为1100010、1100001和1110010

bar
011000100110000101110010

GETBIT命令可以获得一个字符串类型键指定位置的二进制位的值(0或1),索引从0开始:

127.0.0.1:6379> getbit foo 0
(integer) 0
127.0.0.1:6379> getbit foo 6
(integer) 1

如果需要获取的二进制位的索引超出了键值的二进制位的实际长度,则默认位值是0:

127.0.0.1:6379> getbit foo 10000
(integer) 0

SETBIT命令可以设置媳妇穿类型键指定位置的二进制位的值,返回值是该位置的旧值。如要将foo键值设置为aar,可以通过位操作将foo键的二进制位的索引第6位设为0,第7位设为1:

127.0.0.1:6379> setbit foo 6 0
(integer) 1
127.0.0.1:6379> setbit foo 7 1
(integer) 0
127.0.0.1:6379> get foo
"aar"

如果要设置的位置超过了键值的二进制位的长度,SETBIT命令会自动将中间的二进制位设置为0,同理设置一个不存在的键的指定二进制位的值会自动将其前面的位赋值为0.

BITCOUNT命令可以获得字符串类型键中值是1的二进制位个数,如:

127.0.0.1:6379> bitcount foo
(integer) 10

可以通过参数来限制统计的字节范围,如统计前两个字节(即“aa”):

127.0.0.1:6379> bitcount foo 0 1
(integer) 6

BITOP命令可以对多个字符串类型键进行位运算,并将结果存储在destkey参数指定的键中。BITOP命令支持的运算操作有AND、OR、XOR和NOT。如对bar和aar进行OR运算:

127.0.0.1:6379> set foo1 bar
OK
127.0.0.1:6379> set foo3 aar
OK
127.0.0.1:6379> bitop or res foo1 foo3
(integer) 3
127.0.0.1:6379> get res
"car"

10、help

127.0.0.1:6379> help @string

  APPEND key value
  summary: Append a value to a key
  since: 2.0.0

  BITCOUNT key [start end]
  summary: Count set bits in a string
  since: 2.6.0

  BITFIELD key [GET type offset] [SET type offset value] [INCRBY type offset increment] [OVERFLOW WRAP|SAT|FAIL]
  summary: Perform arbitrary bitfield integer operations on strings
  since: 3.2.0

  BITOP operation destkey key [key ...]
  summary: Perform bitwise operations between strings
  since: 2.6.0

  BITPOS key bit [start] [end]
  summary: Find first bit set or clear in a string
  since: 2.8.7

  DECR key
  summary: Decrement the integer value of a key by one
  since: 1.0.0

  DECRBY key decrement
  summary: Decrement the integer value of a key by the given number
  since: 1.0.0

  GET key
  summary: Get the value of a key
  since: 1.0.0

  GETBIT key offset
  summary: Returns the bit value at offset in the string value stored at key
  since: 2.2.0

  GETDEL key
  summary: Get the value of a key and delete the key
  since: 6.2.0

  GETEX key [EX seconds|PX milliseconds|EXAT timestamp|PXAT milliseconds-timestamp|PERSIST]
  summary: Get the value of a key and optionally set its expiration
  since: 6.2.0

  GETRANGE key start end
  summary: Get a substring of the string stored at a key
  since: 2.4.0

  GETSET key value
  summary: Set the string value of a key and return its old value
  since: 1.0.0

  INCR key
  summary: Increment the integer value of a key by one
  since: 1.0.0

  INCRBY key increment
  summary: Increment the integer value of a key by the given amount
  since: 1.0.0

  INCRBYFLOAT key increment
  summary: Increment the float value of a key by the given amount
  since: 2.6.0

  MGET key [key ...]
  summary: Get the values of all the given keys
  since: 1.0.0

  MSET key value [key value ...]
  summary: Set multiple keys to multiple values
  since: 1.0.1

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

  PSETEX key milliseconds value
  summary: Set the value and expiration in milliseconds of a key
  since: 2.6.0

  SET key value [EX seconds|PX milliseconds|EXAT timestamp|PXAT milliseconds-timestamp|KEEPTTL] [NX|XX] [GET]
  summary: Set the string value of a key
  since: 1.0.0

  SETBIT key offset value
  summary: Sets or clears the bit at offset in the string value stored at key
  since: 2.2.0

  SETEX key seconds value
  summary: Set the value and expiration of a key
  since: 2.0.0

  SETNX key value
  summary: Set the value of a key, only if the key does not exist
  since: 1.0.0

  SETRANGE key offset value
  summary: Overwrite part of a string at key starting at the specified offset
  since: 2.2.0

  STRALGO LCS algo-specific-argument [algo-specific-argument ...]
  summary: Run algorithms (currently LCS) against strings
  since: 6.0.0

  STRLEN key
  summary: Get the length of the value stored in a key
  since: 2.2.0


author:su1573
鄙人记录生活点滴,学习并分享,请多指教!!!
如需交流,请联系 sph1573@163.com,鄙人看到会及时回复

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ssy03092919

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值