Ahmed Dhanani
Ahmed Dhanani

Reputation: 861

What would be the best way to use redis for gps location tracking

I am new to redis and was thinking of implementing some project in order to get familiar with it. One interesting project that came in my mind was using redis as a cache for real time gps location. The only part which confuses me is its implementation. I read about support of geospatial data by redia, but lets sey if I want to keep on updating the location points against some key, that doesn't seem to be possible.

One way that I started out with was to use hash structure for storing lat and long of a device that needs to be tracked, and keep on setting its value which in turn updates the value, and keeping all these hashes in a set. But that doesn't seem to be a hood approach and it also wont allow me to use geospatial queries provided by redis.

Any leads on how coukd it be implemented in an efficient way.

Upvotes: 3

Views: 1144

Answers (1)

Not_a_Golfer
Not_a_Golfer

Reputation: 49255

You can simply use GEOADD repeatedly on the same device id with different coordinates. This will "move" the object's location in the geo set and will immediately affect the next radius queries.

127.0.0.1:6379> GEOADD foo 34 32 bar
(integer) 1
127.0.0.1:6379> GEORADIUS foo 34 32 100 m
1) "bar"

# Let's "move" bar in foo to new coordinates
127.0.0.1:6379> GEOADD foo 35 36 bar
(integer) 0
127.0.0.1:6379> GEORADIUS foo 34 32 100 m
(empty list or set)
127.0.0.1:6379> GEORADIUS foo 35 36 100 m
1) "bar"

And if you want the coordinates, that's also easy:

127.0.0.1:6379> GEORADIUS foo 35 36 100 m WITHCOORD
1) 1) "bar"
   2) 1) "34.99999791383743286"
  2) "35.99999953955607168"

Upvotes: 2

Related Questions