Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redis facade hset doesn't allow passing multiple fields/values #41829

Closed
jonnylink opened this issue Apr 4, 2022 · 2 comments
Closed

Redis facade hset doesn't allow passing multiple fields/values #41829

jonnylink opened this issue Apr 4, 2022 · 2 comments

Comments

@jonnylink
Copy link

jonnylink commented Apr 4, 2022

  • Laravel Version: 9.6.0
  • PHP Version: 8.1.2
  • Redis Version: 6.2.6
  • Redis Connection: PhpRedis

Description:

Laravel PHP Redis Connection's hset implementation doesn't properly allow passing multiple fields/values. hmset does allow this, but it is deprecated as of Redis v4. I would expect hset to function properly like hmset.

I did a quick dig and it looks like hset is really using hsetnx behind the scenes which is confusing since the Laravel docs specifically say the facade can call any Redis command.

Steps To Reproduce:

Easily reproduced in tinker

>>> use Illuminate\Support\Facades\Redis;
>>> Redis::hset('foo', 'cow', 'moo', 'sheep', 'baa')
TypeError: Redis::hSet() expects exactly 3 arguments, 5 given

>>> Redis::hmset('foo', 'cow', 'moo', 'sheep', 'baa')
=> true

>>> Redis::hgetall('foo')
=> [
     "cow" => "moo",
     "sheep" => "baa",
   ]

In the docs and as demonstrated in the redis-cli below, hset allows multiple entries:

redis /run/redis/redis.sock> hset 'bar' 'cat' 'meow' 'dog' 'bark'
(integer) 2

redis /run/redis/redis.sock> hgetall 'bar'
1) "cat"
2) "meow"
3) "dog"
4) "bark"
@jonnylink
Copy link
Author

Damnit. This is an issue further upstream: phpredis/phpredis#1768

@eduance
Copy link
Contributor

eduance commented Sep 3, 2022

In case anyone is still finding this thread, by serializing the array it will work.

I have fixed it by:

Redis::hset(123, 'subjects', json_encode($request->get('subjects')));

Redis::hget(123, 'subjects');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants