What type of data does redis generally store?
Redis is an open source key-value storage database written in C language. It can be used in scenarios such as caching, event publishing and subscription, and high-speed queues. And supports rich data types: string (string), hash (hash), list (list), set (unordered set), zset (sorted set: ordered set)
Application scenarios of Redis in projects
1. Caching data
is the most commonly used, for queries that often need to be queried and changes are not very frequent. The data is often called hot data.
2. Message queue
is equivalent to a message subscription system, such as ActiveMQ and RocketMQ. If you have higher consistency requirements for data, it is still recommended to use MQ)
3, counter
, such as counting click-through rates and likes rate, redis is atomic and can avoid concurrency problems
4. E-commerce website information
Cache of initialization page data of large e-commerce platforms . For example, when purchasing a flight ticket on Qunar.com, the price on the home page will be different from the price you click on.
5. Hotspot data
#For example, real-time hot spots on news websites, hot searches on Weibo, etc. need to be updated frequently. When the total amount of data is relatively large, querying directly from the database will affect performance
Application scenarios of Redis data types
As mentioned earlier, Redis supports five There are a variety of rich data types, so how should we choose in different scenarios?
1. String
String is the most commonly used data type. It can store any type of string, including binary, JSONized objects, even base64 encoded images. The maximum capacity of a string in Redis is 512MB, which can be said to be omnipotent.
2. Hash
is often used to store structured data. For example, it can be used to store user IDs, nicknames, and avatars in forum systems. Points and other information. If you need to modify the information, you only need to take out the Value through the Key, deserialize it, modify the value of an item, and then serialize and store it in Redis. The Hash structure is stored because the Hash structure will be modified when a single Hash element is less than a certain number. Compressed storage, so you can save a lot of memory. This does not exist in the String structure.
3、List
List is implemented as a two-way linked list, which can support reverse search and traversal, making it more convenient to operate. However, it brings some additional memory overhead. Many implementations within Redis, including sending buffer queues, etc. This data structure is used. In addition, you can use the lrange command to implement Redis-based paging function, which has excellent performance and good user experience.
4. Set
The external function provided by set is similar to the function of list. The special thing is that set can automatically arrange Seriously, when you need to store a list of data and don't want duplicate data to appear, you can choose to use set at this time.
5. Sort Set
can be sorted according to the weight of a certain condition. For example, a ranking data application can be made based on the number of clicks. .
Redis features:
1. Redis is a pure memory operation, and we need to manually persist it to the hard disk when needed
2. Redis is single-threaded, thus avoiding the frequent context switching operations in multi-threads.
3. Redis data structure is simple, and the operation of data is also relatively simple
4. The underlying models used are different, and the underlying implementation between them is The method and application protocol for communication with the client are different. Redis directly builds its own VM mechanism, because if the general system calls system functions, it will waste a certain amount of time to move and request
5. Use multi-channel I/O multiplexing model, non-blocking I/O
For more Redis related knowledge, please visit the Redis usage tutorial column!
The above is the detailed content of What type of data does redis generally store?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Redis cluster mode deploys Redis instances to multiple servers through sharding, improving scalability and availability. The construction steps are as follows: Create odd Redis instances with different ports; Create 3 sentinel instances, monitor Redis instances and failover; configure sentinel configuration files, add monitoring Redis instance information and failover settings; configure Redis instance configuration files, enable cluster mode and specify the cluster information file path; create nodes.conf file, containing information of each Redis instance; start the cluster, execute the create command to create a cluster and specify the number of replicas; log in to the cluster to execute the CLUSTER INFO command to verify the cluster status; make

How to clear Redis data: Use the FLUSHALL command to clear all key values. Use the FLUSHDB command to clear the key value of the currently selected database. Use SELECT to switch databases, and then use FLUSHDB to clear multiple databases. Use the DEL command to delete a specific key. Use the redis-cli tool to clear the data.

To read a queue from Redis, you need to get the queue name, read the elements using the LPOP command, and process the empty queue. The specific steps are as follows: Get the queue name: name it with the prefix of "queue:" such as "queue:my-queue". Use the LPOP command: Eject the element from the head of the queue and return its value, such as LPOP queue:my-queue. Processing empty queues: If the queue is empty, LPOP returns nil, and you can check whether the queue exists before reading the element.

Use the Redis command line tool (redis-cli) to manage and operate Redis through the following steps: Connect to the server, specify the address and port. Send commands to the server using the command name and parameters. Use the HELP command to view help information for a specific command. Use the QUIT command to exit the command line tool.

Redis memory soaring includes: too large data volume, improper data structure selection, configuration problems (such as maxmemory settings too small), and memory leaks. Solutions include: deletion of expired data, use compression technology, selecting appropriate structures, adjusting configuration parameters, checking for memory leaks in the code, and regularly monitoring memory usage.

Redis counter is a mechanism that uses Redis key-value pair storage to implement counting operations, including the following steps: creating counter keys, increasing counts, decreasing counts, resetting counts, and obtaining counts. The advantages of Redis counters include fast speed, high concurrency, durability and simplicity and ease of use. It can be used in scenarios such as user access counting, real-time metric tracking, game scores and rankings, and order processing counting.

Using the Redis directive requires the following steps: Open the Redis client. Enter the command (verb key value). Provides the required parameters (varies from instruction to instruction). Press Enter to execute the command. Redis returns a response indicating the result of the operation (usually OK or -ERR).

Redis, as a message middleware, supports production-consumption models, can persist messages and ensure reliable delivery. Using Redis as the message middleware enables low latency, reliable and scalable messaging.
