search
HomeDatabaseRedisRedis basic data types and operations (summary sharing)

This article brings you relevant knowledge about Redis, which mainly introduces the relevant content about basic data types and operations. Let’s take a look at it together. I hope it will be helpful to everyone.

Redis basic data types and operations (summary sharing)

Recommended learning: Redis video tutorial

Redis Introduction

redis is a tool mainly developed by developer Salvatore Sanfilippo The open source memory data structure memory developed by Antirez is used to improve the scalability of its website. It can be used as a database, cache, message broker, etc., and can be used in combination with Redis in the project.

Redis has rich data structures, excellent speed, and complete functions. Many companies are using Redis. It has the following characteristics:

  • fast
  • NoSQL database
  • Supports many environments

Redis is an in-memory database, which means that all data is stored in memory, not on the hard disk (of course, full backup of the hard disk is also supported) and incremental backup). The characteristic of memory is that it can support fast access and fast search, but it is also subject to space limitations.

In the database architecture, Redis is usually located between the client and the database to reduce the load on the NoSQL database or application and reduce data access latency during caching.

Main uses of Redis

  • Queue
  • Publish/Subscribe
  • Real-time analysis
  • Machine Learning: Quickly process large, diverse and fast data for machine learning
  • Geospatial Processing
  • Rankings/Counts (that is, the hot searches you see every day)
  • Session Cache
  • Full Page Cache

Companies using Redis

Who uses Redis?

Redis is used with Start

There are many installation tutorials on the Internet. Here is the Mac side as an example. After installing the Redis server, you can start the Redis server through the following command:

redis-server

And start redis through the following command -cli client:

redis-cli -h 127.0.0.1 -p 6379

As shown in the figure:

Now we can accept an optional message as a parameter through the Redis PING command, this The command is usually used to test whether the connection between the client and the server is normal. If the user executes this command with no parameters, the server will return PONG as a reply to the client if the connection is normal:

127.0.0.1:6379> PINGPONG

The actual test is as follows:

The preliminary introduction to Redis is completed. Let’s take a look at the basic data structure machine common operations of Redis. .

Basic data types of Redis

  • String
  • Hash
  • List
  • Collection
  • Ordered Set
  • HyperLogLog
  • Bitmap

This article will introduce the first five common data structures, and more types will be introduced separately in other articles. Note that the five common basic data types are as follows.

String

String (string) is the most basic key-value pair type of Redis. This type can maintain both ordinary text and serialized binary data.

The string type associates a single key with a single value in the database. The associated key and value can be ordinary text data, or pictures, videos, audios, compressed More complex binary data such as files. The string type can store up to 512M of data.

Some common operations on strings

  • SET command: Set the corresponding value for a string, such as SET number "10086"
  • GET command: Get the value corresponding to the key
127.0.0.1:6379> SET number "10086"OK127.0.0.1:6379> GET number"10086"127.0.0.1:6379>
  • GETRANGE: Get the intercepted string content
  • STRLEN: Get the string length, such as STRLEN email
  • SETEX: Set KEY-VALUE with expiration time (seconds), such as SETEX city 5 Beijing

Flash sale activity PSETEX setting milliseconds

  • MSET:设置多个 KEY-VALUE ,如 MSET username jack sex male age 24
  • MGET:获取多个 VALUE,如 MGET username sex age
  • APPEND: 用于在字符串结尾追加内容
  • INCR:数字自增加1 ,如 INCR number
127.0.0.1:6379> INCR number(integer) 10087127.0.0.1:6379>
  • INCRBY:数字加上指定的整数值
  • INCRBYFLOAT:数字加上指定的浮点数
  • DECR:数字自增减一
  • DECBY:数字减去指定的整数值

这些命令,大家都可以自己在。 Redis 客户端进行测试。

哈希类型

