如何在Redis中使用GEOADD存储其他数据

时间:2019-04-02 13:35:12

标签: php redis

我是Redis的新手。我需要存储多个位置数据,如下所示:

[
    [
        name: "shop 1", 
        address: "Address1",
        zip: 12345,
        lng: 13.00,
        lat:37.00
    ], [
        name: "shop 2",
        address: "Address2",
        zip: 67890,
        lng: 15.00,
        lat:38.00
    ],
    //…
]

我能够使用GEOADD并使用GEORADIUS进行搜索。 但是,GEOADD只接受lng,lat和一个参数。而且我还需要存储所有其他字段。并且需要根据地址/邮政编码进行搜索。

有任何例子吗?

1 个答案:

答案 0 :(得分:0)

我通过以下策略在自己的应用中解决了此问题。

插入数据

HSET shop:payload:shop1 name "shop 1" address "address 1" zip 12345
HSET shop:payload:shop2 name "shop 2" address "address 2" zip 67890
GEOADD shop:geo 13.00 37.00 "shop1" 
GEOADD shop:geo 15.00 38.00 "shop2"

查询ID

GEORADIUS shop:geo 14.00 38.00 100 mi WITHDIST WITHCOORD
> shop2,54.4615,14.99999910593032837,38.0000009925631943
> shop1,88.2202,12.99999922513961792,37.00000026605963654

查询有效载荷(我使用管道试图加快速度)

HGETALL shop:payload:shop2
> {
>   "name": "shop 2",
>   "address": "address 2",
>   "zip": "67890"
> }

HGETALL shop:payload:shop1
> {
>   "name": "shop 1",
>   "address": "address 1",
>   "zip": "12345"
> }

然后您将数据添加到后端。

[
  {
    "name": "shop 2",
    "address": "address 2",
    "zip": "67890",
    "dist": 54.4615,
    "lon": 14.99999910593032837,
    "lat": 38.0000009925631943
  },
  {
    "name": "shop 1",
    "address": "address 1",
    "zip": "12345",
    "dist": 88.2202,
    "lon": 12.99999922513961792,
    "lat": 37.00000026605963654
  }
]