伍佰目录 短网址
  当前位置:海洋目录网 » 站长资讯 » 站长资讯 » 文章详细 订阅RssFeed

PHP - Manual: apc_store

来源:网络转载 浏览:57次 时间:2023-07-24
APCIterator » « apc_sma_info PHP 手册 函数参考 影响 PHP 行为的扩展 APC APC 函数

apc_store

(PECL apc >= 3.0.0)

apc_store — Cache a variable in the data store

说明

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

缓存一个变量到APC中

Note: 与PHP中其他的机制不同,使用apc_store() 存储的变量 在不同的请求之间一直持久存在(直到从缓存系统中移除)。

参数

key

存储缓存变量使用的名称.key是唯一的,所以 两个值使用同一个 key,原来的将被新的值覆盖。

var

The variable to store

ttl

生存时间;在缓存中存储var共ttl秒, 在ttl秒过去后,存储的变量将会从缓存中擦除(在下一次请求时), 如果没有设置ttl(或者ttl是0), 变量将一直存活到被手动移除为止,除此之外不在缓存中的可能原因是, 缓存系统使用clear,或者restart等。

values

Names in key, variables in value.

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE。 Second syntax returns array with error keys.

范例

Example #1 apc_store() 例子

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

以上例程会输出:

string(3) "BAR"

参见

apc_add() - 缓存一个变量到数据存储 apc_fetch() - 从缓存中取出存储的变量 apc_delete() - 从用户缓存中删除某个变量
add a note

User Contributed Notes 14 notes

up down 18 JaskaS12 years ago 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());

?>
up down 5 alexey dot maksutov at gmail dot com6 years ago APC does serialization/deserialization during store/fetch operations as well as it calls __sleep()/__wakeup(), or Serializable::serialize()/Serializable::unserialize(). Tested on PHP 5.4.1.0. up down 2 MaxTheDragon at home dot nl6 years ago When specifying a ttl (Time-To-Live), you are allowed to use negative values. This causes a stored entry to be invalidated immediately, but note that it will not physically be removed until you read (eg. apc_fetch or apc_exists) it:

<?php
apc_store('testKey', 'testValue', -1); // ... or any negative integer.

// at this point, the key exists physically but is already technically invalidated by the ttl.

$test = apc_fetch('testKey'); // $test equals false.

// at this point, the key no longer exists physically.
?>

Although you could mimic apc_delete with a negative TTL (like when dealing with cookies), note that the difference is that apc_delete actually physically removes the entry.

This example comes in handy when you want to unit-test a class that uses APC. It's faster to use a negative TTL than a positive in combination with a sleep call to test whether the cache entry has actually been deleted. Other than that I can't think of any situations in which you could use this example, but I'm simply pointing out that doing this will not generate Exceptions or errors.
up down 2 sebastian at 7val dot com11 years ago 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). up down 0 mys5droid at gmail dot com3 years ago "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."

This is not a bug. Surprisingly, it has been assigned a bug id.It is obviously not a bug, because there is a man-made error message, as well as a switch to turn off slam checking.

Think about it, would you release code which modifies core system files without warning? No, you would make a warning for the user. Same is true with shared memory.

I happen to think it is a good feature, it tells you are overwriting something... And if you dislike it, turn it off using that setting, or use an @ in front of the function calls...
up down 0 Anonymous6 years ago Interesting info to read before apc_store() implementation:

http://stackoverflow.com/questions/10494744/deadlock-with-apc-exists-apc-add-apc-php
http://stackoverflow.com/questions/4468805/apc-values-randomly-disappear/4532905#4532905
up down 0 danil dot gazizov at gmail dot com7 years ago Don't save empty arrays and empty values. Sometimes, you can get wrong apc_exists($someKey) result, that this key doesn't exists. up down 0 eda-qa at disemia dot com9 years ago 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.
up down -1 brunohass2303 at gmail dot com6 years ago Functions to update arrays and get the values from an unique key.

<?php

function apc_array_store($apc_var, $key, $valor)
{
    $apcTemp = array();
   
    if ( $valor == NULL ) return FALSE;
   
    if ( $apcTemp = apc_fetch($apc_var) ) // Verifica se a variavel $apc_var existe no cache APC
    { // Se existir
        if ( !array_key_exists($apcTemp, $key) ) // Verifica se a chave $key existe no array
            $apcTemp[$key] = $valor; // Se $valor não for NULL, adiciona no array
       
        if ( apc_store("$apc_var", $apcTemp) ) // Tenta atualizar o array no cache
             return TRUE;
        else return FALSE;
    }
    else
    { // Se a variavel $apc_var nao existir no cache adiciona
        if ( $valor == NULL ) // Se $valor for NULL retorna FALSE
            return FALSE;
        else
        {    // Se $valor não for NULL, cria o array
            $apcTemp[$key] = $valor;
           
            if ( apc_add("$apc_var", $apcTemp) ) // Tenta adicionar o array no cache
                 return TRUE;
            else return FALSE;
        }
    }
   
}

function apc_array_fetch($apc_var, $key)
{

    if ( $apcTemp = apc_fetch($apc_var) ) // Verifica se a variavel $apc_var existe no cache APC
    { // Se existir
        if ( !array_key_exists($apcTemp, $key) ) // Verifica se a chave $key existe no array
                return FALSE; // Se não existir retorna FALSE
            else
                return $apcTemp[$key]; // Se existir retorna o valor
    }
    else // Se não existir
        return FALSE;
   
}

?>
up down -1 Roberto Spadim12 years ago be sure that setting FALSE values can be wrong returned from fetch since fetch return FALSE on errors up down -1 php at tequilasolutions dot com12 years ago 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);
}
?>
up down -1 pere dot cil dot remove dot this at wanadoo dot fr7 years ago 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.
up down -3 Dominik Deobald / Interdose8 years ago 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. up down -5 TaRaKa8 years ago 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. add a note

官方地址:https://www.php.net/manual/en/function.apc-store.php

  推荐站点

  • At-lib分类目录At-lib分类目录

    At-lib网站分类目录汇集全国所有高质量网站,是中国权威的中文网站分类目录,给站长提供免费网址目录提交收录和推荐最新最全的优秀网站大全是名站导航之家

    www.at-lib.cn
  • 中国链接目录中国链接目录

    中国链接目录简称链接目录,是收录优秀网站和淘宝网店的网站分类目录,为您提供优质的网址导航服务,也是网店进行收录推广,站长免费推广网站、加快百度收录、增加友情链接和网站外链的平台。

    www.cnlink.org
  • 35目录网35目录网

    35目录免费收录各类优秀网站,全力打造互动式网站目录,提供网站分类目录检索,关键字搜索功能。欢迎您向35目录推荐、提交优秀网站。

    www.35mulu.com
  • 就要爱网站目录就要爱网站目录

    就要爱网站目录,按主题和类别列出网站。所有提交的网站都经过人工审查,确保质量和无垃圾邮件的结果。

    www.912219.com
  • 伍佰目录伍佰目录

    伍佰网站目录免费收录各类优秀网站,全力打造互动式网站目录,提供网站分类目录检索,关键字搜索功能。欢迎您向伍佰目录推荐、提交优秀网站。

    www.wbwb.net