search
HomeDatabaseRedisThrough commands and application scenarios, we will guide you to understand the five basic types in Redis.

This article will introduce you to the five basic types in Redis through commands and application scenarios. There are many commands and practices. I hope it will be helpful to everyone!

Through commands and application scenarios, we will guide you to understand the five basic types in Redis.

redis The application of traditional 5 big data types

redis The implementation of traditional 5 big data types

Redis introduction:

Redis is an open source (BSD licensed), in-memory data structure storage system that can be used as a database, cache, and messaging middleware. It supports many types of data structures such as strings, hashes, lists, sets, sorted sets] with range queries, bitmaps, hyperloglogs and geospatial (geospatial) Index radius query. Redis has built-in replication, LUA scripting, LRU eviction, transactions and different levels of disk persistence, and through Redis Sentinel and automatic partitioning (Cluster) ) provides high availability. [Related recommendations: Redis video tutorial]

redis command query: http://www.redis.cn/commands.html

Note: redis commands are not size sensitive Write, and key is case-sensitive

Query command help:

help @type noun

Example:

Through commands and application scenarios, we will guide you to understand the five basic types in Redis.

string String type

Most commonly used

## set key vuue

getkey

Set/get multiple key values ​​at the same time

MSET key value [key value .. .]

MGET key [key ,,,]

Increase and decrease the value

increment the number incr key

Increase the specified integer incrby key increment

Decrease the numerical decr key

Decrease the specified integer decrby key decrement

Get the character length

STRLEN key

Distributed lock

setnx key value

set key value [EX seconds] [PX milliseconds] [NX|XX]

Through commands and application scenarios, we will guide you to understand the five basic types in Redis.

    EX: how many seconds does the key expire after?
  • PX: key since Expiration after how many milliseconds
  • NX: When the key does not exist, the key is created. The effect is equivalent to setnx
  • XX: When the key exists, overwrite the key

Through commands and application scenarios, we will guide you to understand the five basic types in Redis.

Application scenarios

    The product number and order number are generated using the INCR command
  • Yes Like the article
Reading count: As long as you click on the rest address, directly use the incr key command to add a number 1 to complete the recording of the number.

Through commands and application scenarios, we will guide you to understand the five basic types in Redis.

Command practice

Through commands and application scenarios, we will guide you to understand the five basic types in Redis.

hash Hash type

and Java data structure mapping

Map>

##Set one field value at a time HSET key field value

Get one field value at a time

HGET key field

Set multiple field values ​​at one time

HMSET key field value [fild value ...]

Get multiple field values ​​at one time

HMGET key field [field ...]

Get all field values

hgetall key

Get all the quantities in a key

hlen

Delete a key

hdel

Command demonstration

Through commands and application scenarios, we will guide you to understand the five basic types in Redis.

Application scenariosIn the early days of the shopping cart, currently small and medium-sized factories can use

to add new products --> hset shopcar:uid1024 334488 1

Add new product--> hset shopcar:uid2014 334477 1

Add product quantity--> hincrby shopcar:uid1024 334477 1

product Total --> hlen shopcar:uid1024

Select all --> hgetall shopcar:uid1024

Through commands and application scenarios, we will guide you to understand the five basic types in Redis.

list List type

Add elements to the left of the list

lpush key value [value ...]

Add elements to the right of the list

rpush key value [value ...]

View the list

lrange key start stop

Get the elements in the list Number

llen key

Command usage

Through commands and application scenarios, we will guide you to understand the five basic types in Redis.

Application scenario

WeChat article subscription public account

1, [xx treasure] and [xx newspaper] published articles 11 and 22## respectively

#2. The author follows both of them. As long as they publish a new article, it will be pushed to my list

lpush likearticle: uid1024 11 22

3. View the author All articles of your own subscription account are similar to paging. The following 0-10 is to display 10 items at a time lrange likearticle:uid1024 0 10

Through commands and application scenarios, we will guide you to understand the five basic types in Redis.

set Non-duplicate list type

Add element

sadd key member [member ...]

Delete element

srem key member [member ...]

Get all elements of the collection

smembers key

Judge whether the element is in the collection

sismember key member

Get the number of elements in the collection

scard key

From the collection Randomly pop up an element from the collection, the element will not be deleted

srandmember key [number]

Randomly pop up an element from the collection, delete one element

spop key [Number]

Set operation

  • Difference operation of sets A - B

A set constructed from elements that belong to A but not B

sdiff key [key ...]

  • The intersection of the sets is calculated as A ^ B

Elements that belong to A and also belong to B.

simter key [key ...]

  • Union of sets Operation A v B

The merged set of elements belonging to A or B

sunion key [key ...]

Application Scenario

WeChat Lottery Mini Program

Through commands and application scenarios, we will guide you to understand the five basic types in Redis.

1. User id, participate immediately, sadd key user id

2. Display how many people have participated. There are currently 67231 people participating. scard key

3. Lottery (arbitrarily select N winners from the set)

srandmember key 2 random If you draw 2 people, the elements will not be deleted

spop key 3 Drivers draw 3 people, the elements will be deleted

Likes in WeChat Moments

1Through commands and application scenarios, we will guide you to understand the five basic types in Redis.

1. Add a like; sadd pub:msgid like user id1 like user id2

2. Cancel like; srem pub:msgid like user id

3. Show all points Users who have liked smembers pub:msgid

4. Statistics of the number of users who like it, which is the common red number waiting for likes. scard pub:msgid

