Home  >  Article  >  Backend Development  >  PHP redis hIncrBy 递增出现问题

PHP redis hIncrBy 递增出现问题

WBOY
WBOYOriginal
2016-06-06 20:24:081865browse

PHP redis hIncrBy 递增出现问题

第一次请求的时候,hGetAll返回的count_comment58,然后hincrBy +1后再hGetAll,返回的count_comment预期为59却变成1了。没有实现预期递增效果。

PHP redis hIncrBy 递增出现问题

再次执行同样请求,hGetAll返回的count_comment1,然后hincrBy +1后再hGetAll,返回的count_comment2了。这次请求hincrBy +1是实现的预期递增。

PHP redis hIncrBy 递增出现问题

不太清楚是什么情况造成,找了好久没找到原因。
这里的redis中使用phpredis来连接
count_comment都是string类型。

回复内容:

PHP redis hIncrBy 递增出现问题

第一次请求的时候,hGetAll返回的count_comment58,然后hincrBy +1后再hGetAll,返回的count_comment预期为59却变成1了。没有实现预期递增效果。

PHP redis hIncrBy 递增出现问题

再次执行同样请求,hGetAll返回的count_comment1,然后hincrBy +1后再hGetAll,返回的count_comment2了。这次请求hincrBy +1是实现的预期递增。

PHP redis hIncrBy 递增出现问题

不太清楚是什么情况造成,找了好久没找到原因。
这里的redis中使用phpredis来连接
count_comment都是string类型。

我找到问题了

This is expected, you can't run INCR, INCRBY, or HINCRBY on serialized data.

初始化redis时,使用的是SERIALIZER格式下存储

<code>$redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_IGBINARY);</code>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn