Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Redis Notes-String types and Operations (2)

2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

String type and operation

String is a simple type, one Key corresponds to one Value. The String type is binary safe. Redis's String can contain any data, such as jpg pictures or serialized objects

Set

Set the value of Key to value of type String

For example: add a key-value pair of name=lijie

127.0.0.1 setnx 6379 > set name lijie// gets the value 127.0.0.1 setnx 6379 > get name// is assigned repeatedly, and will be overwritten

Set the value of key to value of type String. If key already exists, return 0. Nx means not exist. For example, let's add another key-value pair of name=lijie_new

127.0.0.1 name 6379 > setnx name liyanyan (integer) 0 / since the value of name has been set previously, 0 is returned and Setex is not overwritten

Set the value of key to value of type String, and specify the validity period for this key.

For example, we add a key-value pair of haircolor=red and specify a validity period of 10 seconds.

127.0.0.1 setex haircolor 6379 > get haircolor 10 redOK127.0.0.1:6379 > get haircolor "red" 127.0.0.1 get haircolor (nil) setrange

Sets the substring of the value of the specified key

For example, we want to replace lijie's 126mailbox with gmail mailbox.

127.0.0.1 gmail.com 6379 > set name lijie@126.comOK127.0.0.1:6379 > setrange name 6 gmail.com (integer) 15127.0.0.1 gmail.com 6379 > get name "lijie@gmail.com// is set to 127.0.0.1 > setrange name 6 126.com (integer) 15127.0.0.1 gmail.com 6379 > get name" lijie@126.comom "/ / We find that the length of the replacement string is smaller than the original string. Then the characters following the original string will be preserved in Mset

Set multiple key values at a time. If ok is returned successfully, all values are set, and 0 is returned if failure occurs, indicating that no values have been set.

127.0.0.1 mset key1 lijie key2 chenxl key3 zhangsanOK127.0.0.1:6379 > get key1 "lijie" 127.0.0.1 lijie > get key2 "chenxl" 127.0.0.1 > get key3 "zhangsan" msetnx

If you set more than one key at a time, ok is returned successfully, indicating that all values are set, and 0 is returned if failure occurs, indicating that no value has been set, but the existing key will not be overwritten.

As long as one key exists, none of the other key can be set successfully.

Get

Get a value

Getset

Set the value of key and return the old value of key

Getrange

Gets the substring of the value of the value of key

127.0.1 liji 6379 > getrange name 0 3 "liji" mget

Get the values of more than one key at a time. If the corresponding key does not exist, return nil

127.0.0.1 mget key1 key2 key3 key41) "lijie" 2) "chenxl" 3) "zhangsan" 4) (nil) incr

Increment the value of key and return a new value. If key does not exist, set the value of key to 0, and then add it.

127.0.0.1 incr age (integer) 11127.0.0.1 incr age (integer) 12127.0.1 > incr age (integer) 13127.0.1 > incr age (integer) 14incrby

Similar to incr, the value is added to the specified step size. When key does not exist, ke is set, and the original value is considered to be 0.

/ / the value of myage does not exist 127.0.0.1 integer 6379 > incrby myage 3 (integer) 3127.0.0.1 integer 6379 > 127.0.0.1 integer 6379 > incrby myage 3 (integer) 9

If you want to decrement, the value can be set to negative

Decr

Subtract the value of key

Decrby

Similar to decr to specify step size impairment

Append

Appends value to the string of the specified key and returns the length of the new string

127.0.0.1 append name 6379 > get name "lijie@126.comom123" strlen

Gets the length of the value of the specified key

127.0.0.1 6379 > strlen name (integer) 18

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report