PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

APD> <APC Funções
Last updated: Fri, 22 Aug 2008

view this page in

apc_store

(PECL apc:3.0.0-3.0.9)

apc_store Guarda uma variável no cache

Descrição

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

Armazena uma variável no cache.

Nota: Ao contrário de muitos outros mecanismos no PHP, variáveis guardadas usando apc_store() persistirão entre requisições (até que o valor seja removido do cache).

Parâmetros

key

Guarda a variável usando esse nome. key s são únicas para cada cache, então, guardar um segundo valor com a mesma key sobrescreverá o valor original.

var

A variável a ser guardada.

ttl

Tempo de vida; guarda var no cache por ttl segundos. Após ttl ter passado, a variável guardada será removida do cache (na próxima requisição). Se ttl não for passado (ou se ttl for 0), o valor persistirá até ser removido manualmente do cache, ou caso deixe de existir no cache (clear, restart, etc.).

Valor Retornado

Retorna TRUE em caso de sucesso ou FALSE em falhas.

Exemplos

Exemplo #1 Um exemplo de apc_store()

<?php
$bar 
'BAR';
apc_store('foo'$bar);
var_dump(apc_fetch('foo'));
?>

O exemplo acima irá imprimir:

string(3) "BAR"



APD> <APC Funções
Last updated: Fri, 22 Aug 2008
 
add a note add a note User Contributed Notes
apc_store
sebastian at 7val dot com
10-Mar-2008 09: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 http://pecl.php.net/bugs/bug.php?id=13331).
JaskaS
01-Mar-2007 03:06
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());

?>
Roberto Spadim
12-Jan-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
03-Nov-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 ($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);
}
Sudhee
30-Oct-2006 01: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.

APD> <APC Funções
Last updated: Fri, 22 Aug 2008
 
 
show source | credits | sitemap | contact | advertising | mirror sites