In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article is about how to use MySQL to simulate Redis. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Redis supports nearly ten data types, of which 5 are the most commonly used. String, hash, zset, set, list, etc. This paper will discuss the simulation implementation of common operations in view of several common data structures.
In fact, all we need to develop is a redis proxy proxy. The client of redis will parse the protocol after connecting to our agent. The parsed commands will be simulated and then located to the appropriate mysql according to the configured route.
The redis you use actually uses mysql to store data. No rdb, no aof.
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 newline symbol is added uniformly.
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: 9: 9: 6
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 varchar val varchar lastTime 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" = $1
Hash 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 varchar hkey varchar val varchar lastTime 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" = $1
Zset 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 varchar member varchar score double lastTime 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
Select to_date ('2005-12-25 Person13-25-5-5-5-5) from dual
© 2024 shulou.com SLNews company. All rights reserved.