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

Already on GitHub? Sign in to your account

Array support for sAdd #149

Closed
carrier opened this Issue Mar 23, 2012 · 12 comments

Comments

Projects
None yet
8 participants

carrier commented Mar 23, 2012

Would be nice if sAdd had support for arrays when submitting multiple values.

Example:

$arr = array(1, 2, 3, 4, 5);
$r->sAdd("num", $arr);

Contributor

nicolasff commented Mar 23, 2012

Hello,

You should use call_user_func_array for this.

Nicolas

@nicolasff nicolasff closed this Mar 23, 2012

carrier commented Mar 23, 2012

Poked around with call_user_func_array for a while but didn't get it
work the way I was looking for.
From what I understand, call_user_func_array requires the callback
function to define each variable that you want to use.

But what if size of the array changes with each call or if I have a
large array, that would be quite ugly and painful to write.

Anyhow, I just looked briefly at call_user_func_array and maybe I
missed something.
Thanks for replying anyway, and if you have a solution to my problem I
would be really grateful.

Example:
call_user_func_array("func", $arr);
function func($_1, $_2, $_3, $_4, $_5, $_6, $_7, $_8, $_9, $_10, $_11,
$_12, $_13, $_14, $_15, $_16, $_17, $_18, $_19, $_20, $_11, $_22, $_23,
$_24, $_25)
{
$redis->sadd("urls", $_1, $_2, $_3, $_4, $_5, $_6, $_7, $_8, $_9,
$_10, $_11, $_12, $_13, $_14, $_15, $_16, $_17, $_18, $_19, $_20, $_11,
$_22, $_23, $_24, $_25);
}

Den den 23 mars 2012 11:50:11 skrev Nicolas Favre-Felix:

Hello,

You should use call_user_func_array for this.

Nicolas


Reply to this email directly or view it on GitHub:
nicolasff#149 (comment)

Contributor

nicolasff commented Mar 23, 2012

You're almost there, you can use “sadd” on $redis as the function by wrapping it in an array:

$myArray = array($key, $url1, $url2... );
call_user_func(array($redis, "sadd"), $myArray);

Don't forget to set the key as the first parameter. Please also note that this only works with Redis >= 2.4

carrier commented Mar 23, 2012

Den den 23 mars 2012 16:15:00 skrev Nicolas Favre-Felix:

You're almost there, you can use “sadd” on $redis as the function by wrapping it in an array:

$myArray = array($key, $url1, $url2... );
call_user_func(array($redis, "sadd"), $myArray);

Don't forget to set the key as the first parameter. Please also note that this only works with Redis>= 2.4


Reply to this email directly or view it on GitHub:
nicolasff#149 (comment)

Ahh, mixed up a few things there but now it works.
Thanks a lot for the help!

Insted of using call_user_func pls use call_user_func_array

$myArray = array($key, $url1, $url2... );
call_user_func_array(array($redis, "sadd"), $myArray);

I've used the call_user_func_array() recently for this purpose and works just fine.

But submitting all the set members as individual parameters seems to depend a bit too much on php internals. For argument's sake, if php were to limit the max number of parameters for functions, that would be an upper bound on the count of members we can submit to redis.

IMHO, if possible, submitting all values directly as an array could rely less on other factors.

You can't have the function assume an array passed are set members. It could be a single member that is an array to be serialized. So any change in this respect would possibly break sadd for use cases. As for alternative solutions that don't involve changing phpredis is to create a class that extends Redis and overload sadd.

As for changing the library itself, there could be a function like "smadd" added but with keeping to standard redis commands that could be undesirable or arguably redundant. Or there could simply be a setting that could be switched on to configure the behavior of sadd (or any other redis function related to the issue) to assume an array are multiple members to be processed. I think the latter is better or just rely on devs to wrap phpredis how they like.

Hast commented May 1, 2013

IMHO, if possible, submitting all values directly as an array could rely less on other factors.

Yes, it really would be nice. Nicolasff, can you tell us why you suggest to use call_user_func instead of it?

Contributor

nicolasff commented May 1, 2013

@Hast just like @scragg0x said: You can't have the function assume an array passed are set members. It could be a single member that is an array to be serialized. So any change in this respect would possibly break sadd for use cases.

I suggest add a new method such as sMadd($key,$array) to explicitly indicate $array is an array of values would be add to set (only its values need to be serialized)

the sMadd method parses array values to redis protocol : 'sadd value1 value2 ... '

it can reduce io rather than use call_user_func_array

or sAdd($key,$array,$isArray=FALSE)

Owner

michael-grunder commented May 8, 2015

@netweaver That's not a terrible idea. I prefer using the same name as the Redis command but it might be worth it in this case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment