R
R
romany42015-08-05 14:07:24
PHP
romany4, 2015-08-05 14:07:24

How to properly extract data from bitmap-a redis?

Let's say there is a final key in redis from 0\1 (bitmap).
1 - the product met the condition, 0 - not.
Position 0\1 means product id.
50000 items.
How to correctly extract the positions of goods that fit the conditions?

$ar = [];
for($i = 0; $i < 50000; $i++){
  if ($redis->getBit('test', $i) == 1){
    $ar[] = $i;
  }
}

But this code clearly smells)) Therefore, tell me the most correct solution or direction.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
romany4, 2015-08-05
@romany4

for ($i = 0; $i < $len; $i++){
  $tmp = ord($string[$i]);
  for($j=0; $j<8; $j++){
    $result[] = (($tmp & (1 << $j)) != 0);
  }
}

smoked mana and settled on this decision.

E
evnuh, 2015-08-05
@evnuh

50000 roundtrips to the radish from php for each page request is strong )
just take the entire 'test' key from the radish once, and then bit arithmetic will help you.

L
Lesha Kiselev, 2015-08-05
@Yakud

$redisProducts = $redis->get('test');

$ar = [];
for($i = 0; $i < 50000; $i++){
    if ($redisProducts & pow(2, $i)){
        $ar[] = $i;
    }
}

php.net/manual/en/language.operators.bitwise.php

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question