Home Database Mysql Tutorial string主要操作函数

string主要操作函数

Jun 07, 2016 pm 02:53 PM
string main function operate

string主要操作函数 1、新增 www.2cto.com a)set 语法:set key value 解释:把值value赋给key,如果key不存在,新增;否则,更新 [plain] [root@xsf001 ~]# redis-cli redis 127.0.0.1:6379 set user.1.name zhangsan #设置user.1.name 为zhangsan OK redi


string主要操作函数

 

1、新增

  www.2cto.com  

a)set

 

语法:set key value

 

解释:把值value赋给key,如果key不存在,新增;否则,更新

 

[plain] 

[root@xsf001 ~]# redis-cli   

redis 127.0.0.1:6379> set user.1.name zhangsan #设置user.1.name 为zhangsan  

OK  

redis 127.0.0.1:6379> set user.name 45      #设置user.1.name 为45  

OK  

               b)setnx

语法:setnx key value

  www.2cto.com  

解释:只insert不update,即,仅仅key不存在时,则设置key的值为value,并返回1,否则返回0  。setnx 是set if not exists 的缩写

 

[plain] 

redis 127.0.0.1:6379> setnx user.1.name zhangsan   #user.1.name已经存在,则返回0  

(integer) 0  

redis 127.0.0.1:6379> setnx user.2.name zhangsan  #user.2.name不存在,则设置  

(integer) 1  

 c)setex

语法: setex key seconds value

 

解释:设置key的过期时间和值。过期时间seconds单位是秒。设置过期时间和值是原子操作,如果redis仅仅当做缓存,这个很命令很有用。

 

[plain] 

redis 127.0.0.1:6379> setex user.2.age 2 14  #把user.2.age 的值设14 并且2秒后过期失效  

OK  

redis 127.0.0.1:6379> get user.2.age  #失效前  

"14"  

redis 127.0.0.1:6379> get user.2.age #失效后  

(nil)  

 d)mset

语法:mset key value [key value ...]

  www.2cto.com  

解释:同时设置多个key-value

 

[plain] 

redis 127.0.0.1:6379> mset user.4.name lisi user.4.age 34  #设置user.4.name=lisi,user.4.age=34  

OK  

redis 127.0.0.1:6379> get user.4.name  

"lisi"  

redis 127.0.0.1:6379> get user.4.age  

"34"  

 

             e)msetnx

语法:msetnx key value [key value ...]

 

解释:所有key都不存在才执行set操作

 

[plain] 

redis 127.0.0.1:6379> msetnx user.4.name lisi user.4.age 34  #key 都设置过,无法再次set  

(integer) 0  

redis 127.0.0.1:6379> msetnx user.4.name lisi user.4.std 3   #key user.4.name 曾设置过,无法再次设置  

(integer) 0  

redis 127.0.0.1:6379> msetnx user.4.tech lisi user.4.std 3   #key都没有设置过,可以再次设  

(integer) 1  

 

2、查询

a)get

 

语法:get key

 

解释:获取key所set的值

  www.2cto.com  

[plain] 

redis 127.0.0.1:6379> get user.4.name  #获取user.4.name 的值  

"lisi"  

redis 127.0.0.1:6379> get user.4.age  

"34"  

redis 127.0.0.1:6379> get user.4.tech  

"lisi"  

redis 127.0.0.1:6379> get user.4.std  

"3"  

 b)mget

语法: get key [key]

 

解释:批量获取key的值。程序一次获取多个值,可以减少网络连接损耗。

 

[plain] 

redis 127.0.0.1:6379> mget user.4.name user.4.age user.4.std #批量获取存在key的值  

1) "lisi"   #user.4.name的值  

2) "34"     #user.4.age 的值  

3) "3"      #user.4.std 的值  

  

redis 127.0.0.1:6379> mget user.4.name user.4.age user.4.std user.4.fri  #key user.4.fri 不存在仍然可以返回  

1) "lisi"  

2) "34"  

3) "3"  

4) (nil) #user.4.fri 的值  

            c)getrange

语法:getrange key star end

 

解释:获取存储在key中value的字串。字符串的截取有star和end决定,字符串的第一个字符编号是0,第二个是1,一次类推;如果是负数,-1是最后一个字符,-2是倒数第二个字符,一次类推。

 

[plain] 

redis 127.0.0.1:6379> get user.4.name  

"lisi"  

redis 127.0.0.1:6379> getrange user.4.name 0 3  # 0 表示开始  

"lisi"  

redis 127.0.0.1:6379> getrange user.4.name 1 2  

"is"  

redis 127.0.0.1:6379> getrange user.4.name 1 -2 #-2 表示倒数第二  

