Cache Boolean Values

Hi everybody,

I need to cache some heavy function result, which returns true or false.




$bool_var = Yii::app()->cache->get($cache_id);

if ($bool_var === false) {

    $bool_var  = VeryHeavyFunction_Which_May_Return_false;

    Yii::app()->cache->set($cache_id, $bool_var, 60);

}		



In my case, $bool_var may be ‘false’ by function result, this does not mean that cache in not valid. Is there any way to validate cache values without ‘=== false’. Smth like IscacheValid() ???

Hi

I understood the problem

You could make this trick


//1) create a simple empty class MyFalseClass


$bool_var = Yii::app()->cache->get($cache_id);

if ($bool_var === false) {

    $bool_var  = VeryHeavyFunction_Which_May_Return_false;

    if ($bool_var === false)  $bool_var = new MyFalseClass() 

    Yii::app()->cache->set($cache_id, $bool_var, 60);

}

//now, retrieve the real false 

if ($bool_var instanceof MyFalseClass) $bool_var = false;

[size=2]Thanx![/size]

for anyone coming from google
a simper solution would be caching some string, or other non boolean value.

Yii::app()->cache->set($cache_id, $bool_var ? 'yes' : 'no', 60);

you could also have an enum / constants to make the values consistent and not rely on ‘yes’ as a string.

this way if

Yii::app()->cache->get()

returns false, you know its not cached, because if it was, it woud have returned ‘no’.