Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a8859b4

Browse files
Document GEO* commands
1 parent 9d15f07 commit a8859b4

1 file changed

Lines changed: 289 additions & 6 deletions

File tree

README.markdown

Lines changed: 289 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2965,6 +2965,289 @@ while($arr_matches = $redis->zScan('zset', $it, '*pattern*')) {
29652965
}
29662966
~~~
29672967

2968+
## Geocoding
2969+
2970+
### geoAdd
2971+
-----
2972+
2973+
##### *Prototype*
2974+
~~~
2975+
$redis->geoAdd($key, $longitude, $latitude, $member [, $longitude, $lattitude, $member, ...]);
2976+
~~~
2977+
2978+
_**Description**_: Add one or more geospacial items to the specified key. This function must be called with at least one _longitude, latitude, member_ triplet.
2979+
2980+
##### *Return value*
2981+
*Integer*: The number of elements added to the geospacial key.
2982+
2983+
##### *Example*
2984+
~~~
2985+
$redis->del("myplaces");
2986+
2987+
/* Since the key will be new, $result will be 2 */
2988+
$result = $redis->geoAdd(
2989+
"myplaces",
2990+
37.773, -122.431, "San Francisco",
2991+
-157.858, 21.315, "Honolulu"
2992+
);
2993+
~~~
2994+
2995+
### geoHash
2996+
-----
2997+
2998+
##### *Prototype*
2999+
~~~
3000+
$redis->geoHash($key, $member [, $member, $member, ...]);
3001+
~~~
3002+
3003+
_**Description**_: Retreive Geohash strings for one or more elements of a geospacial index.
3004+
3005+
##### *Return value*
3006+
*Array*: One or more Redis Geohash encoded strings.
3007+
3008+
##### *Example*
3009+
~~~
3010+
$redis->geoAdd("hawaii", -157.858, 21.306, "Honolulu", -156.331, 20.798, "Maui");
3011+
$hashes = $redis->geoHash("hawaii", "Honolulu", "Maui");
3012+
var_dump($hashes);
3013+
~~~
3014+
3015+
##### *Output*
3016+
~~~
3017+
array(2) {
3018+
[0]=>
3019+
string(11) "87z9pyek3y0"
3020+
[1]=>
3021+
string(11) "8e8y6d5jps0"
3022+
}
3023+
~~~
3024+
3025+
### geoPos
3026+
-----
3027+
3028+
##### *Prototype*
3029+
~~~
3030+
$redis->geoPos($key, $member [, $member, $member, ...]);
3031+
~~~
3032+
3033+
_**Description**_: Return longitude, lattitude positions for each requested member.
3034+
3035+
##### *Return value*
3036+
*Array*: One or more longitude/latitude positions
3037+
3038+
##### *Example*
3039+
~~~
3040+
$redis->geoAdd("hawaii", -157.858, 21.306, "Honolulu", -156.331, 20.798, "Maui");
3041+
$positions = $redis->geoPos("hawaii", "Honolulu", "Maui");
3042+
var_dump($positions);
3043+
~~~
3044+
3045+
##### *Output*
3046+
~~~
3047+
array(2) {
3048+
[0]=>
3049+
array(2) {
3050+
[0]=>
3051+
string(22) "-157.85800248384475708"
3052+
[1]=>
3053+
string(19) "21.3060004581273077"
3054+
}
3055+
[1]=>
3056+
array(2) {
3057+
[0]=>
3058+
string(22) "-156.33099943399429321"
3059+
[1]=>
3060+
string(20) "20.79799924753607598"
3061+
}
3062+
}
3063+
~~~
3064+
3065+
### GeoDist
3066+
-----
3067+
3068+
##### *Prototype*
3069+
~~~
3070+
$redis->geoDist($key, $member1, $member2 [, $unit]);
3071+
~~~
3072+
3073+
3074+
_**Description**_: Return the distance between two members in a geospacial set. If units are passed it must be one of the following values:
3075+
3076+
* 'm' => Meters
3077+
* 'km' => Kilometers
3078+
* 'mi' => Miles
3079+
* 'ft' => Feet
3080+
3081+
##### *Return value*
3082+
*Double*: The distance between the two passed members in the units requested (meters by default).
3083+
3084+
##### *Example*
3085+
~~~
3086+
$redis->geoAdd("hawaii", -157.858, 21.306, "Honolulu", -156.331, 20.798, "Maui");
3087+
3088+
$meters = $redis->geoDist("hawaii", "Honolulu", "Maui");
3089+
$kilometers = $redis->geoDist("hawaii", "Honolulu", "Maui", 'km');
3090+
$miles = $redis->geoDist("hawaii", "Honolulu", "Maui", 'mi');
3091+
$feet = $redis->geoDist("hawaii", "Honolulu", "Maui", 'ft');
3092+
3093+
echo "Distance between Honolulu and Maui:\n";
3094+
echo " meters : $meters\n";
3095+
echo " kilometers: $kilometers\n";
3096+
echo " miles : $miles\n";
3097+
echo " feet : $feet\n";
3098+
3099+
/* Bad unit */
3100+
$inches = $redis->geoDist("hawaii", "Honolulu", "Maui", 'in');
3101+
echo "Invalid unit returned:\n";
3102+
var_dump($inches);
3103+
~~~
3104+
3105+
##### *Output*
3106+
~~~
3107+
Distance between Honolulu and Maui:
3108+
meters : 168275.204
3109+
kilometers: 168.2752
3110+
miles : 104.5616
3111+
feet : 552084.0028
3112+
Invalid unit returned:
3113+
bool(false)
3114+
~~~
3115+
3116+
### geoRadius
3117+
-----
3118+
3119+
##### *Prototype*
3120+
~~~
3121+
$redis->geoRadius($key, $longitude, $latitude, $radius, $unit [, Array $options]);
3122+
~~~
3123+
3124+
_**Description**_: Return members of a set with geospacial information that are within the radius specified by the caller.
3125+
3126+
##### *Options Array*
3127+
The georadius command can be called with various options that control how Redis returns results. The following table describes the options phpredis supports. All options are case insensitive.
3128+
3129+
Key | Value | Description
3130+
--- | --- | ---- |
3131+
COUNT | `integer $count` | Limit how many results are returned
3132+
| WITHCOORD | Return longitude and latitude of matching members
3133+
| WITHDIST | Return the distance from the center
3134+
| WITHHASH | Return the raw geohash-encoded score
3135+
| ASC | Sort results in ascending order
3136+
| DESC | Sort results in descending order
3137+
3138+
*Note*: It doesn't make sense to pass both `ASC` and `DESC` options but if both are passed the last one passed will win!
3139+
*Note*: PhpRedis does not currently support the `STORE` or `STOREDIST` options but will be added to future versions.
3140+
3141+
##### *Return value*
3142+
*Array*: Zero or more entries that are within the provided radius.
3143+
3144+
##### *Example*
3145+
~~~
3146+
/* Add some cities */
3147+
$redis->geoAdd("hawaii", -157.858, 21.306, "Honolulu", -156.331, 20.798, "Maui");
3148+
3149+
echo "Within 300 miles of Honolulu:\n";
3150+
var_dump($redis->geoRadius("hawaii", -157.858, 21.306, 300, 'mi'));
3151+
3152+
echo "\nWithin 300 miles of Honolulu with distances:\n";
3153+
$options = ['WITHDIST'];
3154+
var_dump($redis->geoRadius("hawaii", -157.858, 21.306, 300, 'mi', $options));
3155+
3156+
echo "\nFirst result within 300 miles of Honolulu with distances:\n";
3157+
$options['count'] = 1;
3158+
var_dump($redis->geoRadius("hawaii", -157.858, 21.306, 300, 'mi', $options));
3159+
3160+
echo "\nFirst result within 300 miles of Honolulu with distances in descending sort order:\n";
3161+
$options[] = 'DESC';
3162+
var_dump($redis->geoRadius("hawaii", -157.858, 21.306, 300, 'mi', $options));
3163+
~~~
3164+
3165+
##### *Output*
3166+
~~~
3167+
Within 300 miles of Honolulu:
3168+
array(2) {
3169+
[0]=>
3170+
string(8) "Honolulu"
3171+
[1]=>
3172+
string(4) "Maui"
3173+
}
3174+
3175+
Within 300 miles of Honolulu with distances:
3176+
array(2) {
3177+
[0]=>
3178+
array(2) {
3179+
[0]=>
3180+
string(8) "Honolulu"
3181+
[1]=>
3182+
string(6) "0.0002"
3183+
}
3184+
[1]=>
3185+
array(2) {
3186+
[0]=>
3187+
string(4) "Maui"
3188+
[1]=>
3189+
string(8) "104.5615"
3190+
}
3191+
}
3192+
3193+
First result within 300 miles of Honolulu with distances:
3194+
array(1) {
3195+
[0]=>
3196+
array(2) {
3197+
[0]=>
3198+
string(8) "Honolulu"
3199+
[1]=>
3200+
string(6) "0.0002"
3201+
}
3202+
}
3203+
3204+
First result within 300 miles of Honolulu with distances in descending sort order:
3205+
array(1) {
3206+
[0]=>
3207+
array(2) {
3208+
[0]=>
3209+
string(4) "Maui"
3210+
[1]=>
3211+
string(8) "104.5615"
3212+
}
3213+
}
3214+
~~~
3215+
3216+
### geoRadiusByMember
3217+
3218+
##### *Prototype*
3219+
~~~
3220+
$redis->geoRadiusByMember($key, $member, $radius, $units [, array $options]);
3221+
~~~
3222+
3223+
_**Description**_: This method is identical to `geoRadius` except that instead of passing a longitude and latitude as the "source" you pass an existing member in the geospacial set.
3224+
3225+
##### *Options Array*
3226+
See [geoRadius](#georadius) command for options array.
3227+
3228+
##### *Return value*
3229+
*Array*: The zero or more entries that are close enough to the member given the distance and radius specified.
3230+
3231+
##### *Example*
3232+
~~~
3233+
$redis->geoAdd("hawaii", -157.858, 21.306, "Honolulu", -156.331, 20.798, "Maui");
3234+
3235+
echo "Within 300,000 miles of Honolulu:\n";
3236+
var_dump($redis->geoRadiusByMember("hawaii", "Honolulu", 300, 'mi'));
3237+
~~~
3238+
3239+
##### *Output*
3240+
~~~
3241+
Within 300,000 meters of Honolulu:
3242+
array(2) {
3243+
[0]=>
3244+
string(8) "Honolulu"
3245+
[1]=>
3246+
string(4) "Maui"
3247+
}
3248+
~~~
3249+
3250+
29683251
## Pub/sub
29693252

29703253
* [pSubscribe](#psubscribe) - Subscribe to channels by pattern
@@ -2977,9 +3260,9 @@ while($arr_matches = $redis->zScan('zset', $it, '*pattern*')) {
29773260
_**Description**_: Subscribe to channels by pattern
29783261

29793262
##### *Parameters*
2980-
*patterns*: An array of patterns to match
2981-
*callback*: Either a string or an array with an object and method. The callback will get four arguments ($redis, $pattern, $channel, $message)
2982-
*return value*: Mixed. Any non-null return value in the callback will be returned to the caller.
3263+
*patterns*: An array of patterns to match
3264+
*callback*: Either a string or an array with an object and method. The callback will get four arguments ($redis, $pattern, $channel, $message)
3265+
*return value*: Mixed. Any non-null return value in the callback will be returned to the caller.
29833266
##### *Example*
29843267
~~~
29853268
function pSubscribe($redis, $pattern, $chan, $msg) {
@@ -3070,13 +3353,13 @@ The return value can be various types depending on what the server itself return
30703353
##### *Example*
30713354
```php
30723355
/* Returns: true */
3073-
$redis->rawCommand("set", "foo", "bar");
3356+
$redis->rawCommand("set", "foo", "bar");
30743357

30753358
/* Returns: "bar" */
3076-
$redis->rawCommand("get", "foo");
3359+
$redis->rawCommand("get", "foo");
30773360

30783361
/* Returns: 3 */
3079-
$redis->rawCommand("rpush", "mylist", "one", 2, 3.5));
3362+
$redis->rawCommand("rpush", "mylist", "one", 2, 3.5));
30803363

30813364
/* Returns: ["one", "2", "3.5000000000000000"] */
30823365
$redis->rawCommand("lrange", "mylist", 0, -1);

0 commit comments

Comments
 (0)