apc_fetch() and apc_store() usage in php

3.6k views Asked by At

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.

1

There are 1 answers

0
BetaDev On BEST ANSWER

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