Skip to content

Commit

Permalink
Add a few geo commands
Browse files Browse the repository at this point in the history
- GEOADD
- GEOPOS
- GEOSEARCH (only FROMLONLAT + BYRADIUS)
  • Loading branch information
jgaskins committed Dec 19, 2024
1 parent 55e14b3 commit 7cc624a
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/commands.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ require "./commands/list"
require "./commands/set"
require "./commands/sorted_set"
require "./commands/stream"
require "./commands/geo"

module Redis
# All Redis commands are defined in this module. Any paradigm that needs to
Expand All @@ -17,6 +18,7 @@ module Redis
include Commands::Set
include Commands::SortedSet
include Commands::Stream
include Commands::Geo

# Execute the given command and return the result from the server. Commands
# must be an `Enumerable` and its `size` method must be re-entrant.
Expand Down
67 changes: 67 additions & 0 deletions src/commands/geo.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Namespace for types that support `Redis::Commands::Geo` commands.
module Redis::Geo
record Radius, magnitude : String, unit : Unit do
def self.new(magnitude : Int, unit : Unit)
new magnitude.to_s, unit
end

def to_tuple
{magnitude, unit.to_s}
end

enum Unit
M
KM
FT
MI
end
end

enum Sort
ASC
DESC
end
end

module Redis::Commands::Geo
def geoadd(key : String, *entries : String, nx = nil, xx = nil, ch = nil)
command = {"geoadd", key}
command += {"nx"} if nx
command += {"xx"} if xx
command += {"ch"} if ch
command += entries

run command
end

def geopos(key : String, *members : String)
run({"geopos", key, *members})
end

def geosearch(
key : String,
*,
fromlonlat lonlat : Tuple(String, String),
byradius radius : Redis::Geo::Radius,
sort : Redis::Geo::Sort? = nil,
count : Int | String | Nil = nil,
withcoord : Bool = false,
withdist : Bool = false,
)
command = {
"geosearch",
key,
"fromlonlat",
*lonlat,
"byradius",
*radius.to_tuple,
}

command += {sort.to_s} if sort
command += {"count", count.to_s} if count
command += {"withcoord"} if withcoord
command += {"withdist"} if withdist

run command
end
end
5 changes: 4 additions & 1 deletion src/connection.cr
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ module Redis
# :nodoc:
macro override_return_types(methods)
{% for method, return_type in methods %}
{% for methods in [Commands, Commands::Hash, Commands::List, Commands::Set, Commands::SortedSet, Commands::Stream].map(&.methods.select { |m| m.name == method }).reject(&.nil?) %}
{% for methods in [Commands, Commands::Hash, Commands::List, Commands::Set, Commands::SortedSet, Commands::Stream, Commands::Geo].map(&.methods.select { |m| m.name == method }).reject(&.nil?) %}
{% for m in methods %}
# :nodoc:
def {{method.id}}(
Expand Down Expand Up @@ -336,6 +336,9 @@ module Redis
xreadgroup: Array(Value)?,
xrevrange: Array,
xtrim: Int64,

geopos: Array,
geosearch: Array,
})
end

Expand Down

0 comments on commit 7cc624a

Please sign in to comment.