0

The code is very simple

$mem = $this->memcache->get("memche_".$_SESSION['userid']."_page_".$page);

if(empty($mem)){
// to make another query and save the data to memcache
}
else {
// to get it from memcache
}

But right now there is no data but cache is not empty if i print_r($mem); I get

a:0:{}

And this pass the if statement if(empty())

Marian Petrov
  • 625
  • 2
  • 9
  • 21
  • How do you save your data to memcache? Looks to me you are saving a serialized array, correct? – Andreas Wong Jun 28 '12 at 08:17
  • $this->memcache->set("memche_".$_SESSION['userid']."_page_".$page, serialize($data), 0, 60*20); Yes @SiGanteng thats correct – Marian Petrov Jun 28 '12 at 08:19
  • 2
    you're saving serialized array, but you don't unserialize it after getting. `$mem = unserialize($mem); // now empty($mem) === true` – German Rumm Jun 28 '12 at 08:35

1 Answers1

3

That is not a bug, you have an empty array stored in memcache. Your array is empty, not the variable referencing the array.

var_dump(unserialize('a:0:{}'));
array(0) {
}

You should be checking if $mem is false ($mem===FALSE), not if it is empty. Memcache returns FALSE if the cache key doesn't exist.

Brent Baisley
  • 12,641
  • 2
  • 26
  • 39