| 1 |
package redis |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"errors" |
| 6 |
) |
| 7 |
|
| 8 |
type GeoCmdable interface { |
| 9 |
GeoAdd(ctx context.Context, key string, geoLocation ...*GeoLocation) *IntCmd |
| 10 |
GeoPos(ctx context.Context, key string, members ...string) *GeoPosCmd |
| 11 |
GeoRadius(ctx context.Context, key string, longitude, latitude float64, query *GeoRadiusQuery) *GeoLocationCmd |
| 12 |
GeoRadiusStore(ctx context.Context, key string, longitude, latitude float64, query *GeoRadiusQuery) *IntCmd |
| 13 |
GeoRadiusByMember(ctx context.Context, key, member string, query *GeoRadiusQuery) *GeoLocationCmd |
| 14 |
GeoRadiusByMemberStore(ctx context.Context, key, member string, query *GeoRadiusQuery) *IntCmd |
| 15 |
GeoSearch(ctx context.Context, key string, q *GeoSearchQuery) *StringSliceCmd |
| 16 |
GeoSearchLocation(ctx context.Context, key string, q *GeoSearchLocationQuery) *GeoSearchLocationCmd |
| 17 |
GeoSearchStore(ctx context.Context, key, store string, q *GeoSearchStoreQuery) *IntCmd |
| 18 |
GeoDist(ctx context.Context, key string, member1, member2, unit string) *FloatCmd |
| 19 |
GeoHash(ctx context.Context, key string, members ...string) *StringSliceCmd |
| 20 |
} |
| 21 |
|
| 22 |
func (c cmdable) GeoAdd(ctx context.Context, key string, geoLocation ...*GeoLocation) *IntCmd { |
| 23 |
args := make([]interface{}, 2+3*len(geoLocation)) |
| 24 |
args[0] = "geoadd" |
| 25 |
args[1] = key |
| 26 |
for i, eachLoc := range geoLocation { |
| 27 |
args[2+3*i] = eachLoc.Longitude |
| 28 |
args[2+3*i+1] = eachLoc.Latitude |
| 29 |
args[2+3*i+2] = eachLoc.Name |
| 30 |
} |
| 31 |
cmd := NewIntCmd(ctx, args...) |
| 32 |
_ = c(ctx, cmd) |
| 33 |
return cmd |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
func (c cmdable) GeoRadius( |
| 38 |
ctx context.Context, key string, longitude, latitude float64, query *GeoRadiusQuery, |
| 39 |
) *GeoLocationCmd { |
| 40 |
cmd := NewGeoLocationCmd(ctx, query, "georadius_ro", key, longitude, latitude) |
| 41 |
if query.Store != "" || query.StoreDist != "" { |
| 42 |
cmd.SetErr(errors.New("GeoRadius does not support Store or StoreDist")) |
| 43 |
return cmd |
| 44 |
} |
| 45 |
_ = c(ctx, cmd) |
| 46 |
return cmd |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
func (c cmdable) GeoRadiusStore( |
| 51 |
ctx context.Context, key string, longitude, latitude float64, query *GeoRadiusQuery, |
| 52 |
) *IntCmd { |
| 53 |
args := geoLocationArgs(query, "georadius", key, longitude, latitude) |
| 54 |
cmd := NewIntCmd(ctx, args...) |
| 55 |
if query.Store == "" && query.StoreDist == "" { |
| 56 |
cmd.SetErr(errors.New("GeoRadiusStore requires Store or StoreDist")) |
| 57 |
return cmd |
| 58 |
} |
| 59 |
_ = c(ctx, cmd) |
| 60 |
return cmd |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
func (c cmdable) GeoRadiusByMember( |
| 65 |
ctx context.Context, key, member string, query *GeoRadiusQuery, |
| 66 |
) *GeoLocationCmd { |
| 67 |
cmd := NewGeoLocationCmd(ctx, query, "georadiusbymember_ro", key, member) |
| 68 |
if query.Store != "" || query.StoreDist != "" { |
| 69 |
cmd.SetErr(errors.New("GeoRadiusByMember does not support Store or StoreDist")) |
| 70 |
return cmd |
| 71 |
} |
| 72 |
_ = c(ctx, cmd) |
| 73 |
return cmd |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
func (c cmdable) GeoRadiusByMemberStore( |
| 78 |
ctx context.Context, key, member string, query *GeoRadiusQuery, |
| 79 |
) *IntCmd { |
| 80 |
args := geoLocationArgs(query, "georadiusbymember", key, member) |
| 81 |
cmd := NewIntCmd(ctx, args...) |
| 82 |
if query.Store == "" && query.StoreDist == "" { |
| 83 |
cmd.SetErr(errors.New("GeoRadiusByMemberStore requires Store or StoreDist")) |
| 84 |
return cmd |
| 85 |
} |
| 86 |
_ = c(ctx, cmd) |
| 87 |
return cmd |
| 88 |
} |
| 89 |
|
| 90 |
func (c cmdable) GeoSearch(ctx context.Context, key string, q *GeoSearchQuery) *StringSliceCmd { |
| 91 |
args := make([]interface{}, 0, 13) |
| 92 |
args = append(args, "geosearch", key) |
| 93 |
args = geoSearchArgs(q, args) |
| 94 |
cmd := NewStringSliceCmd(ctx, args...) |
| 95 |
_ = c(ctx, cmd) |
| 96 |
return cmd |
| 97 |
} |
| 98 |
|
| 99 |
func (c cmdable) GeoSearchLocation( |
| 100 |
ctx context.Context, key string, q *GeoSearchLocationQuery, |
| 101 |
) *GeoSearchLocationCmd { |
| 102 |
args := make([]interface{}, 0, 16) |
| 103 |
args = append(args, "geosearch", key) |
| 104 |
args = geoSearchLocationArgs(q, args) |
| 105 |
cmd := NewGeoSearchLocationCmd(ctx, q, args...) |
| 106 |
_ = c(ctx, cmd) |
| 107 |
return cmd |
| 108 |
} |
| 109 |
|
| 110 |
func (c cmdable) GeoSearchStore(ctx context.Context, key, store string, q *GeoSearchStoreQuery) *IntCmd { |
| 111 |
args := make([]interface{}, 0, 15) |
| 112 |
args = append(args, "geosearchstore", store, key) |
| 113 |
args = geoSearchArgs(&q.GeoSearchQuery, args) |
| 114 |
if q.StoreDist { |
| 115 |
args = append(args, "storedist") |
| 116 |
} |
| 117 |
cmd := NewIntCmd(ctx, args...) |
| 118 |
_ = c(ctx, cmd) |
| 119 |
return cmd |
| 120 |
} |
| 121 |
|
| 122 |
func (c cmdable) GeoDist( |
| 123 |
ctx context.Context, key string, member1, member2, unit string, |
| 124 |
) *FloatCmd { |
| 125 |
if unit == "" { |
| 126 |
unit = "km" |
| 127 |
} |
| 128 |
cmd := NewFloatCmd(ctx, "geodist", key, member1, member2, unit) |
| 129 |
_ = c(ctx, cmd) |
| 130 |
return cmd |
| 131 |
} |
| 132 |
|
| 133 |
func (c cmdable) GeoHash(ctx context.Context, key string, members ...string) *StringSliceCmd { |
| 134 |
args := make([]interface{}, 2+len(members)) |
| 135 |
args[0] = "geohash" |
| 136 |
args[1] = key |
| 137 |
for i, member := range members { |
| 138 |
args[2+i] = member |
| 139 |
} |
| 140 |
cmd := NewStringSliceCmd(ctx, args...) |
| 141 |
_ = c(ctx, cmd) |
| 142 |
return cmd |
| 143 |
} |
| 144 |
|
| 145 |
func (c cmdable) GeoPos(ctx context.Context, key string, members ...string) *GeoPosCmd { |
| 146 |
args := make([]interface{}, 2+len(members)) |
| 147 |
args[0] = "geopos" |
| 148 |
args[1] = key |
| 149 |
for i, member := range members { |
| 150 |
args[2+i] = member |
| 151 |
} |
| 152 |
cmd := NewGeoPosCmd(ctx, args...) |
| 153 |
_ = c(ctx, cmd) |
| 154 |
return cmd |
| 155 |
} |