search
HomeDatabaseMysql Tutorialstring主要操作函数

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
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
Explain the InnoDB Buffer Pool and its importance for performance.Explain the InnoDB Buffer Pool and its importance for performance.Apr 19, 2025 am 12:24 AM

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

MySQL vs. Other Programming Languages: A ComparisonMySQL vs. Other Programming Languages: A ComparisonApr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

Learning MySQL: A Step-by-Step Guide for New UsersLearning MySQL: A Step-by-Step Guide for New UsersApr 19, 2025 am 12:19 AM

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL: Essential Skills for Beginners to MasterMySQL: Essential Skills for Beginners to MasterApr 18, 2025 am 12:24 AM

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL: Structured Data and Relational DatabasesMySQL: Structured Data and Relational DatabasesApr 18, 2025 am 12:22 AM

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL: Key Features and Capabilities ExplainedMySQL: Key Features and Capabilities ExplainedApr 18, 2025 am 12:17 AM

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

The Purpose of SQL: Interacting with MySQL DatabasesThe Purpose of SQL: Interacting with MySQL DatabasesApr 18, 2025 am 12:12 AM

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

MySQL for Beginners: Getting Started with Database ManagementMySQL for Beginners: Getting Started with Database ManagementApr 18, 2025 am 12:10 AM

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

See all articles

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 Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.