当前位置:网站首页>Redis -- three special data types
Redis -- three special data types
2022-07-02 20:40:00 【Wholehearted classmates】
hello ! Hello everyone , I am a 【 One heart classmate 】, A highly motivated 【Java Domain Blogger 】!
【 One heart classmate 】 Of Writing style : Like to use 【 Easy to understand 】 To explain every point of knowledge with your writing , Instead of using 【 On the tall 】 Official statement of .
【 One heart classmate 】 Blog field yes 【 Back end oriented technology 】 Learning from , The future will continue to update more 【 back-end technology 】 as well as 【 The learning 】.
If there is a pair of 【 back-end technology 】 Interested in 【 Cutie 】, Welcome to your attention 【 One heart classmate 】
️️️ Thank you, big and small !️️️
Catalog
One 、HyperLogLog Base Statistics
1.2 Benefits of using cardinality statistics
One 、HyperLogLog Base Statistics
1.1 What is the cardinality ?
We can understand what is... Directly through an example Base Statistics , Like data sets {1, 2, 3, 3, 5, 5,}, So the cardinality set of this dataset is {1,2,3,5}, base ( Don't repeat elements ) by 4. That is to say The number of non repeating elements .
1.2 Benefits of using cardinality statistics
Every HyperLogLog Keys only cost 12 KB Memory , So we can calculate the proximity 2^64 Cardinality of different elements . This is the same as calculating the cardinality , The more elements consume memory, the more collections there are . If you want to from Memory angle To compare Hyperloglog First choice .
1.3 Application scenarios
Web page uv( A person visits a website many times , But still be a person )
- The traditional way :set( because set No repetition , If it repeats, it will overwrite ) Save the user's id, And then you can count ,set The number of elements in is used as the standard judgment , This way, if you save a large number of users id, It will be more troublesome and occupy a lot of memory in large websites . Here's what we're trying to do To count , Instead of saving users id.
- Use HyperLogLog: One HyperLogLog The key only needs to be 12KB, The number that can be calculated is very huge , The occupied memory space is greatly reduced .
1.4 matters needing attention
If Allow fault tolerance (0.81% Error rate , Counting can be ignored ), Then it must be possible to use Hyperloglog! If fault tolerance is not allowed , Just use set Or your own data type !
1.5 Basic commands
Serial number | Command and description |
---|---|
1 | PFADD key element [element ...] Add specified elements to HyperLogLog in . |
2 | PFCOUNT key [key ...] Return to a given HyperLogLog The base estimate of . |
3 | PFMERGE destkey sourcekey [sourcekey ...] Will be multiple HyperLogLog Merge into one HyperLogLog |
1.6 Use
127.0.0.1:6379> pfadd mykey1 a b c d e f # Add data to the first group
(integer) 1
127.0.0.1:6379> pfcount mykey1 # Statistics mykey1 Base number of
(integer) 6
127.0.0.1:6379> pfadd mykey2 e e f j # Add data to the second group
(integer) 1
127.0.0.1:6379> pfcount mykey2 # Statistics mykey2 Base number of
(integer) 3
127.0.0.1:6379> pfmerge mykey3 mykey1 mykey2 # Combine the two groups mykey1 mykey2 => mykey3 Combine
OK
127.0.0.1:6379> pfcount mykey3 # Statistics mykey3 Base number of
(integer) 7
Two 、Geospatial Location
2.1 Introduce
Redis3.2 The release of the Geospatial, It can be calculated Geographic information , The distance between the two places , People in a few miles around .
2.2 Use scenarios
Friend orientation
Check out the people around
Taxi distance calculation
2.3 Basic commands
Serial number | Command and description |
---|---|
1 | GEOADD key longitude latitude Location name Geospatial location to be specified ( latitude 、 longitude 、 name ) Add to specified key in |
2 | GEOPOS key Location name from key Returns the location of all the positioning elements ( Longitude and latitude ). |
3 | GEODIST key place 1 place 2 Company Returns the distance between two given positions , If one of the two positions does not exist , Then the command returns a null value . |
4 | GEORADIUS key longitude latitude Range value Company Centered on a given latitude and longitude , Find the elements in a certain radius |
5 | GEORADIUSBYMEMBER key place Distance value Company Find the elements in the specified range , The center point is determined by a given location element |
6 | GEOHASH key place 1 place 2 Will return 11 A character Geohash character string , If the two strings are closer , So the closer we get . |
7 | zrange key start stop Get assigned key Coordinate information in |
8 | zrem key place Delete the specified key Specify the data of the target |
Inquire the longitude and latitude of the place :
City longitude and latitude query - Online query tool for longitude and latitude of domestic cities
2.4 Explain in detail
2.4.1 GEOADD
effect : Add location
The rules : Two levels cannot be added directly , We usually download city data , Directly through java The program is imported once !
grammar :GEOADD key longitude latitude Location name
matters needing attention
Effective longitude from -180 C to 180 degree .
Effective latitude from -85.05112878 C to 85.05112878 degree .
When the coordinate position exceeds the specified range above , The command will return an error .
Use
# Add a single message
127.0.0.1:6379> geoadd address 116.708463 23.37102 shantou
(integer) 1
# Add more information
127.0.0.1:6379> geoadd address 116.405285 39.904989 beijin 121.472644 31.231706 shanghai
(integer) 2
2.4.2 GEOPOS
effect : Get the location information of the designated place ( Longitude and latitude )
grammar :GEOPOS key Location name
Use
127.0.0.1:6379> geopos address beijin # Get the geographical location of Beijing
1) 1) "116.40528291463851929" # longitude
2) "39.9049884229125027" # latitude
2.4.3 GEODIST
effect : Returns the distance between two given positions , If one of the two positions does not exist , Then the command returns a null value .
grammar :GEODIST key place 1 place 2 Company
Unit parameter :
m Expressed in meters .
km Expressed in kilometers .
mi In miles .
ft In feet .
If the user does not explicitly specify the unit parameter , that GEODIST The default is meters As a unit .
Use :
127.0.0.1:6379> geodist address beijin shanghai km # Check the distance between Beijing and Shanghai
"1067.5980"
2.4.4 GEORADIUS
effect : Centered on a given latitude and longitude , Find the elements in a certain radius .
grammar :GEORADIUS key longitude latitude Range value Company
Use :
# Looking to 116,39 This longitude and latitude are the center , Looking for a square circle 1500km In the city
127.0.0.1:6379> georadius address 116 39 1500 km
1) "shanghai"
2) "beijin"
# Show the position to the middle distance
127.0.0.1:6379> georadius address 116 39 1500 km withdist
1) 1) "shanghai"
2) "996.7313"
2) 1) "beijin"
2) "106.5063"
# Show other people's location information
127.0.0.1:6379> georadius address 116 39 1500 km withcoord
1) 1) "shanghai"
2) 1) "121.47264629602432251"
2) "31.23170490709807012"
2) 1) "beijin"
2) 1) "116.40528291463851929"
2) "39.9049884229125027"
# Filter out the nearest city and show its distance
127.0.0.1:6379> georadius address 116 39 1500 km withdist withcoord count 1
1) 1) "beijin"
2) "106.5063"
3) 1) "116.40528291463851929"
2) "39.9049884229125027"
# Filter the nearest two cities and show their distance
127.0.0.1:6379> georadius address 116 39 1500 km withdist withcoord count 2
1) 1) "beijin"
2) "106.5063"
3) 1) "116.40528291463851929"
2) "39.9049884229125027"
2) 1) "shanghai"
2) "996.7313"
3) 1) "121.47264629602432251"
2) "31.23170490709807012"
2.4.5 GEORADIUSBYMEMBER
effect : Find the elements in the specified range , The center point is determined by a given location element .
grammar :GEORADIUSBYMEMBER key place Distance value Company
Use :
# Find out the distance from Beijing 1500km The city inside
127.0.0.1:6379> georadiusbymember address beijin 1500 km
1) "shanghai"
2) "beijin"
2.4.6 GEOHASH
effect : Will return 11 A character Geohash character string , If the two strings are closer , So the closer we get .
grammar :GEOHASH key place 1 place 2
Use :
127.0.0.1:6379> geohash address beijin shantou
1) "wx4g0b7xrt0"
2) "ws4uzy8d030"
2.4.7 ZRANGE
effect : Get assigned key Coordinate information in .
grammar :zrange key start stop
Use :
127.0.0.1:6379> zrange address 0 -1
1) "shantou"
2) "shanghai"
3) "beijin"
2.4.8 ZREM
effect : Delete the specified key Specify the data of the target .
grammar :zrem key place
Use :
127.0.0.1:6379> zrem address shanghai
(integer) 1
3、 ... and 、BitMap
Introduce
BitMap It's through One bit position To represent the corresponding value or state of an element , Only 0 and 1 Two states , Among them key It's the corresponding element itself .365 God = 365 bit ,1 byte = 8bit , That is to say, it only needs 46 In bytes or so , So it can Save a lot of space .
Application scenarios
(1) User check-in
(2) Count active users
(3) User online status ( Online is set to 1, If you are not online, set it to 0)
Use
demand : Record Clock in from Monday to Sunday
1: Indicates that there is a clock out
0: It means you didn't punch in
127.0.0.1:6379> setbit sign 0 1
(integer) 0
127.0.0.1:6379> setbit sign 1 1
(integer) 0
127.0.0.1:6379> setbit sign 2 0
(integer) 0
127.0.0.1:6379> setbit sign 3 1
(integer) 0
127.0.0.1:6379> setbit sign 4 1
(integer) 0
127.0.0.1:6379> setbit sign 5 0
(integer) 0
127.0.0.1:6379> setbit sign 6 0
(integer) 0
Check to see if there is a clock out on a certain day
127.0.0.1:6379> getbit sign 3
(integer) 1
127.0.0.1:6379> getbit sign 6
(integer) 0
Count the clocking records of this week
127.0.0.1:6379> bitcount sign
(integer) 4
Summary
That's all 【 One heart classmate 】 Organized 【Redis】 Medium 【 Three special data types 】, Maybe we seldom use , But if we can use it , Then its effect on us is very big , You can give us 【 Save huge space 】 as well as 【 Bring extremely fast speed 】.
If this article 【 article 】 It helps you , I hope I can give 【 One heart classmate 】 Order one Fabulous , It's not easy to create , Compared with the official statement , I prefer to use 【 Easy to understand 】 To explain every point of knowledge with your writing , If there is a pair of 【 back-end technology 】 Interested little cute , You are welcome to pay attention to ️️️ 【 One heart classmate 】️️️, I will bring you great 【 Harvest and surprise 】!
边栏推荐
- 【QT】QPushButton创建
- In depth understanding of modern web browsers (I)
- After 65 days of closure and control of the epidemic, my home office experience sharing | community essay solicitation
- Is it safe to buy funds on securities accounts? Where can I buy funds
- Research Report on the overall scale, major manufacturers, major regions, products and applications of metal oxide arresters in the global market in 2022
- I would like to ask what securities dealers recommend? Is it safe to open a mobile account?
- Research Report on the overall scale, major manufacturers, major regions, products and applications of hollow porcelain insulators in the global market in 2022
- Exemple complet d'enregistrement du modèle pytoch + enregistrement du modèle pytoch seuls les paramètres d'entraînement sont - ils enregistrés? Oui (+ Solution)
- Add two numbers of leetcode
- Activation function - relu vs sigmoid
猜你喜欢
Activation function - relu vs sigmoid
JASMINER X4 1U deep disassembly reveals the secret behind high efficiency and power saving
pytorch 模型保存的完整例子+pytorch 模型保存只保存可训练参数吗?是(+解决方案)
ROS learning (10): ROS records multiple topic scripts
面试经验总结,为你的offer保驾护航,满满的知识点
Talk about macromolecule coding theory and Lao Wang's fallacy from the perspective of evolution theory
API文档工具knife4j使用详解
GCC: Graph Contrastive Coding for Graph Neural NetworkPre-Training
Solution to blue screen after installing TIA botu V17 in notebook
c语言链表--待补充
随机推荐
SBT tutorial
【Hot100】22. 括号生成
Postman interface test practice, these five questions you must know
Don't you want to have a face-to-face communication with cloud native and open source experts? (including benefits
API文档工具knife4j使用详解
at编译环境搭建-win
Research Report on the overall scale, major manufacturers, major regions, products and application segmentation of power management units in the global market in 2022
Research Report on the overall scale, major manufacturers, major regions, products and applications of friction dampers in the global market in 2022
台湾SSS鑫创SSS1700替代Cmedia CM6533 24bit 96KHZ USB音频编解码芯片
Esp32c3 crash analysis
Research Report on the overall scale, major manufacturers, major regions, products and applications of building automation power meters in the global market in 2022
ROS learning (10): ROS records multiple topic scripts
B-end e-commerce - reverse order process
Research Report on the overall scale, major manufacturers, major regions, products and applications of swivel chair gas springs in the global market in 2022
为什么我对流程情有独钟?
GCC: Graph Contrastive Coding for Graph Neural NetworkPre-Training
2021 v+ Quanzhen internet global innovation and Entrepreneurship Challenge, one of the top ten audio and video scene innovation and application pioneers
Function, function, efficiency, function, utility, efficacy
CRM Customer Relationship Management System
Properties of expectation and variance