Redis是文本协议
redis
是文本协议,协议名称叫做RESP
。RESP
是 Redis
序列化协议的简写。它是一种直观的文本协议,优势在于实现异常简单,解析性能极好。
如图,Redis
协议将传输的结构数据,可以总结为 5 种最小单元类型。每个单元结束时,统一加上回车换行符号 \r\n 。
下面是几个规则:
单行字符串 以 + 开头; 多行字符串 以 $ 开头,后跟字符串长度; 整数值 以 : 开头,后跟整数的字符串形式; 错误消息 以 - 符号开头; 数组 以 * 号开头,后跟数组的长度;
比如,下面这个就是数组[9,9,6]的报文。
*3\r\n:9\r\n:9\r\n:6\r\n
所以这个协议的解析和拼装,是非常简单的。拿netty
来说,就有codec-redis 模块供我们使用。
实现:数据结构设计
在数据表的设计上,我们发现,kv
和hash
在效率上没有什么差别,因为它能够直接根据key
定位到。
反倒是zset
,由于有排序的功能,造成了很多操作的执行效率都不尽人意。
另外,由于我们不同的数据结构,是使用不同的表进行存储的。所以删除操作,要在每张表上都执行一遍。
kv设计
kv
,即string
,是redis
里最基本的数据类型。一个key
对应一个value
,string
类型的值最大能存储512MB。
设计专用的数据库表rstore_kv
,其中,rkey
是主键。
rkey varchar val varchar lastTime bigint
set操作
insert into rstore_kv("rkey","val","lastTime") values($1,$2,$3) on duplicate key update set "val"=$2,"lastTime"=$3
get操作
select val from rstore_kv where "rkey" = $1
del操作
delete from rstore_kv where "rkey" = $1
exists操作
select count(*) as n from rstore_kv where "rkey" = $1
ttl操作
select lastTIme from rstore_kv where "rkey" = $1
hash设计
hash
是一个键值(key=>value)对集合。hash
特别适合用于存储对象。
设计专用的数据库表rstore_hash
,其中,rkey
和hkey
是联合主键。
rkey varchar hkey varchar val varchar lastTime bigint
hset操作
insert into rstore_hash("rkey","hkey","val","lastTime") values($1,$2,$3,$4) on duplicate key update set "val"=$3,"lastTime"=$4
hget操作
select val from rstore_hash where "rkey" = $1 and "hkey" = $2
hgetall操作
select hkey,val from rstore_hash where "rkey" = $1
hdel操作
delete from rstore_hash where "rkey" = $1 and "hkey" = $2
del操作
delete from rstore_hash where "rkey" = $1
hlen,hexists操作
select count(*) as num from rstore_hash where "rkey" = $1
ttl操作
select max(lastTIme) from rstore_hash where "rkey" = $1
zset设计
Redis zset
和 set
一样也是string
类型元素的集合,且不允许重复的成员。不同的是每个元素都会关联一个double
类型的分数。redis
正是通过分数来为集合中的成员进行从小到大的排序。它的底层结构是跳跃表,效率特别高,但是会占用大量内存。
设计专用的数据库表rstore_zset
,其中,rkey
和member
是联合主键。
rkey varchar member varchar score double lastTime bigint
zadd操作
insert into rstore_zset("rkey","member","score","lastTime") values($1,$2,$3,$4) on duplicate key update update set "score"=$3,"lastTime"=$4
zscore操作
select score from rstore_zset where "rkey" = $1 and "member" = $2
zrem操作
delete from rstore_zset where "rkey" = $1 and "member" = $2"
zcard,exists操作
select count(*) as num from rstore_zset where "rkey" = $1
zcount操作
select count(*) as num from rstore_zset where "rkey" = $1 and score>=$2 and scorezremrangebyscore操作
delete from rstore_zset where "rkey" = $1 and score>=$2 and scorezrangebyscore操作
select member,score from rstore_zset where "rkey" = $1 and score>=$2 and scorezrange操作
select member,score from rstore_zset where "rkey" = $1 order by score asc offset $2 limit $3zrank操作
select rank from (select member,rank() over (order by "score" asc, "lastTime" asc) as rank from rstore_zset where "rkey" = $1 ) m where m."member"= $2;ttl操作
select max(lastTIme) from rstore_zset where "rkey" = $1del操作
delete from rstore_zset where "rkey" = $1set设计
Redis
的Set
是string
类型的无序集合。设计专用的数据库表
rstore_set
,其中,rkey
和member
是联合主键。rkey varchar member varchar lastTime bigintsadd操作
insert into rstore_set("rkey","member","lastTime") values($1,$2,$3) on duplicate key update update set "lastTime"=$3scard操作
select count(*) as num from rstore_set where "rkey" = $1sismember操作
select member from rstore_set where "rkey" = $1 and "member" = $2smembers操作
select member from rstore_set where "rkey" = $1srem操作
delete from rstore_set where "rkey" = $1 and "member" = $2del操作
delete from rstore_set where "rkey" = $1ttl操作
select max(lastTIme) from rstore_set where "rkey" = $1
The above is the detailed content of How to use mysql to simulate redis. For more information, please follow other related articles on the PHP Chinese website!

