Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Shake function of making Wechat with Redis

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Redis provides geolocation information (GEO) functionality, with which it can complete nearby people, shake and other functions. First of all, introduce the relevant API of GEO.

GEO API

Add address location information

geoadd key longitude latitude member [longitude latitude member ...]

longitude: longitude

latitude: latitude

Member: Member

This command can add one or more members at a time

Some users, all in Hefei, now store their geographical coordinates in Redis.

Little A is watching TV at home. His home coordinates are:117.230279,31.81676.

Little B works overtime in the company. The coordinates of the company are: 117.229704,31.824676

Little C is on a business trip. The coordinates of his business trip address are: 117.300419,31.696095.

Small D takes care of his baby at home. His home address coordinates are: 117.192909,31.732465.

Little E is still in school. His school address coordinates are: 117.189604, 31.838297.

127.0.0.1:6379> geoadd location 117.230279 31.81676 a 117.229704 31.824676 b(integer) 2127.0.0.1:6379> geoadd location 117.300419 31.696095 c(integer) 1127.0.0.1:6379> geoadd location 117.192909 31.732465 d(integer) 1127.0.0.1:6379> geoadd location 117.189604 31.838297 e(integer) 1

Get the distance between two locations

geodist key member1 member2 [unit]

Unit has four units.

'm' => meters

'km' => km

'mi' => miles

'ft' => ruler

We're going to use meters and kilometers.

Now let's look at the distance between little A and little B.

127.0.0.1:6379> GEODIST location a b km"0.8821"

You can see that there's 0.88 kilometers between little A and little B.

And look at the distance between little C and little E.

127.0.0.1:6379> GEODIST location c e km"18.9728"

There's a difference of almost 19 kilometers between them.

Get address location information

geopos key member [member ...]

Let's look at the latitude and longitude information of the address where Little D is located.

127.0.0.1:6379> geopos location d1) 1) "117.19290822744369507" 2) "31.73246441933707018"

Gets a collection of geographic information locations within a specified range of locations

georadius key longitude latitude radiusm km|ft|mi [withcoord] [withdist] [withhash] [COUNT count] [asc|desc] [store key] [storedist key] georadiusbymember key member radiusm km|ft|mi [withcoord] [withdist] [withhash] [COUNT count] [asc|desc] [store key] [storedist key]

These two commands are slightly more complex than the others. Let's look at these two commands together.

The two commands are basically similar in function, the main difference is that the first command gives a specific latitude and longitude, while the second command only gives the member name. For example, I want to know the distance between the members and Hefei Mount Shu, because the latitude and longitude information of Mount Shu has not been stored in redis, so we need to use the first command to input the latitude and longitude of Mount Shu. Another example is the distance between other members and the coordinates of small A, then you can use the second command to directly enter the member small A.

radiusm and the units that follow are required information that specifies the radius within which to search.

The coordinates of Hefei Dashu Mountain are 117.175571,31.846746

#See which members are 10km from Dashushan 127.0.0.1:6379> GEORADIUS location 117.175571 31.846746 10 km1) "e"2) "a"3) "b"

You can see that small e, small a and small b are relatively close to Dashushan, within 10km.

WITH COORD: Returns the longitude and latitude of the location element

127.0.0.1:6379> GEORADIUS location 117.175571 31.846746 10 km withcoord1) 1) "e" 2) 1) "117.18960374593734741" 2) "31.83829663190295634"2) 1) "a" 2) 1) "117.23027676343917847" 2) "31.81675910621205361"3) 1) "b" 2) 1) "117.22970277070999146" 2) "31.8246750403926697"

As you can see, in addition to giving members, the member location information page is also given.

withdist: Return the distance from the center node

127.0.0.1:6379> GEORADIUS location 117.175571 31.846746 10 km withcoord withdist1) 1) "e" 2) "1.6252" 3) 1) "117.18960374593734741" 2) "31.83829663190295634"2) 1) "a" 2) "6.1522" 3) 1) "117.23027676343917847" 2) "31.81675910621205361"3) 1) "b" 2) "5.6737" 3) 1) "117.22970277070999146" 2) "31.8246750403926697"

It can be seen that small E is 1.62 kilometers away from Dashu Mountain, small A is 6.15 kilometers away from Dashu Mountain, and small B is 5.67 kilometers away from Dashu Mountain.

withhash: This command can be ignored, basically not used

COUNT count: Specifies the number of results returned.

asc| desc: Return results in ascending or descending order according to distance from the center node.

storedist key: stores the distance from the returned result to the center node to the specified key.

#Get members within 100km of Dashushan, in ascending order of distance, just give the nearest 4 members 127.0.0.1: 6379> GEORADIUS location 117.175571 31.846746 100 km withdist count 4 asc1) 1) "e" 2) "1.6252"2) 1) "b" 2) "5.6737"3) 1) "a" 2) "6.1522"4) 1) "d" 2) "12.8164"

actual combat

After introducing the above knowledge, you can use php combined with redis to complete the function of shaking to find nearby people. First, save the location information of the members.

The pseudocode is as follows:

function addLocation ($key,$member, $lng, $lat){ $redis->geoadd($key, $lng, $lat, $member);}

Then, get information about people nearby.

function near ( $key, $member, $radius, $unit = 'km', $count = 0, $withDist = false, $withcoord = false, $orderby = 'ASC'){ $redis = new Redis(); $redis->connect('localhost', 6379); $options = [$orderby]; if ($count > 0) { $options['count'] = $count; } if ($withDist) { $options[] = 'WITHDIST'; } if ($withcoord) { $options[] = 'WITHCOORD'; } $result = $redis->geoRadiusByMember($key, $member, $radius, $unit, $options); return $result;}

The above is the detailed content of using Redis to complete the WeChat shake function. For more information, please pay attention to other related articles!

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: 242

*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.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report