PHP Doku:: Cache a variable in the data store - function.apc-store.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzDas Verhalten von PHP beeinflussenAlternativer PHP CacheAPC Funktionenapc_store

Ein Service von Reinhard Neidl - Webprogrammierung.

APC Funktionen


Advanced PHP debugger>>


(PECL apc >= 3.0.0)

apc_store Cache a variable in the data store


bool apc_store ( string $key , mixed $var [, int $ttl = 0 ] )

Cache a variable in the data store.

Hinweis: Unlike many other mechanisms in PHP, variables stored using apc_store() will persist between requests (until the value is removed from the cache).



Store the variable using this name. keys are cache-unique, so storing a second value with the same key will overwrite the original value.


The variable to store


Time To Live; store var in the cache for ttl seconds. After the ttl has passed, the stored variable will be expunged from the cache (on the next request). If no ttl is supplied (or if the ttl is 0), the value will persist until it is removed from the cache manually, or otherwise fails to exist in the cache (clear, restart, etc.).


Gibt bei Erfolg TRUE zurück. Im Fehlerfall wird FALSE zurückgegeben.


Beispiel #1 A apc_store() example


Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

string(3) "BAR"

Siehe auch

8 BenutzerBeiträge:
- Beiträge aktualisieren...
Dominik Deobald / Interdose
17.11.2010 20:17
It might be interesting to note that storing an object in the cache does not serialize the object, i.e. does not call the __sleep()/__wakeup() or serialize()/unserialize() methods.
25.08.2010 10:32
Note APC version 3.1.3 there is a bug ( that will display a cache slam averted warning for all writes to a cache var that exists. Slam checking can be disabled by setting apc.slam_defense = 0.
eda-qa at disemia dot com
5.01.2010 16:40
Note that the TTL only takes effect when you attempt to access the variable again (at least in my version).  That is, just issuing a new request to a page won't clear outdated items -- you have to call apc_fetch on that specific item.

If you call apc_info after the TTL of an item it will still be listed.

This is important if you are expecting items to be cleared to conserve memory.
sebastian at 7val dot com
10.03.2008 9:53
Note that since APC 3.0.15 or 3.0.16, the time-to-live-feature does not work within the same request (see
1.03.2007 15:06
if you want to store array of objects in apc use ArrayObject wrapper (PHP5).

= array();
$objs[] = new TestClass();
$objs[] = new TestClass();
$objs[] = new TestClass();

//Doesn't work
$tmp = apc_fetch('objs');

apc_store('objs',new ArrayObject($objs),60);
$tmp = apc_fetch('objs');

Roberto Spadim
12.01.2007 11:11
be sure that setting FALSE values can be wrong returned from fetch since fetch return FALSE on errors
php at tequilasolutions dot com
3.11.2006 12:45
Seems to be no (easy) way at the to know how old a value fetched is and to check whether it is out of date.

I've made these wrappers so that you can fetch and store values based on a udt returned from get_last_modified_date() which should return a udt of when your data was last changed, and hence needs junking out of the cache.

function apc_fetch_udt($key){
$g = apc_fetch($key);
    if (
$udt,$val) = $g;
        if (
get_last_modified_date()<$udt) {
$val = unserialize($val);
        } else {
$udt = time();
$g   = serialize($g);
$apc = array($udt,$g);
apc_store($key, $apc);
30.10.2006 13:09
It should be noted that apc_store appears to only store one level deep.  So if you have an array of arrays, and you store it.  When you pull it back out with apc_fetch it will only have the top level row of keys with nulls as the values of each key.
Solution to this, is to serialize the data before storing it in the cache and unserialize it while retrieving from the cache.

PHP Powered Diese Seite bei
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)