ACID attributes include atomicity, consistency, isolation and durability, and are the cornerstone of database design. 1. Atomicity ensures that the transaction is either completely successful or completely failed. 2. Consistency ensures that the database remains consistent before and after a transaction. 3. Isolation ensures that transactions do not interfere with each other. 4. Persistence ensures that data is permanently saved after transaction submission.

MySQL is not only a database management system (DBMS) but also closely related to programming languages. 1) As a DBMS, MySQL is used to store, organize and retrieve data, and optimizing indexes can improve query performance. 2) Combining SQL with programming languages, embedded in Python, using ORM tools such as SQLAlchemy can simplify operations. 3) Performance optimization includes indexing, querying, caching, library and table division and transaction management.

MySQL uses SQL commands to manage data. 1. Basic commands include SELECT, INSERT, UPDATE and DELETE. 2. Advanced usage involves JOIN, subquery and aggregate functions. 3. Common errors include syntax, logic and performance issues. 4. Optimization tips include using indexes, avoiding SELECT* and using LIMIT.

MySQL is an efficient relational database management system suitable for storing and managing data. Its advantages include high-performance queries, flexible transaction processing and rich data types. In practical applications, MySQL is often used in e-commerce platforms, social networks and content management systems, but attention should be paid to performance optimization, data security and scalability.

The relationship between SQL and MySQL is the relationship between standard languages and specific implementations. 1.SQL is a standard language used to manage and operate relational databases, allowing data addition, deletion, modification and query. 2.MySQL is a specific database management system that uses SQL as its operating language and provides efficient data storage and management.

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

Key metrics for EXPLAIN commands include type, key, rows, and Extra. 1) The type reflects the access type of the query. The higher the value, the higher the efficiency, such as const is better than ALL. 2) The key displays the index used, and NULL indicates no index. 3) rows estimates the number of scanned rows, affecting query performance. 4) Extra provides additional information, such as Usingfilesort prompts that it needs to be optimized.

Usingtemporary indicates that the need to create temporary tables in MySQL queries, which are commonly found in ORDERBY using DISTINCT, GROUPBY, or non-indexed columns. You can avoid the occurrence of indexes and rewrite queries and improve query performance. Specifically, when Usingtemporary appears in EXPLAIN output, it means that MySQL needs to create temporary tables to handle queries. This usually occurs when: 1) deduplication or grouping when using DISTINCT or GROUPBY; 2) sort when ORDERBY contains non-index columns; 3) use complex subquery or join operations. Optimization methods include: 1) ORDERBY and GROUPB


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

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),

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

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.