apc_add()
(PECL apc >= 3.0.13)
缓存一个变量到数据存储
说明
apc_add(string $key, mixed $var[,int $ttl= 0]): bool
apc_add(array $values[,mixed $unused= NULL[,int $ttl= 0]]): array
仅仅在缓存变量不存在的情况下缓存变量到数据存储中
Note:与PHP中其他的机制不同,使用apc_add()存储变量在不同的请求之间一直持久存在(直到从缓存系统中移除)
参数
- $key
存储缓存变量使用的名称$keys 是唯一的,所以试图使用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"
参见
apc_store()
Cache a variable in the data storeapc_fetch()
从缓存中取出存储的变量apc_delete()
从用户缓存中删除某个变量
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.