Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 5626a22

Browse files
committed
Add support for GeoAdd + GeoPos
1 parent 6c313b9 commit 5626a22

File tree

4 files changed

+103
-0
lines changed

4 files changed

+103
-0
lines changed

src/ServiceStack.Redis/Commands.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,5 +211,13 @@ public static class Commands
211211
public readonly static byte[] Slaves = "slaves".ToUtf8Bytes();
212212
public readonly static byte[] Failover = "failover".ToUtf8Bytes();
213213
public readonly static byte[] GetMasterAddrByName = "get-master-addr-by-name".ToUtf8Bytes();
214+
215+
//Geo commands
216+
public readonly static byte[] GeoAdd = "GEOADD".ToUtf8Bytes();
217+
public readonly static byte[] GeoDist = "GEODIST".ToUtf8Bytes();
218+
public readonly static byte[] GeoHash = "GEOHASH".ToUtf8Bytes();
219+
public readonly static byte[] GeoPos = "GEOPOS".ToUtf8Bytes();
220+
public readonly static byte[] GeoRadius = "GEORADIUS".ToUtf8Bytes();
221+
public readonly static byte[] GeoRadiusByMember = "GEORADIUSBYMEMBER".ToUtf8Bytes();
214222
}
215223
}

src/ServiceStack.Redis/RedisNativeClient.cs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2201,6 +2201,67 @@ public RedisPipelineCommand CreatePipelineCommand()
22012201

22022202
#endregion
22032203

2204+
#region GEO Operations
2205+
2206+
public long GeoAdd(string key, double longitude, double latitude, string member)
2207+
{
2208+
if (key == null)
2209+
throw new ArgumentNullException("key");
2210+
if (key == null)
2211+
throw new ArgumentNullException("member");
2212+
2213+
return SendExpectLong(Commands.GeoAdd, key.ToUtf8Bytes(), longitude.ToUtf8Bytes(), latitude.ToUtf8Bytes(), member.ToUtf8Bytes());
2214+
}
2215+
2216+
public long GeoAdd(string key, RedisGeo[] geoPoints)
2217+
{
2218+
throw new NotImplementedException();
2219+
}
2220+
2221+
public double GeoDist(string key, string fromMember, string toMember, string unit = null)
2222+
{
2223+
throw new NotImplementedException();
2224+
}
2225+
2226+
public byte[][] GeoHash(string key, params string[] members)
2227+
{
2228+
throw new NotImplementedException();
2229+
}
2230+
2231+
public List<RedisGeo> GeoPos(string key, params string[] members)
2232+
{
2233+
var cmdWithArgs = MergeCommandWithArgs(Commands.GeoPos, key.ToUtf8Bytes(), members.Map(x => x.ToUtf8Bytes()).ToArray());
2234+
var data = SendExpectComplexResponse(cmdWithArgs);
2235+
var to = new List<RedisGeo>();
2236+
2237+
for (var i = 0; i < members.Length; i++)
2238+
{
2239+
var entry = data.Children[i];
2240+
to.Add(new RedisGeo
2241+
{
2242+
Longitude = double.Parse(entry.Children[0].Data.FromUtf8Bytes()),
2243+
Latitude = double.Parse(entry.Children[1].Data.FromUtf8Bytes()),
2244+
Member = members[i],
2245+
});
2246+
}
2247+
2248+
return to;
2249+
}
2250+
2251+
public List<RedisGeoResult> GeoRadius(string key, double longitude, double latitude, double radius,
2252+
string unit = null, bool withCoords = false, bool withHash = false, int count = 0, bool? asc = null)
2253+
{
2254+
throw new NotImplementedException();
2255+
}
2256+
2257+
public List<RedisGeoResult> GeoRadiusByMember(string key, double longitude, double latitude, double radius,
2258+
string unit = null, bool withCoords = false, bool withHash = false, int count = 0, bool? asc = null)
2259+
{
2260+
throw new NotImplementedException();
2261+
}
2262+
2263+
#endregion
2264+
22042265
internal bool IsDisposed { get; set; }
22052266

22062267
public bool IsManagedClient
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using NUnit.Framework;
2+
3+
namespace ServiceStack.Redis.Tests
4+
{
5+
[TestFixture]
6+
public class RedisGeoTests
7+
{
8+
private RedisNativeClient redis;
9+
10+
public RedisGeoTests()
11+
{
12+
redis = new RedisNativeClient("10.0.0.121");
13+
}
14+
15+
[TestFixtureTearDown]
16+
public void TestFixtureTearDown()
17+
{
18+
redis.Dispose();
19+
}
20+
21+
[Test]
22+
public void Can_GeoAdd_and_GeoPos()
23+
{
24+
redis.GeoAdd("Sicily", 13.361389, 38.115556, "Palermo");
25+
var results = redis.GeoPos("Sicily", "Palermo");
26+
27+
Assert.That(results.Count, Is.EqualTo(1));
28+
Assert.That(results[0].Longitude, Is.EqualTo(13.361389).Within(.1));
29+
Assert.That(results[0].Latitude, Is.EqualTo(38.115556).Within(.1));
30+
Assert.That(results[0].Member, Is.EqualTo("Palermo"));
31+
}
32+
}
33+
}

tests/ServiceStack.Redis.Tests/ServiceStack.Redis.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@
189189
<Compile Include="Examples\TestData.cs" />
190190
<Compile Include="Issues\RedisCharacterizationTests.cs" />
191191
<Compile Include="RedisBatchTests.cs" />
192+
<Compile Include="RedisGeoTests.cs" />
192193
<Compile Include="RedisManagerPoolTests.cs" />
193194
<Compile Include="DiagnosticTests.cs" />
194195
<Compile Include="Examples\ServiceStack_Redis_UseCase.cs" />

0 commit comments

Comments
 (0)