There is no problem not worth solving, and no technology not worth learning!

Redis facade in Laravel 9 HSET does not allow multiple fields/values to be passed

运行期间报错:local.ERROR: ERR wrong number of arguments for 'hset' command

1. Try to pass multiple fields/values based on the Redis facade Hset, the code is implemented as follows


$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. Error during operation: local.error: err wrong number of arguments forhsetcommand. as shown in Figure 1

运行期间报错:local.ERROR: ERR wrong number of arguments for 'hset' command
Figure 1

[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. However, starting from Redis 4.0, HSET can set one or more field/value pairs at a time. Since Redis 4.0.0, HMSET is discarded, please use HSET instead.

4. Reference: Redis Facade HSet DoesnT allow passing multiple fields/values . The final decision is still to use HSET, and only one field is passed at a time, and it is executed multiple times. The code is implemented as follows


$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. No more errors, check the structure in Redis, which is in line with expectations. as shown in Figure 2

不再报错,查看 Redis 中的结构,符合预期
Figure 2

PHP / Laravel / Yii2 Legacy Project Maintenance & Long-Term Technical Support

If your PHP / Laravel / Yii2 project is already in production but needs bug fixing, API troubleshooting, performance optimization, developer handover support, or long-term maintenance, feel free to contact me for remote technical support.

Ideal For:
✅ PHP legacy systems without active maintenance
✅ Laravel / Yii2 project bug fixes
✅ Admin panel feature iterations
✅ RESTful API troubleshooting
✅ MySQL / Redis / Nginx performance issues
✅ Long-term remote part-time maintenance

We can start with a small task:
✅ Production error troubleshooting
✅ API issue analysis
✅ Slow query and performance bottleneck diagnosis
✅ Initial code structure review
✅ Deployment environment and log inspection

If you would like to discuss your project, please contact me and mention: PHP Maintenance Consultation.

Contact Me:
Telegram: @shuijingwan
WeChat: 13980074657
Email: shuijingwanwq@gmail.com

评论

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.