Skip to content

Commit

Permalink
修复当开启 Redis 序列化时,geoAdd() 会报错:ERR value is not a valid float (#579)
Browse files Browse the repository at this point in the history
* 修复当开启 Redis 序列化时,geoAdd() 会报错:ERR value is not a valid float
phpredis 官方认为这不算 BUG:https://github.com/phpredis/phpredis/issues/1549,所以这里做了一个兼容处理

* 在 Windows 上不测试 geoadd,因为 windows 版 redis 版本太低不支持 geo

* 更新文档
  • Loading branch information
Yurunsoft authored Aug 21, 2023
1 parent f4430e1 commit 59f2238
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
10 changes: 10 additions & 0 deletions doc/base/qa.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,13 @@ net.ipv4.tcp_wmem = 4096 32768 262142
* Swoole 常驻内存并且有协程,所以 `worker_num` 建议设为 CPU 的 1-2 倍。

* Workerman 常驻内存但依然是阻塞的,`count` 可参考 php-fpm 进程数的配置,并酌情减少一定的数量。

## Redis 报错 ERR value is not a valid float

该问题主要出现在,当启用 Redis 序列化时候,方法的参数值会被序列化后传输。

官方认为这不算 BUG:<https://github.com/phpredis/phpredis/issues/1549>

imi v2.1.53 中对 `geoAdd()` 做了兼容处理。

如果你在调用其它 Redis 方法报这个类似的错误,可以尝试关闭 Redis 连接池配置中的序列化,然后到 imi 的 github 提 issue,我们会在后续版本中做兼容处理。
30 changes: 30 additions & 0 deletions src/Redis/RedisHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,36 @@ public function zscanEach(string $key, ?string $pattern = null, int $count = 0)
}
}

/**
* geoadd.
*
* 当开启序列化后,经纬度会被序列化,并返回错误:ERR value is not a valid float
*
* 如下链接,官方认为这不算 BUG,所以这里做了一个兼容处理
*
* @see https://github.com/phpredis/phpredis/issues/1549
*
* @param float|string $lng
* @param float|string $lat
* @param mixed ...$other_triples_and_options
*
* @return mixed
*/
public function geoadd(string $key, $lng, $lat, string $member, ...$other_triples_and_options)
{
$redis = $this->redis;
$serializer = $redis->getOption(\Redis::OPT_SERIALIZER);
$redis->setOption(\Redis::OPT_SERIALIZER, \Redis::SERIALIZER_NONE);
try
{
return $redis->geoadd($key, $lng, $lat, $member, ...$other_triples_and_options);
}
finally
{
$redis->setOption(\Redis::OPT_SERIALIZER, $serializer);
}
}

/**
* 是否为集群.
*/
Expand Down
15 changes: 15 additions & 0 deletions tests/unit/Component/Tests/RedisTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,19 @@ public function testZscanEach(): void
}
$this->assertEquals($excepted, $map);
}

public function testGeoAdd(): void
{
if (\PHP_OS_FAMILY === 'Windows')
{
$this->markTestSkipped('Windows redis not support geo.');
}
foreach ([
'redis_test', // 开启序列化
'redis_cache', // 禁用序列化
] as $poolName)
{
$this->assertNotFalse(Redis::use(static fn (RedisHandler $redis) => $redis->geoAdd('imi:geo', 120.31858, 31.49881, $poolName), $poolName));
}
}
}

0 comments on commit 59f2238

Please sign in to comment.