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

Commit e1ff3be

Browse files
committed
Refactored RedisNativeClient to use RedisDataExtensions methods for parsing RedisData.
Added ToDouble and ToInt64 to RedisDataExtensions in order to parse RedisData.
1 parent be6a5ab commit e1ff3be

File tree

3 files changed

+35
-56
lines changed

3 files changed

+35
-56
lines changed
Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Generic;
2+
using System.Globalization;
23

34
namespace ServiceStack.Redis
45
{
@@ -19,28 +20,28 @@ public static RedisText ToRedisText(this RedisData data)
1920
return to;
2021
}
2122

22-
public static string GetResult(this RedisText from)
23-
{
24-
return from.Text;
25-
}
23+
public static double ToDouble(this RedisData data)
24+
=> double.Parse(data.Data.FromUtf8Bytes(),
25+
NumberStyles.Float,
26+
CultureInfo.InvariantCulture);
2627

27-
public static T GetResult<T>(this RedisText from)
28-
{
29-
return from.Text.FromJson<T>();
30-
}
28+
public static long ToInt64(this RedisData data)
29+
=> long.Parse(data.Data.FromUtf8Bytes(),
30+
NumberStyles.Integer,
31+
CultureInfo.InvariantCulture);
32+
33+
public static string GetResult(this RedisText from) => from.Text;
34+
35+
public static T GetResult<T>(this RedisText from) => from.Text.FromJson<T>();
3136

3237
public static List<string> GetResults(this RedisText from)
33-
{
34-
return from.Children == null
35-
? new List<string>()
36-
: from.Children.ConvertAll(x => x.Text);
37-
}
38+
=> from.Children == null
39+
? new List<string>()
40+
: from.Children.ConvertAll(x => x.Text);
3841

3942
public static List<T> GetResults<T>(this RedisText from)
40-
{
41-
return from.Children == null
42-
? new List<T>()
43-
: from.Children.ConvertAll(x => x.Text.FromJson<T>());
44-
}
43+
=> from.Children == null
44+
? new List<T>()
45+
: from.Children.ConvertAll(x => x.Text.FromJson<T>());
4546
}
4647
}

src/ServiceStack.Redis/RedisNativeClient.cs

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2236,17 +2236,15 @@ public List<RedisGeo> GeoPos(string key, params string[] members)
22362236
break;
22372237

22382238
var entry = data.Children[i];
2239-
if (entry.Children.Count == 0)
2239+
2240+
var children = entry.Children;
2241+
if (children.Count == 0)
22402242
continue;
22412243

22422244
to.Add(new RedisGeo
22432245
{
2244-
Longitude = double.Parse(entry.Children[0].Data.FromUtf8Bytes(),
2245-
NumberStyles.Float,
2246-
CultureInfo.InvariantCulture),
2247-
Latitude = double.Parse(entry.Children[1].Data.FromUtf8Bytes(),
2248-
NumberStyles.Float,
2249-
CultureInfo.InvariantCulture),
2246+
Longitude = children[0].ToDouble(),
2247+
Latitude = children[1].ToDouble(),
22502248
Member = members[i],
22512249
});
22522250
}
@@ -2307,24 +2305,15 @@ public List<RedisGeoResult> GeoRadius(string key, double longitude, double latit
23072305
var i = 0;
23082306
var result = new RedisGeoResult { Unit = unit, Member = child.Children[i++].Data.FromUtf8Bytes() };
23092307

2310-
if (withDist)
2311-
result.Distance = double.Parse(child.Children[i++].Data.FromUtf8Bytes(),
2312-
NumberStyles.Float,
2313-
CultureInfo.InvariantCulture);
2308+
if (withDist) result.Distance = child.Children[i++].ToDouble();
23142309

2315-
if (withHash)
2316-
result.Hash = long.Parse(child.Children[i++].Data.FromUtf8Bytes(),
2317-
NumberStyles.Integer,
2318-
CultureInfo.InvariantCulture);
2310+
if (withHash) result.Hash = child.Children[i++].ToInt64();
23192311

23202312
if (withCoords)
23212313
{
2322-
result.Longitude = double.Parse(child.Children[i].Children[0].Data.FromUtf8Bytes(),
2323-
NumberStyles.Float,
2324-
CultureInfo.InvariantCulture);
2325-
result.Latitude = double.Parse(child.Children[i].Children[1].Data.FromUtf8Bytes(),
2326-
NumberStyles.Float,
2327-
CultureInfo.InvariantCulture);
2314+
var children = child.Children[i].Children;
2315+
result.Longitude = children[0].ToDouble();
2316+
result.Latitude = children[1].ToDouble();
23282317
}
23292318

23302319
to.Add(result);
@@ -2386,24 +2375,15 @@ public List<RedisGeoResult> GeoRadiusByMember(string key, string member, double
23862375
var i = 0;
23872376
var result = new RedisGeoResult { Unit = unit, Member = child.Children[i++].Data.FromUtf8Bytes() };
23882377

2389-
if (withDist)
2390-
result.Distance = double.Parse(child.Children[i++].Data.FromUtf8Bytes(),
2391-
NumberStyles.Float,
2392-
CultureInfo.InvariantCulture);
2378+
if (withDist) result.Distance = child.Children[i++].ToDouble();
23932379

2394-
if (withHash)
2395-
result.Hash = long.Parse(child.Children[i++].Data.FromUtf8Bytes(),
2396-
NumberStyles.Integer,
2397-
CultureInfo.InvariantCulture);
2380+
if (withHash) result.Hash = child.Children[i++].ToInt64();
23982381

23992382
if (withCoords)
24002383
{
2401-
result.Longitude = double.Parse(child.Children[i].Children[0].Data.FromUtf8Bytes(),
2402-
NumberStyles.Float,
2403-
CultureInfo.InvariantCulture);
2404-
result.Latitude = double.Parse(child.Children[i].Children[1].Data.FromUtf8Bytes(),
2405-
NumberStyles.Float,
2406-
CultureInfo.InvariantCulture);
2384+
var children = child.Children[i].Children;
2385+
result.Longitude = children[0].ToDouble();
2386+
result.Latitude = children[1].ToDouble();
24072387
}
24082388

24092389
to.Add(result);

src/ServiceStack.Redis/ScanResult.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ public static Dictionary<string, double> AsItemsWithScores(this ScanResult resul
1616
for (var i = 0; i < result.Results.Count; i += 2)
1717
{
1818
var key = result.Results[i];
19-
var score = double.Parse(result.Results[i + 1].FromUtf8Bytes(),
20-
NumberStyles.Float,
21-
CultureInfo.InvariantCulture);
19+
var score = result.Results[i + 1].ToDouble();
2220
to[key.FromUtf8Bytes()] = score;
2321
}
2422
return to;

0 commit comments

Comments
 (0)