Skip to content

Commit 7e138c5

Browse files
author
Alexander Cheprasov
committed
Added Geo and Latency commands
1 parent 5070346 commit 7e138c5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+675
-85
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
# php-redis-client
22
Project in development
3+
4+
Docker for tests
5+
https://hub.docker.com/r/cheprasov/redis-for-tests/

src/RedisClient/Command/Response/ResponseParser.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class ResponseParser {
1616
const PARSE_INTEGER = 2;
1717
const PARSE_TIME = 3;
1818
const PARSE_INFO = 4;
19+
const PARSE_GEO_ARRAY = 5;
1920

2021
/**
2122
* @param int $type
@@ -32,6 +33,8 @@ public static function parse($type, $response) {
3233
return self::parseTime($response);
3334
case self::PARSE_INFO:
3435
return self::parseInfo($response);
36+
case self::PARSE_GEO_ARRAY:
37+
return self::parseGeoArray($response);
3538
default:
3639
return $response;
3740
}
@@ -52,6 +55,21 @@ public static function parseAssocArray($response) {
5255
return $array;
5356
}
5457

58+
/**
59+
* @param string[] $response
60+
* @return array
61+
*/
62+
public static function parseGeoArray($response) {
63+
if (!is_array($response)) {
64+
return $response;
65+
}
66+
$array = [];
67+
for ($i = 0, $count = count($response); $i < $count; $i += 1) {
68+
$array[array_shift($response[$i])] = $response[$i];
69+
}
70+
return $array;
71+
}
72+
5573
/**
5674
* @param string $response
5775
* @return string[]|array

src/RedisClient/Command/Traits/Version2x8/CommandsTrait.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ trait CommandsTrait {
2424
use HashesCommandsTrait;
2525
use HyperLogLogCommandsTrait;
2626
use KeysCommandsTrait;
27+
use LatencyCommandsTrait;
2728
use ListsCommandsTrait;
2829
use ScriptingCommandsTrait;
2930
use ServerCommandsTrait;
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
/**
3+
* This file is part of RedisClient.
4+
* git: https://github.com/cheprasov/php-redis-client
5+
*
6+
* (C) Alexander Cheprasov <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
namespace RedisClient\Command\Traits\Version2x8;
12+
13+
use RedisClient\Command\Parameter\Parameter;
14+
15+
/**
16+
* Latency Monitoring
17+
* @link http://redis.io/topics/latency-monitor
18+
*/
19+
trait LatencyCommandsTrait {
20+
21+
/**
22+
* LATENCY LATEST
23+
* Available since 2.8.13
24+
* @link http://redis.io/topics/latency-monitor
25+
*
26+
* @return array
27+
*/
28+
public function latencyLatest() {
29+
return $this->returnCommand(['LATENCY', 'LATEST']);
30+
}
31+
32+
/**
33+
* LATENCY HISTORY event-name
34+
* Available since 2.8.13
35+
* @link http://redis.io/topics/latency-monitor
36+
*
37+
* @param string $eventName
38+
* @return array
39+
*/
40+
public function latencyHistory($eventName) {
41+
return $this->returnCommand(['LATENCY', 'HISTORY'], [Parameter::string($eventName)]);
42+
}
43+
44+
/**
45+
* LATENCY RESET [event-name ... event-name]
46+
* Available since 2.8.13
47+
* @link http://redis.io/topics/latency-monitor
48+
*
49+
* @param string|string[] $eventNames
50+
* @return int
51+
*/
52+
public function latencyReset($eventNames = null) {
53+
return $this->returnCommand(['LATENCY', 'RESET'], Parameter::strings($eventNames));
54+
}
55+
56+
/**
57+
* LATENCY GRAPH event-name
58+
* Available since 2.8.13
59+
* @link http://redis.io/topics/latency-monitor
60+
*
61+
* @param string $eventName
62+
* @return string
63+
*/
64+
public function latencyGraph($eventName) {
65+
return $this->returnCommand(['LATENCY', 'GRAPH'], [Parameter::string($eventName)]);
66+
}
67+
68+
/**
69+
* LATENCY DOCTOR
70+
* Available since 2.8.13
71+
* @link http://redis.io/topics/latency-monitor
72+
*
73+
* @return string
74+
*/
75+
public function latencyDoctor() {
76+
return $this->returnCommand(['LATENCY', 'DOCTOR']);
77+
}
78+
79+
}

src/RedisClient/Command/Traits/Version2x9/CommandsTrait.php

Lines changed: 0 additions & 47 deletions
This file was deleted.

src/RedisClient/Command/Traits/Version3x0/CommandsTrait.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use RedisClient\Command\Traits\Version2x8\HashesCommandsTrait;
1616
use RedisClient\Command\Traits\Version2x6\ListsCommandsTrait;
1717
use RedisClient\Command\Traits\Version2x6\ScriptingCommandsTrait;
18+
use RedisClient\Command\Traits\Version2x8\LatencyCommandsTrait;
1819
use RedisClient\Command\Traits\Version2x9\ServerCommandsTrait;
1920
use RedisClient\Command\Traits\Version2x8\SetsCommandsTrait;
2021
use RedisClient\Command\Traits\Version2x8\StringsCommandsTrait;
@@ -30,6 +31,7 @@ trait CommandsTrait {
3031
use HashesCommandsTrait;
3132
use HyperLogLogCommandsTrait;
3233
use KeysCommandsTrait;
34+
use LatencyCommandsTrait;
3335
use ListsCommandsTrait;
3436
use ScriptingCommandsTrait;
3537
use ServerCommandsTrait;

src/RedisClient/Command/Traits/Version3x2/CommandsTrait.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
namespace RedisClient\Command\Traits\Version3x2;
1212

1313
use RedisClient\Command\Traits\AbstractCommandsTrait;
14+
use RedisClient\Command\Traits\Version2x8\LatencyCommandsTrait;
1415
use RedisClient\Command\Traits\Version3x0\ClusterCommandsTrait;
1516
use RedisClient\Command\Traits\Version2x6\ConnectionCommandsTrait;
1617
use RedisClient\Command\Traits\Version2x8\HyperLogLogCommandsTrait;
@@ -28,9 +29,11 @@ trait CommandsTrait {
2829

2930
use ClusterCommandsTrait;
3031
use ConnectionCommandsTrait;
32+
use GeoCommandsTrait;
3133
use HashesCommandsTrait;
3234
use HyperLogLogCommandsTrait;
3335
use KeysCommandsTrait;
36+
use LatencyCommandsTrait;
3437
use ListsCommandsTrait;
3538
use ScriptingCommandsTrait;
3639
use ServerCommandsTrait;

src/RedisClient/Command/Traits/Version3x2/GeoCommandsTrait.php

Lines changed: 125 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@
1111
namespace RedisClient\Command\Traits\Version3x2;
1212

1313
use RedisClient\Command\Parameter\Parameter;
14+
use RedisClient\Command\Response\ResponseParser;
1415

1516
trait GeoCommandsTrait {
1617

1718
/**
1819
* GEOADD key longitude latitude member [longitude latitude member ...]
1920
* Beta Not yet available in a stable version of Redis. Download unstable if you want to test this command.
2021
* Time complexity: O(log(N)) for each item added, where N is the number of elements in the sorted set.
22+
* @link http://redis.io/commands/geoadd
2123
*
2224
* @param $key
2325
* @param array $members [member => [longitude, latitude]]
@@ -29,9 +31,9 @@ public function geoadd($key, array $members) {
2931
Parameter::key($key)
3032
];
3133
foreach ($members as $member => $degrees) {
32-
$params[] = $degrees[0];
33-
$params[] = $degrees[1];
34-
$params[] = $member;
34+
$params[] = Parameter::string($degrees[0]);
35+
$params[] = Parameter::string($degrees[1]);
36+
$params[] = Parameter::key($member);
3537
}
3638
return $this->returnCommand(['GEOADD'], $params);
3739
}
@@ -40,6 +42,7 @@ public function geoadd($key, array $members) {
4042
* GEODIST key member1 member2 [unit]
4143
* Beta Not yet available in a stable version of Redis. Download unstable if you want to test this command.
4244
* Time complexity: O(log(N))
45+
* @link http://redis.io/commands/geodist
4346
*
4447
* @param string $key
4548
* @param string $member1
@@ -64,6 +67,7 @@ public function geodist($key, $member1, $member2, $unit = null) {
6467
* GEOHASH key member [member ...]
6568
* Beta Not yet available in a stable version of Redis. Download unstable if you want to test this command.
6669
* Time complexity: O(log(N)) for each member requested, where N is the number of elements in the sorted set.
70+
* @link http://redis.io/commands/geohash
6771
*
6872
* @param string $key
6973
* @param string|string[] $members
@@ -76,4 +80,122 @@ public function geohash($key, $members) {
7680
Parameter::keys($members),
7781
]);
7882
}
83+
84+
/**
85+
* GEOPOS key member [member ...]
86+
* Beta Not yet available in a stable version of Redis. Download unstable if you want to test this command.
87+
* Time complexity: O(log(N)) for each member requested, where N is the number of elements in the sorted set.
88+
* @link http://redis.io/commands/geopos
89+
*
90+
* @param string $key
91+
* @param string|string[] $members
92+
* @return string[] The command returns an array where each element is a two elements array
93+
* representing longitude and latitude (x,y) of each member name passed as argument to the command.
94+
* Non existing elements are reported as NULL elements of the array.
95+
*/
96+
public function geopos($key, $members) {
97+
return $this->returnCommand(['GEOPOS'], [
98+
Parameter::key($key),
99+
Parameter::keys($members),
100+
]);
101+
}
102+
103+
/**
104+
* GEORADIUS key longitude latitude radius m|km|ft|mi [WITHCOORD] [WITHDIST] [WITHHASH] [COUNT count]
105+
* Beta Not yet available in a stable version of Redis. Download unstable if you want to test this command.
106+
* Time complexity: O(N+log(M)) where N is the number of elements inside the bounding box of
107+
* the circular area delimited by center and radius and M is the number of items inside the index.
108+
* @link http://redis.io/commands/georadius
109+
*
110+
* @param string $key
111+
* @param string $longitude
112+
* @param string $latitude
113+
* @param string $radius
114+
* @param string $unit
115+
* @param bool|false $withcoord
116+
* @param bool|false $withdist
117+
* @param bool|false $withhash
118+
* @param int|null $count
119+
* @param bool|null $asc (true => ASC, false => DESC)
120+
* @return array
121+
*/
122+
public function georadius($key, $longitude, $latitude, $radius, $unit, $withcoord = false, $withdist = false, $withhash = false, $count = null, $asc = null) {
123+
$params = [
124+
Parameter::key($key),
125+
Parameter::string($longitude),
126+
Parameter::string($latitude),
127+
Parameter::string($radius),
128+
Parameter::geoUnit($unit),
129+
];
130+
$parse = false;
131+
if ($withcoord) {
132+
$params[] = Parameter::string('WITHCOORD');
133+
$parse = true;
134+
}
135+
if ($withdist) {
136+
$params[] = Parameter::string('WITHDIST');
137+
$parse = true;
138+
}
139+
if ($withhash) {
140+
$params[] = Parameter::string('WITHHASH');
141+
$parse = true;
142+
}
143+
if ($count) {
144+
$params[] = Parameter::string('COUNT');
145+
$params[] = Parameter::integer($count);
146+
}
147+
if (isset($asc)) {
148+
$params[] = Parameter::string((bool) $asc ? 'ASC' : 'DESC');
149+
}
150+
return $this->returnCommand(['GEORADIUS'], $params, $parse ? ResponseParser::PARSE_GEO_ARRAY : null);
151+
}
152+
153+
154+
/**
155+
* GEORADIUSBYMEMBER key member radius m|km|ft|mi [WITHCOORD] [WITHDIST] [WITHHASH] [COUNT count]
156+
* Beta Not yet available in a stable version of Redis. Download unstable if you want to test this command.
157+
* Time complexity: O(N+log(M)) where N is the number of elements inside the bounding box of
158+
* the circular area delimited by center and radius and M is the number of items inside the index.
159+
* @link http://redis.io/commands/georadiusbymember
160+
*
161+
* @param string $key
162+
* @param string $member
163+
* @param string $radius
164+
* @param string $unit
165+
* @param bool|false $withcoord
166+
* @param bool|false $withdist
167+
* @param bool|false $withhash
168+
* @param int|null $count
169+
* @param bool|null $asc (true => ASC, false => DESC)
170+
* @return array
171+
*/
172+
public function georadiusbymember($key, $member, $radius, $unit, $withcoord = false, $withdist = false, $withhash = false, $count = null, $asc = null) {
173+
$params = [
174+
Parameter::key($key),
175+
Parameter::key($member),
176+
Parameter::string($radius),
177+
Parameter::geoUnit($unit),
178+
];
179+
$parse = false;
180+
if ($withcoord) {
181+
$params[] = Parameter::string('WITHCOORD');
182+
$parse = true;
183+
}
184+
if ($withdist) {
185+
$params[] = Parameter::string('WITHDIST');
186+
$parse = true;
187+
}
188+
if ($withhash) {
189+
$params[] = Parameter::string('WITHHASH');
190+
$parse = true;
191+
}
192+
if ($count) {
193+
$params[] = Parameter::string('COUNT');
194+
$params[] = Parameter::integer($count);
195+
}
196+
if (isset($asc)) {
197+
$params[] = Parameter::string((bool) $asc ? 'ASC' : 'DESC');
198+
}
199+
return $this->returnCommand(['GEORADIUSBYMEMBER'], $params, $parse ? ResponseParser::PARSE_GEO_ARRAY : null);
200+
}
79201
}

0 commit comments

Comments
 (0)