$cache = Cache::get('test'); if($cache){ //using cache whatever I want. }else{ $sql = 'SELECT man FROM peoples where man_age >= 20'; $db_answer = DB::Query($sql)>execute()->as_array(); Cache::set('test', $db_answer, 3600 * 3); }
Jelmer Schreuder wrote on Wednesday 28th of March 2012:false, null, '' (empty string) and 0 (zero) are all valid values to cache if the operation to get to it is heavy. You couldn't test for those in your way, thus exceptions are used to singal when retrieval failed. When cache is retrieved succesfully there's no penalty to using exceptions, only when it couldn't be retrieved is there an incredibly small added overhead for creating the exception. That overhead however should be completely insignificant compared to the code that follows to generate whatever you want cached, and if that's not the case you shouldn't be caching it.
try{ cache::get('test'); }catch(CacheNotFoundException $e){ Log::error($e->getMessage()); cache::set('test','Hello Man, I am here'); }
Thank you, I understood.Harro Verton wrote on Tuesday 27th of March 2012:In case 1, a reload should read and use the cache file, not run the query again, and not create a second cache file. Is that what you say happens? Which version of FuelPHP are you using? In case 2, see the example in the docs. get() with throw an exeption if the cache does not exist, so you need a try/catch block, not an if block. Other then that, the code is ok.
Thanks. But I do not understood why I should use try catch if I can comment line where exception execute and sets manuallyJelmer Schreuder wrote on Wednesday 28th of March 2012:Along those lines, you might want to check the docs: http://docs.fuelphp.com/classes/cache/usage.html#/method_get
It looks like you're new here. If you want to get involved, click one of these buttons!