PHP: apc_store - Manual

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

search for in the

APD apc_sma_info

Last updated: Sat, 27 Jan 2007

view this page in

# apc_store

(PECL)

apc_store -- Cache a variable in the data store

## Описание

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

> Замечание: Unlike many other mechanisms in PHP, variables stored using apc_store() will persist between requests (until the value is removed from the cache).

## Список параметров

`key`

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

`var`

  The variable to store

`ttl`

  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.).

## Возвращаемые значения

Возвращает `TRUE` в случае успешного завершения или `FALSE` в случае возникновения ошибки.

## Примеры

Пример 1. A apc_store() example

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

Результат выполнения данного примера:

    string(3) "BAR"

## Смотрите также

apc_fetch()
apc_delete()

add a note User Contributed Notes
apc_store
JaskaS
01-Mar-2007 06: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 02: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 03: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 04: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 at metalshard dot com
28-Oct-2006 10:48
`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.`
add a note
APD apc_sma_info

Last updated: Sat, 27 Jan 2007
show source | credits | stats | sitemap | contact | advertising | mirror sites
Copyright © 2001-2007 The PHP Group
All rights reserved.
This mirror generously provided by: Yahoo! Inc.
Last updated: Sat Mar 10 09:32:46 2007 PST