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.
apc_store
(PECL apc >= 3.0.0)
apc_store — Guardar una variable en caché en el almacén de datos
Descripción
Guarda una variable en caché en el almacén de datos.
Note: A diferencia de otros mecanismos de PHP, las variables almacenadas al usar apc_store() persistirán entre peticiones (hasta que el valor sea eliminado de la caché).
Parámetros
- key
-
Almacena la variable usando este nombre. Las claves (keys) son únicas en la caché, por lo que almacenar un segundo valor con la misma clave dada por key sobrescribirá el valor original.
- var
-
La variable a almacenar
- ttl
-
Time To Live (Tiempo de Vida); almacena var en la caché durante ttl segundos. Después de que pase ttl, la variable almacenada será expurgada de la caché (en la siguiente solicitud). Si no se proporciona ttl (o si ttl es 0), el valor persistirá hasta que sea eliminado de la caché manualmente, o si no dejará de existir (al limpiar, reiniciar, etc.).
- values
-
Nombres en clave, variables en valor.
Valores devueltos
Devuelve TRUE en caso de éxito o FALSE en caso de error. La segunda sintaxis devuelve un array con claves de errores.
Ejemplos
Example #1 Un ejemplo de apc_store()
<?php
$bar = 'BAR';
apc_store('foo', $bar);
var_dump(apc_fetch('foo'));
?>
El resultado del ejemplo sería:
string(3) "BAR"
Ver también
- apc_add() - Poner una variable en caché en el almacén de datos
- apc_fetch() - Traer una variable almacenada desde la caché
- apc_delete() - Elimina una variable almacenada de la caché
Note APC version 3.1.3 there is a bug (http://pecl.php.net/bugs/bug.php?id=16814) 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.
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.
Note that since APC 3.0.15 or 3.0.16, the time-to-live-feature does not work within the same request (see http://pecl.php.net/bugs/bug.php?id=13331).
if you want to store array of objects in apc use ArrayObject wrapper (PHP5).
<?php
$objs = array();
$objs[] = new TestClass();
$objs[] = new TestClass();
$objs[] = new TestClass();
//Doesn't work
apc_store('objs',$objs,60);
$tmp = apc_fetch('objs');
print_r($tmp);
//Works
apc_store('objs',new ArrayObject($objs),60);
$tmp = apc_fetch('objs');
print_r($tmp->getArrayCopy());
?>
be sure that setting FALSE values can be wrong returned from fetch since fetch return FALSE on errors
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.
<?php
function apc_fetch_udt($key){
$g = apc_fetch($key);
if ($g){
list($udt,$val) = $g;
if (get_last_modified_date()<$udt) {
$val = unserialize($val);
return $val;
} else {
apc_delete($key);
}
}
}
function apc_store_udt($key,$g){
$udt = time();
$g = serialize($g);
$apc = array($udt,$g);
apc_store($key, $apc);
}
?>
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.
