In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to use mysql to simulate redis". In daily operation, I believe many people have doubts about how to use mysql to simulate redis. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use mysql to simulate redis". Next, please follow the editor to study!
Redis is a text protocol
Redis is a text protocol and the name of the protocol is RESP. RESP is short for Redis serialization protocol. It is an intuitive text protocol, which has the advantages of simple implementation and excellent parsing performance.
As shown in the figure, the structural data transmitted by the Redis protocol can be summarized into five minimum unit types. At the end of each unit, enter the newline symbol\ r\ n.
Here are a few rules:
A single-line string begins with +; a multiline string begins with $, followed by the length of the string; the integer value begins with:, followed by the string of integers; the error message begins with the-sign; the array begins with the * sign, followed by the length of the array.
For example, the following is the message of the array [9 ~ 9 ~ 9 ~ 6].
* 3\ r\ nVR9\ r\ nVO9\ r\ nVO6\ r\ n
So the analysis and assembly of this protocol is very simple. In the case of netty, there is a codec-redis module for us to use.
Implementation: data structure design
In the design of the data table, we find that there is no difference in efficiency between kv and hash, because it can be located directly according to key.
On the contrary, it is zset, because of the sorting function, the execution efficiency of many operations is not satisfactory.
In addition, because of our different data structures, we use different tables for storage. So the delete operation should be performed once on each table.
Kv design
Kv, or string, is the most basic data type in redis. A key corresponds to a value of type value,string that can store the maximum 512MB.
Design a dedicated database table rstore_kv, where rkey is the primary key.
Rkey varcharval varcharlastTime bigint
Set operation
Insert into rstore_kv ("rkey", "val", "lastTime") values ($1 and 2) on duplicate key update set "val" = $2, "lastTime" = $3
Get operation
Select val from rstore_kv where "rkey" = $1
Del operation
Delete from rstore_kv where "rkey" = $1
Exists operation
Select count (*) as n from rstore_kv where "rkey" = $1
Ttl operation
Select lastTIme from rstore_kv where "rkey" = $1hash design
Hash is a collection of key-value pairs (key= > value). Hash is particularly suitable for storing objects.
Design a dedicated database table rstore_hash, where rkey and hkey are federated primary keys.
Rkey varcharhkey varcharval varcharlastTime bigint
Hset operation
Insert into rstore_hash ("rkey", "hkey", "val", "lastTime") values ($1, on duplicate key update set "val" = $3, "lastTime" = $4)
Hget operation
Select val from rstore_hash where "rkey" = $1 and "hkey" = $2
Hgetall operation
Select hkey,val from rstore_hash where "rkey" = $1
Hdel operation
Delete from rstore_hash where "rkey" = $1 and "hkey" = $2
Del operation
Delete from rstore_hash where "rkey" = $1
Hlen,hexists operation
Select count (*) as num from rstore_hash where "rkey" = $1
Ttl operation
Select max (lastTIme) from rstore_hash where "rkey" = $1zset design
Redis zset, like set, is a collection of elements of type string, and duplicate members are not allowed. The difference is that each element is associated with a score of type double. Redis sorts the members of the collection from small to large by scores. Its underlying structure is a jump table, which is very efficient, but takes up a lot of memory.
Design a dedicated database table rstore_zset, where rkey and member are federated primary keys.
Rkey varcharmember varcharscore doublelastTime bigint
Zadd operation
Insert into rstore_zset ("rkey", "member", "score", "lastTime") values ($1, on duplicate key update update set "score" = $3, "lastTime" = $4)
Zscore operation
Select score from rstore_zset where "rkey" = $1 and "member" = $2
Zrem operation
Delete from rstore_zset where "rkey" = $1 and "member" = $2 "
Zcard,exists operation
Select count (*) as num from rstore_zset where "rkey" = $1
Zcount operation
Select count (*) as num from rstore_zset where "rkey" = $1 and score > = $2 and score=$2 and score=$2 and score
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.