salex772
(Salex772)
September 11, 2013, 7:44pm
1
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()
konapaz
(Konapaz)
September 11, 2013, 10:15pm
2
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;
duckbill
(duckbill)
December 3, 2024, 10:06am
4
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’.