"is"  

redis 127.0.0.1:6379> getrange user.4.name -1 -2  # end 》 start  

""  

redis 127.0.0.1:6379> getrange user.4.name 1 66  #end 》 值的长度  

"isi"  

  3、修改

a)getset

 

语法:getset key value

 

解释:设置key的值,并返回key的旧值。

 

[plain] 

redis 127.0.0.1:6379> get user.4.name   #存在的key  

"lisi"  

redis 127.0.0.1:6379> getset user.4.name wangwu  #把存在的user.4.name设置为wagnwu  

"lisi"  

redis 127.0.0.1:6379> get user.4.name    

"wangwu"  

redis 127.0.0.1:6379> get user.5.name  #不存在的key  

(nil)  

redis 127.0.0.1:6379> getset user.5.name lisi  

(nil)  

redis 127.0.0.1:6379> get user.5.name  

"lisi"  

  b) append

语法:append key value

  www.2cto.com  

解释:key存在,在旧值的后面追加value;key不存在,直接set

 

[plain] 

redis 127.0.0.1:6379> get user.4.name #存在的key  

"wangwu"  

redis 127.0.0.1:6379> append user.4.name 01  

(integer) 8  

redis 127.0.0.1:6379> get user.4.name  

"wangwu01"  

[plain] 

redis 127.0.0.1:6379> get user.6.name #不能存在的key  

(nil)  

redis 127.0.0.1:6379> append user.6.name jim  

(integer) 3  

redis 127.0.0.1:6379> get user.6.name  

"jim"  

 

c)setrange

语法:setrange key offset value

 

解释:用value重写key值的一部分,偏移量由offset指定

 

[plain] 

redis 127.0.0.1:6379> get user.4.name  #key存在  

"wangwu01"  

redis 127.0.0.1:6379> setrange user.4.name 0 lisi  

(integer) 8  

redis 127.0.0.1:6379> get user.4.name  

"lisiwu01"  

redis 127.0.0.1:6379> setrange user.4.name 9 lisi  # offset 》字符串长度  

(integer) 13  

redis 127.0.0.1:6379> get user.4.name  

"lisiwu01\x00lisi"  

redis 127.0.0.1:6379> setrange user.4.name 8 lisi  

(integer) 13  

redis 127.0.0.1:6379> get user.4.name  

"lisiwu01lisii"  

redis 127.0.0.1:6379> get user.6.std #key 不存在  

(nil)  

redis 127.0.0.1:6379> setrange user.6.std 0 3  

(integer) 1  

redis 127.0.0.1:6379> get user.6.std  

"3"  

 d)incr

语法:incr key

 

解释:key中如果存储的是数字,则可以通过incr递增key的值,返回递增后的值。如果key不能存在,视为初始值为0

 

[plain] 

redis 127.0.0.1:6379> get user.7.age #key不存在 ,初始值视为0,  

(nil)  

redis 127.0.0.1:6379> incr user.7.age   

(integer) 1  

redis 127.0.0.1:6379> get user.7.age  

"1"  

redis 127.0.0.1:6379> incr user.7.age  

(integer) 2  

e)incrby

 

语法:incrby key increment

 

解释:用指定的步长增加key存储的数字。如果步长increment是负数,则减

 

[plain] 

redis 127.0.0.1:6379> get user.7.age  

"3"  

redis 127.0.0.1:6379> incrby user.7.age 15  #增加15  

(integer) 18  

redis 127.0.0.1:6379> get user.7.age  

"18"  

redis 127.0.0.1:6379> incrby user.7.fri 18  #key不能存在,原值视为0  

(integer) 18  

redis 127.0.0.1:6379> get user.7.fri  

"18"  

redis 127.0.0.1:6379> incrby  user.7.age -1  #步长为负  

(integer) 17  

redis 127.0.0.1:6379> get user.7.age  

"17"  

 

f)decr

语法:decr key

 

解释:递减key保存的数字,如果key不存在,初始值视为0

 

[plain] 

redis 127.0.0.1:6379> get user.7.age  

"17"  

redis 127.0.0.1:6379> decr user.7.age  

(integer) 16  

redis 127.0.0.1:6379> decr user.7.num #key 不存在,初始值视为0  

(integer) -1  

redis 127.0.0.1:6379> decr user.7.num  

(integer) -2  

g)decrby

 

语法:decrby key decrement

 

解释:用指定的步长递减key的值,如果步长decrment是负值,则递增

  www.2cto.com  

[plain] 

redis 127.0.0.1:6379> decrby user.7.num 4  #递减  

(integer) -6  

redis 127.0.0.1:6379> decrby user.7.num -9 #负值,递增  

