Redis SCARD returning incorrect results?
I am adding a large number of data points to the 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 get is9866
$t
is the counter I set up to check how many iterations the loop is doing, and $t
is 9866
after the loop starts, which is strange considering that scard returns9832
Then I thought that maybe duplicates are being added, so I logged an answer 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 added item is unique. There are also 9866
log calls, which contradicts the result scard
. I tried validation with redis-cli and I still get wrong results. What gives?
I was actually injecting values ββwith a variable:
$redis->sadd('database/ab/state:'.$a->state, a->id);
it turns out that some of the states were in lower case, which passed these values ββto another key.
Correction: $redis->sadd('database/ab/state:'.strtoupper($a->state), a->id);
now i am getting the correct number 9866
when i callSCARD
Always double check your key names!