Common commands for Redis string types summary _redis

Source: Internet
Author: User
Tags delete key hash redis

Redis String Type

A string type is the most basic type of data storage in Redis, and it is binary safe in Redis, which means that the type can accept data in any format, such as JPEG image data or JSON object description information. The value of the string type in Redis can hold up to 512M of data length.

One, the simplest of orders

1, to obtain the key list of the rules

Keys *

The * number here is the list of all the keys, while the * can also be replaced with other support GLOB style wildcard format, the specific rules are as follows:

?: Match one character

*: Match any (including 0) characters

[]: Matches the number of characters between parentheses, you can use the "-" to represent a range, such as [A-z]

\x: Match character X, for escape, if need to match question mark, needs \? to match

Here, the Redis command is case-insensitive, and the keys command is to traverse all the keys in the Redis, when the key is many, very expensive performance. So in the production environment to be as far as possible without.

Cases:

Redis 127.0.0.1:6379> set name "Joan"
OK
redis 127.0.0.1:6379> get name
"Joan"
Redis 127.0.0.1:6379> Set AAA 1
OK
redis 127.0.0.1:6379> set BBB 2
OK
redis 127.0.0.1:6379> keys *
   1) "AAA"
2 "" Name "
3" "BBB"

2, determine whether the key exists

exists foo

Returns 0 if there is a return certificate type 1

Cases:

Redis 127.0.0.1:6379> exists name
(integer) 1
redis 127.0.0.1:6379> exists n
(integer) 0

3, delete key

 del foo

Return is the number of deleted keys, this note can also delete multiple keys at a time, just a space and then add a key to it. For example:

del Foo bar

If you delete a key, the key does not exist and will return 0.

Cases:

Redis 127.0.0.1:6379> Keys *
1) "AAA"
2) "name"
3) "BBB"
Redis 127.0.0.1:6379> del Triple A BBB
( Integer) 2
redis 127.0.0.1:6379> del aaa
(integer) 0

4, get the key value of the data type

Type Foo

Here I repeat what I said last time, the Redis data types are: string (string type), hash (hash type), list (list type), set (set type), Zset (ordered collection type).

The following is a detailed description of the commands for each of the data types.

Second, String type

A string type is the most basic data type in Redis, and he can store any form of string, including binary data. The string type is also the basis for four other data types. That is, the other four data types are made up of string types.

Common commands are as follows:

set key value#赋值, the return value is OK

get key#取值, the return value is the value of the corresponding key

incr key #递增数字, although it is a string type, it can be incremented using this command if it can be an integral type, and the return value is an incremented value, after which the value is fetched again, and a new value is obtained after the increment operation.

Cases:

Redis 127.0.0.1:6379> Get Age
"" "
Redis 127.0.0.1:6379> incr age
(integer)
Redis 127.0.0.1:6379> get age
"33"

incrby key number#这个命令和上一个类似, it's just that you can specify the number of growth by numbers, and the return value is the same as the growth value

Cases:

Redis 127.0.0.1:6379> Incrby age 5
(integer)
redis 127.0.0.1:6379> get age
"38"

decr key#递减数字, like incrementing, there's not much to explain here

decrby key number#类似于incrby, here you can guess, Decrby key number and Incrby Key-number is a meaning

incrbyfloat key floatnumber#增加指定浮点数, this is to add a double-precision floating-point number, more digits will be denied

append key value #向尾部追加值, such as append foo "value", this time in quotation marks to identify a space, the return value is the total length of the value

Cases:

Redis 127.0.0.1:6379> append name ' Hello '
(integer) 9
redis 127.0.0.1:6379> get name
"Joanhello"

strlen key#获取字符串值总长度, the return value is the length and returns 0 if the key does not exist

mget key [key ...] #同时获得多个键值, List of key values

Cases:

Redis 127.0.0.1:6379> mget name age float
1) "Joanhello"
2) "3" "
33.33"

mset key value [key value ...] #同时设置多个键值, successful return OK

Redis 127.0.0.1:6379> mset Name "Joan" age 9 float 33.33
OK
redis 127.0.0.1:6379> mget name age float
1) "Joan"
2) "9"
3) "33.33"

setbit/getbit#这个是位操作, a specific example is as follows:

Redis set foo bar
OK

This assigns the Foo value to bar, the corresponding ASCII code is 98, 97, 114, converted to eight-bit binary as:

01100010, 01100001, 01110010

Perform the following actions:

redis> getbit foo 0
0
redis> getbit foo 1
1
redis> getbit foo 2
1
redis> Getbit Foo 3

So getbit is the binary value (0 or 1) of the specified position of the string type key corresponding to the key, and the index starts at 0.

If the fetch exceeds the maximum value, it also returns 0.

Instead, the setbit is to set the binary value of the key corresponding to the specified position. The return value is the old value for that location. For example

redis> setbit Foo 6 0
(integer) 1

If you want to set a value that exceeds the maximum length, the command sets the value that is not set between the maximum length and the maximum length to 0, and, by the same token, sets a nonexistent key value, automatically setting the binary of all the positions that are not set before it to 0. Such as:

Redis> setbit QQQ 8 1
(integer) 0

The corresponding value of QQQ will be changed to 00000001.

bitcount keyThe command can return the number of binary values in the string type key value of 1.

At the same time, his parameters can also set the scope of the query,

bitcount foo 0 1#意思是只查询ba两个字节中包含二进制值为1的个数.

The last Bitop command, which can be bitwise operated.

bitop OR res foo1 foo2#意思是把foo1和foo2做OR运算, the result is stored in the Res. Bitop supports bit operations with ND, or, XOR, not four.

The operation command for the string that's all. Bit commands are also useful, such as when we are storing sex, we can set the type of sex to a bit, so it's a super province.

Summarize

The above is the entire content of this article, I hope to be able to learn or work to bring certain help, if you have questions you can message exchange.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.