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.
Recommended learning: Redis video tutorial
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:
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.
Companies using Redis
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. .
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) 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 number "10086"
127.0.0.1:6379> SET number "10086"OK127.0.0.1:6379> GET number"10086"127.0.0.1:6379>
STRLEN email
SETEX city 5 Beijing
Flash sale activity PSETEX setting milliseconds
MSET username jack sex male age 24
MGET username sex age
INCR number
127.0.0.1:6379> INCR number(integer) 10087127.0.0.1:6379>
这些命令,大家都可以自己在。 Redis 客户端进行测试。
用来保存更复杂的结构化数据
HGET 8000 ename
HMGET 80000 ename job deptno
HINCRBY 8000 deptono 10
当我们需要向 VALUE 保存序列化的数据,可以使用列表类型
RPUSH dname 技术部 后勤部 售后部 LPUSH dname 秘书处 LSET dname 2 销售部 LRANGE dname 0 -1
lindex dname 0
linsert dname before 秘书处 董事会
LPOP dname
RPOP dname
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
SCARD empno
SISMENBER empno 8000
SPOP empno
SRANDMEMBER empno 5
带有排序功能的集合,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
ZCOUNT keyword 5 10
ZRANGE keyword 0 -1
ZREVRANGE keyword 0 -1
zrangebyscore keyword 5 10 # 5-10 zrangebyscore keyword 5 (10 # 大于等于5, 小于 10 zrangebyscore keyword 100000 +inf
zrevrangebyscore keyword 10 5
zrank keyword "xx"
ZREM keyword "x" "y"
zremrangebyrank keyword 0 2
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!