Search code examples
phpapc

apc_fetch() and apc_store() usage in php


What's the usage of apc_store(); and apc_fetch(); on the following example?

function getData($uid){
    $cached = apc_fetch($uid);
    return $cached?$cached:"Start";
}

function setData($uid,$step){
    apc_store($uid,$step,60*60*12);
}

I want to store string and then use it later. I can't understand the main and general explanations about these two functions in PHP.


Solution

  • The first method getData($uid) simply returns the value of $uid from cache and if there is not variable for the key $uid then it will returns a string "Start". So simply returns the value of $uid from cache.

    And the second method setData($uid,$step) defines a variable in cache as $uid storing the value as $step with ttl 60*60*12.

    TTL is the time that defines life of the cached variable $uid. (Time To Live; store var in the cache for ttl seconds.)

    apc_store($key,$val,$time_in_seconds);
    //This method stores variable in alternative PHP cache and this is used for performance.
    

    For more detail, see:

    1. apc_store()
    2. apc_fetch

    Also take a look at apc