用来保存更复杂的结构化数据

  • HSET:设置哈希表字段
  • HMSET:设置哈希表多个字段
  • HGET:获取哈希表字段值,如 HGET 8000 ename
  • HMGET:获取多个哈希表字段值,如 HMGET 80000 ename job deptno
  • HGETALL:获取所有哈希表字段值
  • HKEYS:获取所有哈希表字段名
  • HLEN:哈希表中的字段数量
  • HEXISTS:判断哈希表是否存在某个字段
  • HVALS:获取哈希表中的所有字段值
  • HDEL:删除哈希表的字段
  • HINCRBY:让哈希表某个字段值加上指定的整数值,如 HINCRBY 8000 deptono 10
  • HINCRBYFLOART:让哈希表某个字段值加上指定的浮点数

列表类型

当我们需要向 VALUE 保存序列化的数据,可以使用列表类型

RPUSH dname 技术部 后勤部 售后部
LPUSH dname 秘书处
LSET dname 2 销售部
LRANGE dname 0 -1
  • RPUSH:在列表末尾新增值
  • LPUSH:在列表开头新增值
  • LLEN:获取列表长度
  • LINDEX:获取列表某个元素,如 lindex dname 0
  • LINSERT:在某个位置插入元素,如 linsert dname before 秘书处 董事会
  • LPOP:删除最左边的元素 LPOP dname
  • RPOP:删除列表最右边的元素 RPOP dname
  • LREM:删除列表某个元素,如
RPUSH employee Scott
RPUSH employee Jack
RPUSH employee Scott

LREM employee 1 Scott # 删除第一个Scott,不是指索引为一

集合类型

假如要求数据不允许重复,则可以使用集合类型。

集合操作

SADD empno 8000SADD empno 8001SADD empno 8002SADD empno 8003 8004 8005SMEMBERS empno
  • SADD:将给定值添加到集合
  • SCARD:获取集合长度,如: SCARD empno
  • SISMEMBER:判断是否含有某个元素,如 SISMENBER empno 8000
  • SREM:删除某个元素
  • SPOP:随机删除并返回集合的某个元素,如 SPOP empno
  • SRANDMEMBER:随机返回集合中的元素,如 SRANDMEMBER empno 5
  • SUNION:组合两个或多个集合并返回所有元素的列表
  • SMOVE:将成员从一个集合移动到另一个集合

有序集合

带有排序功能的集合,Redis 按照元素分数值排序

ZADD keyword 0 "han" 0 "jack ma" 0 "Andrew wu"ZINCRBY keyword 1 "han"ZINCRBY keyword 5 "jack ma"ZINCRBY keyword 2 "Andrew wu"ZREVRANGE key 0 -1
  • ZCARD:获取有序集合长度
  • ZCOUNT:查询某个分数值区间内的元素数量,如 ZCOUNT keyword 5 10
  • ZSCORE:查询元素的分数值
  • ZRANGE:获取有序集合的内容(升序),如 ZRANGE keyword 0 -1
  • ZREVRANGE:获取有序集合的内容(降序),如 ZREVRANGE keyword 0 -1
  • ZRANGEBYSCORE:获取分数值区间内的集合内容(升序),如
zrangebyscore keyword 5 10  # 5-10
zrangebyscore keyword 5 (10  # 大于等于5, 小于 10
zrangebyscore keyword 100000 +inf
  • ZREVRANGEBYSCORE:获取分数值区间内的集合内容(降序) zrevrangebyscore keyword 10 5
  • ZRANK:获取元素的升序排名(从0开始)zrank keyword "xx"
  • ZREVRANK:获取元素的降序排名(从0开始)
  • ZREM:删除有序集合中的元素 ZREM keyword "x" "y"
  • ZREMRANGEBYRANK:删除排名区间内的元素 zremrangebyrank keyword 0 2
  • ZREMRANGEBYSCORE:删除分数值区间内的元素 zremrangebyscore keyword 0 -3

zremrangebyscore keyword inf (5000)

推荐学习:Redis视频教程

The above is the detailed content of Redis basic data types and operations (summary sharing). For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:掘金. If there is any infringement, please contact admin@php.cn delete
Redis's Role: Exploring the Data Storage and Management CapabilitiesRedis's Role: Exploring the Data Storage and Management CapabilitiesApr 22, 2025 am 12:10 AM

Redis plays a key role in data storage and management, and has become the core of modern applications through its multiple data structures and persistence mechanisms. 1) Redis supports data structures such as strings, lists, collections, ordered collections and hash tables, and is suitable for cache and complex business logic. 2) Through two persistence methods, RDB and AOF, Redis ensures reliable storage and rapid recovery of data.

