©
Dieses Dokument verwendet PHP-Handbuch für chinesische Websites Freigeben
(PECL apc >= 3.0.13)
apc_add — 缓存一个变量到数据存储
$key
, mixed $var
[, int $ttl
= 0
] )$values
[, mixed $unused
= NULL
[, int $ttl
= 0
]] )仅仅在缓存变量不存在的情况下缓存变量到数据存储中
Note: 与PHP中其他的机制不同,使用 apc_add() 存储变量 在不同的请求之间一直持久存在(直到从缓存系统中移除)
key
存储缓存变量使用的名称key
s 是唯一的,
所以试图使用 apc_add() 去添加一个名称已经存在的缓存,
将不会覆盖现有的缓存的值, 并且返回 FALSE
. (这个是 apc_add() 和
apc_store() 之间唯一的不同.)
var
存储的变量
ttl
生存时间;在缓存中存储var
共ttl
秒,
在ttl
秒过去后,存储的变量将会从缓存中擦除(在下一次请求时),
如果没有设置ttl
(或者ttl
是0),
变量将一直存活到被手动移除为止,除此之外不在缓存中的可能原因是,缓存系统使用clear,或者restart等
values
Names in key, variables in value.
Returns TRUE if something has effectively been added into the cache, FALSE otherwise. Second syntax returns array with error keys.
Example #1 apc_add() 例子
<?php
$bar = 'BAR' ;
apc_add ( 'foo' , $bar );
var_dump ( apc_fetch ( 'foo' ));
echo "\n" ;
$bar = 'NEVER GETS SET' ;
apc_add ( 'foo' , $bar );
var_dump ( apc_fetch ( 'foo' ));
echo "\n" ;
?>
以上例程会输出:
string(3) "BAR" string(3) "BAR"
[#1] liv_romania at yahoo dot com [2015-10-28 19:39:03]
In order to understand better how APC caching works you can do the following:
1. Restart web server (Apache or Nginx)
2. Create file "apc_fetch.php":
<?php
var_dump(apc_fetch(array(
'CUR_DATE_5s_1',
'CUR_DATE_5s_2',
'CUR_DATE_5s_3',
'CUR_DATE_0s_1',
'CUR_DATE_0s_2',
'CUR_DATE_0s_3',
)));
?>
3. Create file "apc_add.php":
<?php
$ttl = 5;
$key = 'CUR_DATE_5s_1';
$var = date('c');
$result = apc_add($key, $var, $ttl);
var_dump($result);
echo "\n";
$var = date('c');
$result = apc_add($key, $var, $ttl);
var_dump($result);
echo "\n";
$key = 'CUR_DATE_0s_1';
$var = date('c');
$result = apc_add($key, $var);
var_dump($result);
echo "\n";
$values = array(
'CUR_DATE_5s_2' => date('c'),
'CUR_DATE_5s_3' => rand(),
);
$result = apc_add($values, null, $ttl);
var_dump($result);
echo "\n";
$values = array(
'CUR_DATE_0s_2' => date('c'),
'CUR_DATE_0s_3' => rand(),
);
$result = apc_add($values, null);
var_dump($result);
?>
4. Run "apc_fetch.php" and "apc_add.php" several times in order to see the persistent result and how values change from one request to another.
[#2] zhubaiying at 126 dot com [2013-04-17 14:25:02]
$cache = apc_cache('demo' ,'demo');
//return true
$cache = apc_cache('demo', 'other');
//return false