Make writing a habit together! This is the second day of my participation in the “Gold Digging Day New Plan · April More text challenge”. Click here for more details.

Redis has five commonly used data types. This article focuses on the use of the String type, common commands, and data structures.

The data structure

String is the most basic type of Redis. Each key corresponds to a value. A Redis string value can be a maximum of 512 MB.

The String type is binary safe. This means that a Redis string can contain any data. Like JPG images or serialized objects.

What is binary security?

Generally speaking, in C language, “\0” is used to indicate the end of the string. If there is a “\0” character in the string itself, then the string will be truncated, that is, not binary safe; A string is binary safe if it is read or written by a mechanism that does not damage its contents.

How does redis String ensure binary security?

Simple Dynamic Strings (SDS) is one of Redis’ basic data structures for storing string and integer data. SDS is compatible with C language standard string processing functions, and on this basis, it ensures binary security.

Buf in an SDS object is a flexible array. When called by SDS, BUF is returned directly. Since buF is a direct pointer to content, it is compatible with C language functions. When the content is actually read, SDS limits the read length by len instead of “\0”, thus ensuring binary security.

What is the data structure of SDS?

ArrayList is a string that can be modified. It is similar to The Java ArrayList in internal structure and reduces the frequent allocation of memory by preallocating redundant space.

Redis usually allocates more memory for String capacity than len.

There are two situations during capacity expansion:

  • If the character string is less than 1 MB, the current capacity is doubled during each capacity expansion.
  • If the string length is greater than 1 MB, 1 MB is added each time.

The maximum length is 512 MB.

Common commands

The set command

When we connect to the client and type the set command on the command line, the following prompt will appear:

127.0.0.1:6379 >set key value [EX seconds|PX milliseconds|EXAT timestamp|PXAT milliseconds-timestamp|KEEPTTL] [NX|XX] [GET]
Copy the code

There are a series of parameters, which we explain one by one:

  • Let’s start with a set of parameters that set the expiration time:

    • EX: Sets the timeout seconds for the key
    • PX: Sets the timeout milliseconds for the key
    • EXAT: Sets the expiration timestamp of the key, accurate to seconds
    • PXAT: Sets the expiration timestamp of the key to milliseconds
    • KEEPTTL: sets the key to never expire

    Example Set the age to 100 and expire after 3 seconds:

    127.0.0.1:6379 >set age 100 EX 3
    OK
    Copy the code
  • Arguments with logical judgments:

    • NX: If the key does not exist, the setting succeeds
    • XX: If the key exists, the setting succeeds

    As an example, set age to 100:

    127.0.0.1:6379 >setAge 100 nx (nil) 127.0.0.1:6379>set age 100 xx
    OK
    Copy the code
  • Set values and return:

    • GET: Returns the old value while setting key to the new value

    To use an example, set age to 1000:

    127.0.0.1:6379 >set age 1000 get
    "100"
    Copy the code

Get, Append, strlen, setnx

These commands are all relatively simple. Put them together:

  • Get: Obtains a value based on the key

  • Append: Appends the given value to the end of the original key value

    127.0.0.1:6379 > get the age"1000"
    127.0.0.1:6379> append age 1
    (integer) 5
    127.0.0.1:6379> get age
    "10001"
    Copy the code
  • Strlen: Gets the length of the value

    127.0.0.1:6379 > strlen age (integer5)Copy the code
  • Setnx: If the key exists, the setting fails. If the key does not exist, the setting succeeds. The value 1 is returned on success, and 0 is returned on failure

    127.0.0.1:6379> setnx age 100
    (integer0 127.0.0.1:6379> setNx age1 100 (integer1)Copy the code

Incr and decr

These two operations, with one of the most important properties, atomicity, are not interrupted by the thread scheduling mechanism, i.e. there is no thread context switch.

The atomicity of redis single commands is due to its single thread, as explained in a later article.

  • Incr: Increments the numeric value stored in the key by 1. If no value exists, the value is assigned to 1

    127.0.0.1:6379 > incr age2 (integer) 1
    127.0.0.1:6379> incr age2
    (integer2)Copy the code
  • Decr: Decates the numeric value stored in the key by 1, or assigns -1 if the value does not exist

    127.0.0.1:6379 > decr age3 (integer1 127.0.0.1:6379> Decr age3 (integer2) -Copy the code
  • The above two operations, along with two custom step commands, specify value to increase or decrease the specified value

    • Incrby: Adds the specified step
    • Decrby: decreases the specified step size
    127.0.0.1:6379> incrby age2 2
    (integer) 4
    127.0.0.1:6379> decrby age3 2
    (integer4) -Copy the code

Batch operation commands

Redis provides some commands for batch operations:

  • Mset: sets key-value in batches

    127.0.0.1:6379> mset age 100 age1 101 age2 102 age3 103
    OK
    Copy the code
  • Mget: Batch fetch value, no return nil

    127.0.0.1:6379> mget age age1 age2 age3 age4
    1) "100"
    2) "101"
    3) "102"
    4) "103"
    5) (nil)
    Copy the code
  • Msetnx: batch set nonexistent values, this command is atomic, if there is a failure, all failed, return 1 on success, return 0 on failure:

    127.0.0.1:6379> msetnx age 50 age4 104
    (integer0 127.0.0.1:6379> get age4 (nil) 127.0.0.1:6379> msetNx age5 105 age4 104 (integer1)Copy the code

Scope of operation

For string values, Redis supports reading and setting data in its range:

Given a string as follows:

127.0.0.1:6379 >setFlag goodgoodstudy, daydayUp OK 127.0.0.1:6379> get flag"goodgoodstudy,daydayup"
Copy the code
  • Getrange: Gets the content between specified ranges
    127.0.0.1:6379> getrange flag 5 10
    "oodstu"
    Copy the code
  • Setrange: sets the value content starting at the specified position
    127.0.0.1:6379> setrange flag 5 11111
    (integer) 22
    127.0.0.1:6379> get flag
    "goodg11111udy,daydayup"
    Copy the code

setex

When setting the key value, set the expiration time

  • setex key seconds value

    127.0.0.1:6379> setex age 100 100
    OK
    127.0.0.1:6379> ttl age
    (integer) 96
    127.0.0.1:6379> get age
    "100"
    Copy the code

    So much for the redis string type