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

search for in the

APCIterator> <apc_sma_info
[edit] Last updated: Fri, 25 May 2012

view this page in

apc_store

(PECL apc >= 3.0.0)

apc_store 変数をデータ領域にキャッシュする

説明

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

変数をデータ領域にキャッシュします。

注意: PHP の他の多くの仕組みと異なり、apc_store() を用いて格納された変数はリクエストを超えて (その値がキャッシュから取り除かれるまで)持続します。

パラメータ

key

この名前を用いて変数を格納します。key は キャッシュ内で一意です。そのため、同一の key で新しい値を格納すると、元の値は上書きされます。

var

格納する変数。

ttl

有効期間。var は、キャッシュに ttl 秒間だけ格納されます。 ttl が経過すると、格納されている変数は (次のリクエスト時に)キャッシュから削除されます。 ttl が指定されていない(あるいは ttl0 の場合)は、 キャッシュから手動で削除される・あるいはキャッシュに存在できなくなる (clear, restart など)まで値が持続します。

values

名前をキー、変数を値に指定します。

返り値

成功した場合に TRUE を、失敗した場合に FALSE を返します。 二番目の構文は、エラーになったキーを含む配列を返します。

例1 A apc_store() の例

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

上の例の出力は以下となります。

string(3) "BAR"

参考

  • apc_add() - 新規の変数をデータ領域にキャッシュする
  • apc_fetch() - 格納されている変数をキャッシュから取得する
  • apc_delete() - 格納されている変数をキャッシュから取り除く



APCIterator> <apc_sma_info
[edit] Last updated: Fri, 25 May 2012
 
add a note add a note User Contributed Notes apc_store
danil dot gazizov at gmail dot com 03-Feb-2012 11:49
Don't save empty arrays and empty values. Sometimes, you can get wrong apc_exists($someKey) result, that this key doesn't exists.
pere dot cil dot remove dot this at wanadoo dot fr 09-Dec-2011 01:49
Note that caching resources is not possible; even if the apc cache doesn't seems to call the serialize / unserialize functions, that doesn't means that resources can be cached!

Small non-working example:

<?php
// Setter code
$r = fopen( '/tmp/test.txt', 'r' );
var_dump( $r );
apc_store( 'test', $r );
?>

<?php
// Getter code
$d = apc_fetch( 'test' );
var_dump( $d );
echo
fread( $d, 1024 );
?>

var_dump( $d ) returns Resource #n of type (Unknown). The resource is still here, but unavailable.
Dominik Deobald / Interdose 17-Nov-2010 11: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.
TaRaKa 25-Aug-2010 01:32
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.
eda-qa at disemia dot com 05-Jan-2010 07: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-Mar-2008 12: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 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.

<?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);
}
?>
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.

 
show source | credits | sitemap | contact | advertising | mirror sites