(integer) 3  

 

注意:递增递减系列的函数,只能对保存的是数字的key操作,不能是字符串

  4)删除

 

语法:del key [key]

 

解释:删除指定的key,返回删除key的个数

 

[plain] 

redis 127.0.0.1:6379> del user.7.num  

(integer) 1  

redis 127.0.0.1:6379> get user.7.num  

(nil)  

redis 127.0.0.1:6379> del user.7.age user.7.fri #删除多个key  

(integer) 2  

redis 127.0.0.1:6379> mget user.7.age user.7.fri  

1) (nil)  

2) (nil)  

  5)其他

 

语法:strlen key

 

解释:获取key中所存储值的长度

 

[plain] 

redis 127.0.0.1:6379> get user.1.name  

"45"  

redis 127.0.0.1:6379> strlen user.1.name  #user.1.name 长度  

(integer) 2  

redis 127.0.0.1:6379> strlen user.8.name #key不存在  

(integer) 0  

通过上面的大量demo,对string的操作基本总结完毕。
 

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Tips for dynamically creating new functions in golang functions Tips for dynamically creating new functions in golang functions Apr 25, 2024 pm 02:39 PM

Go language provides two dynamic function creation technologies: closure and reflection. closures allow access to variables within the closure scope, and reflection can create new functions using the FuncOf function. These technologies are useful in customizing HTTP routers, implementing highly customizable systems, and building pluggable components.

Considerations for parameter order in C++ function naming Considerations for parameter order in C++ function naming Apr 24, 2024 pm 04:21 PM

In C++ function naming, it is crucial to consider parameter order to improve readability, reduce errors, and facilitate refactoring. Common parameter order conventions include: action-object, object-action, semantic meaning, and standard library compliance. The optimal order depends on the purpose of the function, parameter types, potential confusion, and language conventions.

How to write efficient and maintainable functions in Java? How to write efficient and maintainable functions in Java? Apr 24, 2024 am 11:33 AM

The key to writing efficient and maintainable Java functions is: keep it simple. Use meaningful naming. Handle special situations. Use appropriate visibility.

Complete collection of excel function formulas Complete collection of excel function formulas May 07, 2024 pm 12:04 PM

1. The SUM function is used to sum the numbers in a column or a group of cells, for example: =SUM(A1:J10). 2. The AVERAGE function is used to calculate the average of the numbers in a column or a group of cells, for example: =AVERAGE(A1:A10). 3. COUNT function, used to count the number of numbers or text in a column or a group of cells, for example: =COUNT(A1:A10) 4. IF function, used to make logical judgments based on specified conditions and return the corresponding result.

Comparison of the advantages and disadvantages of C++ function default parameters and variable parameters Comparison of the advantages and disadvantages of C++ function default parameters and variable parameters Apr 21, 2024 am 10:21 AM

The advantages of default parameters in C++ functions include simplifying calls, enhancing readability, and avoiding errors. The disadvantages are limited flexibility and naming restrictions. Advantages of variadic parameters include unlimited flexibility and dynamic binding. Disadvantages include greater complexity, implicit type conversions, and difficulty in debugging.

What are the benefits of C++ functions returning reference types? What are the benefits of C++ functions returning reference types? Apr 20, 2024 pm 09:12 PM

The benefits of functions returning reference types in C++ include: Performance improvements: Passing by reference avoids object copying, thus saving memory and time. Direct modification: The caller can directly modify the returned reference object without reassigning it. Code simplicity: Passing by reference simplifies the code and requires no additional assignment operations.

What is the difference between custom PHP functions and predefined functions? What is the difference between custom PHP functions and predefined functions? Apr 22, 2024 pm 02:21 PM

The difference between custom PHP functions and predefined functions is: Scope: Custom functions are limited to the scope of their definition, while predefined functions are accessible throughout the script. How to define: Custom functions are defined using the function keyword, while predefined functions are defined by the PHP kernel. Parameter passing: Custom functions receive parameters, while predefined functions may not require parameters. Extensibility: Custom functions can be created as needed, while predefined functions are built-in and cannot be modified.

C++ Function Exception Advanced: Customized Error Handling C++ Function Exception Advanced: Customized Error Handling May 01, 2024 pm 06:39 PM

Exception handling in C++ can be enhanced through custom exception classes that provide specific error messages, contextual information, and perform custom actions based on the error type. Define an exception class inherited from std::exception to provide specific error information. Use the throw keyword to throw a custom exception. Use dynamic_cast in a try-catch block to convert the caught exception to a custom exception type. In the actual case, the open_file function throws a FileNotFoundException exception. Catching and handling the exception can provide a more specific error message.

See all articles