Redis: Understanding NoSQL ConceptsRedis: Understanding NoSQL ConceptsApr 21, 2025 am 12:04 AM

Redis is a NoSQL database suitable for efficient storage and access of large-scale data. 1.Redis is an open source memory data structure storage system that supports multiple data structures. 2. It provides extremely fast read and write speeds, suitable for caching, session management, etc. 3.Redis supports persistence and ensures data security through RDB and AOF. 4. Usage examples include basic key-value pair operations and advanced collection deduplication functions. 5. Common errors include connection problems, data type mismatch and memory overflow, so you need to pay attention to debugging. 6. Performance optimization suggestions include selecting the appropriate data structure and setting up memory elimination strategies.

Redis: Real-World Use Cases and ExamplesRedis: Real-World Use Cases and ExamplesApr 20, 2025 am 12:06 AM

The applications of Redis in the real world include: 1. As a cache system, accelerate database query, 2. To store the session data of web applications, 3. To implement real-time rankings, 4. To simplify message delivery as a message queue. Redis's versatility and high performance make it shine in these scenarios.

Redis: Exploring Its Features and FunctionalityRedis: Exploring Its Features and FunctionalityApr 19, 2025 am 12:04 AM

Redis stands out because of its high speed, versatility and rich data structure. 1) Redis supports data structures such as strings, lists, collections, hashs and ordered collections. 2) It stores data through memory and supports RDB and AOF persistence. 3) Starting from Redis 6.0, multi-threaded I/O operations have been introduced, which has improved performance in high concurrency scenarios.

Is Redis a SQL or NoSQL Database? The Answer ExplainedIs Redis a SQL or NoSQL Database? The Answer ExplainedApr 18, 2025 am 12:11 AM

RedisisclassifiedasaNoSQLdatabasebecauseitusesakey-valuedatamodelinsteadofthetraditionalrelationaldatabasemodel.Itoffersspeedandflexibility,makingitidealforreal-timeapplicationsandcaching,butitmaynotbesuitableforscenariosrequiringstrictdataintegrityo

Redis: Improving Application Performance and ScalabilityRedis: Improving Application Performance and ScalabilityApr 17, 2025 am 12:16 AM

Redis improves application performance and scalability by caching data, implementing distributed locking and data persistence. 1) Cache data: Use Redis to cache frequently accessed data to improve data access speed. 2) Distributed lock: Use Redis to implement distributed locks to ensure the security of operation in a distributed environment. 3) Data persistence: Ensure data security through RDB and AOF mechanisms to prevent data loss.

Redis: Exploring Its Data Model and StructureRedis: Exploring Its Data Model and StructureApr 16, 2025 am 12:09 AM

Redis's data model and structure include five main types: 1. String: used to store text or binary data, and supports atomic operations. 2. List: Ordered elements collection, suitable for queues and stacks. 3. Set: Unordered unique elements set, supporting set operation. 4. Ordered Set (SortedSet): A unique set of elements with scores, suitable for rankings. 5. Hash table (Hash): a collection of key-value pairs, suitable for storing objects.

Redis: Classifying Its Database ApproachRedis: Classifying Its Database ApproachApr 15, 2025 am 12:06 AM

Redis's database methods include in-memory databases and key-value storage. 1) Redis stores data in memory, and reads and writes fast. 2) It uses key-value pairs to store data, supports complex data structures such as lists, collections, hash tables and ordered collections, suitable for caches and NoSQL databases.

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.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools