Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Caching troubles with using
  • Hello! I have a little difficulties to understand how to use caching. 1. Db cache.
    Example.
    $sql = 'SELECT man FROM peoples WHERE man_age >= 20';
    DB::Query($sql)->cache(3600)->execute()->as_array(); After that I got one cache file in APPPATH.'cache/'.
    If I will refreshes my page with that query I will get many cache files.
    But should get one? or not? 2. Standard cache.
    That cache I do not understood.
    Some thing like that ? ->>
    $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);
    }
    

    Will be very very helpful you you can show little part of your code where you are using these two variants of caching.
  • 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.

    Ok, understood.
    try{
    cache::get('test');
    }catch(CacheNotFoundException $e){
    Log::error($e->getMessage());
    cache::set('test','Hello Man, I am here');
    }
    

    It is right way? :)
  • Not sure why you'd log the CacheNotFoundException as an error? I could see it logged as debug, but it's distinctly not an error. It's an exception you do expect when the value has not yet been cached.
  • 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.
  • 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.
    Thank you, I understood.
    And now, I will learn how to use try/catch block.
    Fuelphp 1.1
  • Something like that? try{
    Cache::get('test');
    }catch(????){
    Cache::set('test','test',3000);
    }
  • Jelmer 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
    Thanks. :) But I do not understood why I should use try catch if I can comment line where exception execute and sets manually
    return FALSE; And then use boolean answer to generate own cache? $cache_data = Cache::get('test');
    if($cache_data){
    use already cached data and send to view
    }else{
    generate cache and send to view
    } View->set(....).... Sure if I want to test my app, exceptions can explain to me where is errors, but in production mode - I think it is not very need.
    Or I not not understand something in that life? :)
  • 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.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion