Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redis SCARD returning the wrong results?

Tags:

php

redis

predis

I'm adding a large number of data points into a redis set:

$t = 0;
$redis = Redis::Connection();
foreach($adv as $a) {
    $t = $t + 1;
    print($t); //Prints to log

    $test = $redis -> sadd('database/ab/AL', $a -> id);
    print($test); //Prints to log
}

When I call redis->scard('database/ab/AL') I get the result 9832, The answer I should be getting is 9866

$t is a counter I put in to check how many iterations the loop is doing, and $t is 9866 after running the loop, which is weird considering scard is returning 9832

Then I thought maybe there are duplicates being added, so I logged the response from sadd

1 [2015-06-29 16:24:55] local.INFO: 1 [] []
2 [2015-06-29 16:24:55] local.INFO: 1 [] []
3 [2015-06-29 16:24:55] local.INFO: 1 [] []
4 [2015-06-29 16:24:55] local.INFO: 1 [] []
5 [2015-06-29 16:24:55] local.INFO: 1 [] []
6 [2015-06-29 16:24:55] local.INFO: 1 [] []
...
9861 [2015-06-29 16:24:59] local.INFO: 1 [] []
9862 [2015-06-29 16:24:59] local.INFO: 1 [] []
9863 [2015-06-29 16:24:59] local.INFO: 1 [] []
9864 [2015-06-29 16:24:59] local.INFO: 1 [] []
9865 [2015-06-29 16:24:59] local.INFO: 1 [] []
9866 [2015-06-29 16:24:59] local.INFO: 1 [] []

There are no zeros in the entire log, which means that each element being added is unique. There are also 9866 log calls which contradicts the scard result. I have tried checking with redis-cli and I still get the wrong results. What gives?

like image 780
RN_ Avatar asked Jan 23 '26 11:01

RN_


1 Answers

I was actually keying the values with a variable:

$redis->sadd('database/ab/state:'.$a->state, a->id);

turns out some states were in lowercase, which sent those values to another key.

Fix: $redis->sadd('database/ab/state:'.strtoupper($a->state), a->id);

now I get the correct number, 9866, when I call SCARD

Always double check your key names!

like image 80
RN_ Avatar answered Jan 25 '26 00:01

RN_



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!