Laravel 9 中的 Redis 门面 hset 不允许传递多个字段/值
1、尝试基于 Redis 门面 hset 传递多个字段/值,代码实现如下
1 2 3 | $cache_list_key = sprintf( 'order:batch:%s_list' , $batch_id ); Redis::hset( $cache_list_key , 'key' , $key , 'result' , $result , 'message' , $message ); Redis::expire( $cache_list_key , 86400); |
2、运行期间报错:local.ERROR: ERR wrong number of arguments for ‘hset’ command 。如图1
1 2 | [2024-03-28 08:09:35] local.ERROR: ERR wrong number of arguments for 'hset' command {"exception":"[object] (Predis\\Response\\ServerException(code: 0): ERR wrong number of arguments for 'hset' command at E:\\wwwroot\\object\\vendor\\predis\\predis\\src\\Client.php:384) [stacktrace] |
3、但是,从 Redis 4.0 起,HSET 可以一次设置一个或多个 field/value 对。Redis 4.0.0起,HMSET 被废弃,请使用 HSET 代替。
4、参考: Redis facade hset doesn’t allow passing multiple fields/values 。最终决定仍然使用 HSET,每一次只传一个字段,多次执行。代码实现如下
1 2 3 4 5 | $cache_list_key = sprintf( 'order:batch:%s_list' , $batch_id ); Redis::hset( $cache_list_key , 'key' , $key ); Redis::hset( $cache_list_key , 'result' , $result ); Redis::hset( $cache_list_key , 'message' , $message ); Redis::expire( $cache_list_key , 86400); |
5、不再报错,查看 Redis 中的结构,符合预期。如图2
近期评论