5. Determine whether a friend is right The author liked it, sismember pub:msgid user id

Weibo friends follow social relationships

1Through commands and application scenarios, we will guide you to understand the five basic types in Redis.

People who follow together

sadd s1 1 2 3 4

sadd s2 2 3 6 8

sinter s1 s2

Common attention: I go to someone’s Weibo and immediately get the information that is shared with someone People

The people I follow also follow him (we all have the same hobbies)

I follow Yu Chengdong of Huawei, Yu Chengdong also follows Zhang Zhaodong, Mr. Yu and I have the same hobbies

sadd s1 1 2 3 4 5

sadd s2 3 4 5 6 7

sismember s1 3

sismember s2 3

QQ Recommend people you may know

sadd s1 1 2 3 4 5

sadd s2 3 4 5 6 7

// Common friends

sinter s1 s2

// Difference set

sdiff s1 s2

sdiff s2 s1

zset ordered set

Common commands

1. Add an element and the score of the element to the ordered set

2. Add element

    ZADD key score member [score member ...]
3. Return all elements with index from strat to stop in the order of arrival of element scores

    zrange key start stop [WITHSORES]
4. Get the score of the element

    zscore key member [member ...]
5. Delete Element

    zrem key member [member ...]
6. Get the elements of the specified score range

    zrangebyscore key min max [ WITHSCORES] [LIMIT offset count]
7. Increase the score of an element

    zincrby key increment member
8. Get the set The number of elements in

    zcard key
9. Obtain the number of elements within the specified score range

    zcount key min max
10. Delete elements according to ranking range

  • zremrangebyrank key start stop

11. Get the ranking of elements

  • zrank key member

  • Zrevrank key member from large to small

Application scenarios

1. More product sales Sorting and displaying products

Idea: Define the product sales ranking list (sorted set), the key is goods:sellsort, and the score is the product sales quantity.

The sales volume of product number 1001 is 9, and the sales volume of product number 1002 is 15 | zadd goods:sellsort 9 1001 15 1002
A customer bought 2 more items Product 1001, the product number is 1001 loudly increase 2 | zincrby goods:sellsort 2 10001
Find the top 10 products by sales zrange goods:sellsort 0 10 withscores
1Through commands and application scenarios, we will guide you to understand the five basic types in Redis.

2. Douyin hot search

1Through commands and application scenarios, we will guide you to understand the five basic types in Redis.

1. Click on the video

ZINCRBY hotavi:20220203 1 八百

ZINCRBY hotavi :20220203 15 Eight Hundred 2 Hua Mulan

2. Display the top 10 items on the day

zrevrange hotavi:20220203 0 9 withscores

For more programming-related knowledge, please visit: Introduction to Programming! !

The above is the detailed content of Through commands and application scenarios, we will guide you to understand the five basic types in Redis.. 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
es和redis区别es和redis区别Jul 06, 2019 pm 01:45 PM

Redis是现在最热门的key-value数据库,Redis的最大特点是key-value存储所带来的简单和高性能;相较于MongoDB和Redis,晚一年发布的ES可能知名度要低一些,ES的特点是搜索,ES是围绕搜索设计的。

一起来聊聊Redis有什么优势和特点一起来聊聊Redis有什么优势和特点May 16, 2022 pm 06:04 PM

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了关于redis的一些优势和特点,Redis 是一个开源的使用ANSI C语言编写、遵守 BSD 协议、支持网络、可基于内存、分布式存储数据库,下面一起来看一下,希望对大家有帮助。

实例详解Redis Cluster集群收缩主从节点实例详解Redis Cluster集群收缩主从节点Apr 21, 2022 pm 06:23 PM

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了Redis Cluster集群收缩主从节点的相关问题,包括了Cluster集群收缩概念、将6390主节点从集群中收缩、验证数据迁移过程是否导致数据异常等,希望对大家有帮助。

Redis实现排行榜及相同积分按时间排序功能的实现Redis实现排行榜及相同积分按时间排序功能的实现Aug 22, 2022 pm 05:51 PM

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了Redis实现排行榜及相同积分按时间排序,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,希望对大家有帮助。

详细解析Redis中命令的原子性详细解析Redis中命令的原子性Jun 01, 2022 am 11:58 AM

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了关于原子操作中命令原子性的相关问题,包括了处理并发的方案、编程模型、多IO线程以及单命令的相关内容,下面一起看一下,希望对大家有帮助。

实例详解Redis实现排行榜及相同积分按时间排序功能的实现实例详解Redis实现排行榜及相同积分按时间排序功能的实现Aug 26, 2022 pm 02:09 PM

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了Redis实现排行榜及相同积分按时间排序,本文通过实例代码给大家介绍的非常详细,下面一起来看一下,希望对大家有帮助。

一文搞懂redis的bitmap一文搞懂redis的bitmapApr 27, 2022 pm 07:48 PM

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了bitmap问题,Redis 为我们提供了位图这一数据结构,位图数据结构其实并不是一个全新的玩意,我们可以简单的认为就是个数组,只是里面的内容只能为0或1而已,希望对大家有帮助。

redis error什么意思redis error什么意思Jun 17, 2019 am 11:07 AM

redis error就是redis数据库和其组合使用的部件出现错误,这个出现的错误有很多种,例如Redis被配置为保存数据库快照,但它不能持久化到硬盘,用来修改集合数据的命令不能用。

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 Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

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.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),