Home  >  Article  >  Database  >  Redis basic data types and operations (summary sharing)

Redis basic data types and operations (summary sharing)

WBOY
WBOYforward
2022-11-10 16:38:011835browse

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:juejin.im. If there is any infringement, please contact admin@php.cn delete