search
HomeDatabaseMysql TutorialNosql之Redis: list数据类型及操作命令

list类据类型及操作命令 一:概述 List类型是按照插入顺序排序的字符串链表。 二:相关操作命令 1: lpush 描述:从list头部添加一个元素.如果key不存在则先创建一个空链表,再将数据从头部插入. 命令格式: lpush key value [value...] 返回值:插入后链表中元素的

list类据类型及操作命令

一:概述
List类型是按照插入顺序排序的字符串链表。

二:相关操作命令

1: lpush

描述:从list头部添加一个元素.如果key不存在则先创建一个空链表,再将数据从头部插入.
命令格式: lpush key value [value...]
返回值:插入后链表中元素的数量。
时间复杂度: O(1)
操作如下:
redis 127.0.0.1:6379> lpush user_list v_1
(integer) 1
redis 127.0.0.1:6379> lrange user_list 0 2
1) “v_1″
redis 127.0.0.1:6379> lpush user_list v_1 v_2 v_3
(integer) 4
redis 127.0.0.1:6379> lrange user_list 0 6
1) “v_3″
2) “v_2″
3) “v_1″
4) “v_1″
2: lpushx
描述:当Key存在时,该命令才会在其所关联的List Value的头部插入参数中给出的Value,否则将不会有任何操作发生。
返回值:插入链表中元素的数量.
时间复杂度: O(1)
操作如下:
redis 127.0.0.1:6379> lpushx user_list_1 aa
(integer) 0
redis 127.0.0.1:6379> lrange user_list_1 0 6
(empty list or set)
redis 127.0.0.1:6379> lpushx user_list v_4
(integer) 5
redis 127.0.0.1:6379> lrange user_list 0 8
1) “v_4″
2) “v_3″
3) “v_2″
4) “v_1″
5) “v_1″

3:lrange
描述:从自定范围内返回list中元素.0表示链表头部的第一个元素。其中start的值也可以为负值,-1将表示链表中的最后一个元素,即尾部元素,-2表示倒数第二个并以此类推。
返回值:返回指定范围内元素列表.
时间复杂度:O(S+N) S为start参数表示的偏移量,N表示元素的数量。
语法格式: lrange key start stop

操作如下:
redis 127.0.0.1:6379> lrange user_list 0 8
1) “v_4″
2) “v_3″
3) “v_2″
4) “v_1″
5) “v_1″
redis 127.0.0.1:6379> lrange user_list -2 -1
1) “v_1″
2) “v_1″
redis 127.0.0.1:6379>

4: lpop
描述:返回并弹出指定Key关联的链表中的第一个元素,即头部元素,。如果该Key不存,返回nilr.
返回值:返回头部元素.
时间复杂度:O(1)
操作如下:
redis 127.0.0.1:6379> lpop user_list
“v_4″

5:llen
描述:获取链表中元素的数量,如果该Key不存在,则返回0.不是list类型KEY时,则报错.
时间复杂度: O(1)
操作如下:
redis 127.0.0.1:6379> llen user_list
(integer) 4
redis 127.0.0.1:6379> llen user_list1
(integer) 0
redis 127.0.0.1:6379> llen name
(error) ERR Operation against a key holding the wrong kind of value

6:lrem
描述:从key对应的list中删除n个和value相同的元素(n 时间复杂度:O(N) N表示链表中元素的数量
返回值:返回被删除元素的个数
操作命令如下:
redis 127.0.0.1:6379> lrange user_list 0 9
1) “v_3″
2) “v_2″
3) “v_1″
4) “v_1″
redis 127.0.0.1:6379> lrem user_list 2 v_1
(integer) 2
redis 127.0.0.1:6379> lrange user_list 0 9
1) “v_3″
2) “v_2″
7: lset
描述:? 更新某个位置元素的值,如果索引值Index超出了链表中元素的数量范围,该命令将返回相关的错误信息。
时间复杂度:O(N) N表示链表中元素的数量 注:但是设定头部或尾部的元素时,其时间复杂度为O(1)
操作命令如下:
redis 127.0.0.1:6379> lset user_list 3 val
(error) ERR index out of range
redis 127.0.0.1:6379> lset user_list 1 vv
OK
redis 127.0.0.1:6379> lrange user_list 0 3
1) “v_3″
2) “vv”

8:lindex
描述:获取list中指定元素.
时间复杂度: O(N) 注:对于头部或尾部元素,其时间复杂度为O(1)。
返回值:返回请求的元素,如果index超出范围,则返回nil。
操作命令如下:
redis 127.0.0.1:6379> lindex user_list 8
(nil)
redis 127.0.0.1:6379> lindex user_list 0
“v_3″

9: ltrim
描述: 裁剪一个 List 到指定范围
时间复杂度:O(N) N:被删除的元素数量
操作命令如下:
redis 127.0.0.1:6379> lrange user_list 0 9
1) “v9″
2) “v7″
3) “v6″
4) “v5″
5) “v_3″
6) “vv”
redis 127.0.0.1:6379> ltrim user_list 0 3
OK
redis 127.0.0.1:6379> lrange user_list 0 9
1) “v9″
2) “v7″
3) “v6″
4) “v5″

10:linsert
描述:在list特定位置前或后面添加元素
语法格式:LINSERT key BEFORE|AFTER pivot value
时间复杂度:O(N) N表示在找到该元素pivot之前需要遍历的元素数量
返回值:成功插入后链表中元素的数量,如果没有找到pivot,返回-1,如果key不存在,返回0。
操作如下:
redis 127.0.0.1:6379> lrange user_list 0 9
1) “v9″
2) “v7″
3) “v6″
4) “v5″
redis 127.0.0.1:6379> linsert user_list before v9 v0
(integer) 5
redis 127.0.0.1:6379> lrange user_list 0 9
1) “v0″
2) “v9″
3) “v7″
4) “v6″
5) “v5″
redis 127.0.0.1:6379> linsert user_list after v9 v11
(integer) 6
redis 127.0.0.1:6379> lrange user_list 0 9
1) “v0″
2) “v9″
3) “v11″
4) “v7″
5) “v6″
6) “v5″
redis 127.0.0.1:6379>

11: rpush
描述:往list尾部压入元素
时间复杂度: O(1)
语法格式:RPUSH key value [value ...]
返回值:插入后的元素数量
操作命令如下:
redis 127.0.0.1:6379> rpush user_list v12 v13
(integer) 8
redis 127.0.0.1:6379> lrange user_list 0 10
1) “v0″
2) “v9″
3) “v11″
4) “v7″
5) “v6″
6) “v5″
7) “v12″
8) “v13″

12: rpushx
描述:当key存时,往list尾部压入元素,不存在没有操作
时间复杂度: O(1)
返回值:插入后的元素数量

13: rpop
描述:弹出尾部元素.如果该Key不存,返回nil。
时间复杂度: O(1)

10: rpoplpush
描述:弹出 (源list)中最后一个元素并将其压入 (目标list)
时间复杂度: O(1)
返回值:返回弹出和插入的元素。
操作命令如下:
redis 127.0.0.1:6379> rpoplpush user_list user_list_1
“v13″
redis 127.0.0.1:6379> lrange user_list_1 0 2
1) “v13″

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

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.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft